0% found this document useful (0 votes)
16 views12 pages

Module-2-DSA-T5-Application of List

The document discusses the representation and manipulation of polynomials using singly linked lists in data structures and algorithms. It details the structure of polynomial nodes, methods for adding polynomials, and the analysis of polynomial addition complexity. Additionally, it outlines functions for reading, printing, and erasing polynomials.

Uploaded by

5449644.somesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

Module-2-DSA-T5-Application of List

The document discusses the representation and manipulation of polynomials using singly linked lists in data structures and algorithms. It details the structure of polynomial nodes, methods for adding polynomials, and the analysis of polynomial addition complexity. Additionally, it outlines functions for reading, printing, and erasing polynomials.

Uploaded by

5449644.somesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MODULE-II

BCSE202L -
COURSE INSTRUCTOR: DATA STRUCTURES AND
DR. OM KUMAR C.U. ALGORITHMS
ASSISTANT PROFESSOR,
SCOPE,VIT.

DR. OM KUMAR C.U. 20/01/2025 1


POLYNOMIALS

DR. OM KUMAR C.U. 20/01/2025 2


POLYNOMIALS (1/9)

 Representing Polynomials As Singly Linked Lists


 The manipulation of symbolic polynomials, has a classic
example of list processing.
 In general, we want to represent the polynomial:

A( x ) am  1 x em  1   a0 x e0

 Where the a are nonzero coefficients and the e are


i i
nonnegative integer exponents such that
em-1 > em-2 > … > e1 > e0 ≧ 0 .
 We will represent each term as a node containing coefficient
DR. OM KUMAR C.U. 20/01/2025 3

and exponent fields, as well as a pointer to the next term.


POLYNOMIALS (2/9)
 Assuming that the coefficients are integers, the type
declarations are:
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef; a 3 x14  2 x 8  1
int expon;
poly_pointer link;
};
b 8 x 14  3x 10  10 x 6
poly_pointer a,b,d;
 Draw poly_nodes as:

DR. OM KUMAR C.U.


coef expon link 20/01/2025 4
POLYNOMIALS (3/9)
 Adding Polynomials
 To add two polynomials,we examine their terms starting at the nodes
pointed to by a and b.
 If the exponents of the two terms are equal

1. add the two coefficients


2. create a new term for the result.
 If the exponent of the current term in a is less than b

1. create a duplicate term of b


2. attach this term to the result, called d
3. advance the pointer to the next term in b.
 We take a similar action on a if a->expon > b->expon.
DR. OM KUMAR C.U. 20/01/2025 5

 Figure 4.12 generating the first three term of


POLYNOMIALS
(4/9)

DR. OM KUMAR C.U. 20/01/2025 6


POLYNOMIALS
(5/9)

 Add two
polynomials

DR. OM KUMAR C.U. 20/01/2025 7


POLYNOMIALS (6/9)
 Attach a node to the end of a list
void attach(float coefficient, int exponent, poly_pointer *ptr){
/* create a new node with coef = coefficient and expon = exponent, attach it to
the node pointed to by ptr. Ptr is updated to point to this new node */
poly_pointer temp;
temp = (poly_pointer) malloc(sizeof(poly_node));
/* create new node */
if (IS_FULL(temp)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
temp->coef = coefficient; /* copy item to the new node */
temp->expon = exponent;
(*ptr)->link = temp; /* attach */
DR. OM KUMAR C.U. 20/01/2025 8

*ptr = temp; /* move ptr to the end of the list */}


POLYNOMIALS (7/9)

 Analysis of padd
A( x )( am  1 x em  1   a0 x e0 )  B ( x )( bn  1 x f n  1    b0 x f0 )

1. coefficient additions
0  additions  min(m, n)
where m (n) denotes the number of terms in A (B).
2. exponent comparisons
extreme case:
em-1 > fm-1 > em-2 > fm-2 > … > e1 > f1 > e0 > f0
m+n-1 comparisons
3. creation of new nodes
extreme case: maximum number of terms in d is m+n m + n new
nodes
summary: O(m+n)
DR. OM KUMAR C.U. 20/01/2025 9
POLYNOMIALS (8/9)

 A Suite for Polynomials read_poly()


e(x) = a(x) * b(x) + d(x) print_poly()
padd()
poly_pointer a, b, d, e;
psub()
...
pmult()
a = read_poly();
b = read_poly();
temp is used to hold a partial result.
d = read_poly(); By returning the nodes of temp, we
may use it to hold other polynomials
temp = pmult(a, b);
DR. OM KUMAR C.U. e = padd(temp, d); 20/01/2025 10

print_poly(e);
POLYNOMIALS (9/9)
 Erase Polynomials
 erase frees the nodes in temp
void erase (poly_pointer *ptr){
/* erase the polynomial pointed to by ptr */
poly_pointer temp;
while ( *ptr){
temp = *ptr;
*ptr = (*ptr) -> link;
DR. OM KUMAR C.U.
free(temp); 20/01/2025 11

}}
THANK YOU

DR. OM KUMAR C.U. 20/01/2025 12

You might also like