7/27/2019 Apllications of Linked Lists
1/19
Applications of Linked Lists
1. Polynomial Representation and operation on Polynomials
Ex: 10 X 6 +20 X 3 + 55
2. Sparse Matrix Representation
0 0 11
22 0 0
0 0 66
7/27/2019 Apllications of Linked Lists
2/19
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef;int expon;poly_pointer next;
};poly_pointer a, b, c;
A x a x a x a xm
e
m
e em m( ) . . .= + + +
1 2 0
1 2 0
Representation
Polynomials
coef expon link
7/27/2019 Apllications of Linked Lists
3/19
3 14 2 8 1 0a
8 14 -3 10 10 6b
a x x= + +3 2 11 4 8
b x x x= +8 3 1 014 10 6
null
null
Example
7/27/2019 Apllications of Linked Lists
4/19
Polynomial Operation
1. Addition2. Subtraction
3. Multiplication
4. Scalar Division
7/27/2019 Apllications of Linked Lists
5/19
3 14 2 8 1 0a
8 14 -3 10 10 6
b
11 14
d
a->expon == b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14
d
a->expon < b->expon-3 10
Polynomial Addition
7/27/2019 Apllications of Linked Lists
6/19
3 14 2 8 1 0
a
8 14 -3 10 10 6
b11 14
a->expon > b->expon
-3 10
d
2 8
Polynomial Addition
7/27/2019 Apllications of Linked Lists
7/19
3 14 2 8
1 0
a
8 14 -3 10 10 6
b11 14
a->expon > b->expon
3 10
d
2 8
Polynomial Addition (contd)
10 6
1 0
7/27/2019 Apllications of Linked Lists
8/19
poly_pointer padd(poly_pointer a, poly_pointer b){
poly_pointer front, rear, temp;int sum;rear =(poly_pointer)malloc(sizeof(poly_node));if (IS_FULL(rear)) {
fprintf(stderr, The memory is full\n);exit(1);
}front = rear;
while (a && b) {switch (COMPARE(a->expon, b->expon)) {
Adding Polynomials (Pseudocode)
7/27/2019 Apllications of Linked Lists
9/19
case -1: /* a->expon < b->expon */attach(b->coef, b->expon, &rear);b= b->next;break;
case 0: /* a->expon == b->expon */sum = a->coef + b->coef;if (sum) attach(sum,a->expon,&rear);
a = a->next; b = b->next;break;case 1: /* a->expon > b->expon */
attach(a->coef, a->expon, &rear);a = a->next;
}
}for (; a; a = a->next)attach(a->coef, a->expon, &rear);
for (; b; b=b->next)attach(b->coef, b->expon, &rear);
rear->next = NULL;temp = front; front = front->next; free(temp);return front;
}
7/27/2019 Apllications of Linked Lists
10/19
3 142 8
1 0a
8 14 -3 10 10 6
b
-5 14
d
a->expon == b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
-5 14
d
a->expon < b->expon3 10
Polynomial Subtraction
7/27/2019 Apllications of Linked Lists
11/19
3 14 2 8 1 0
a
8 14 -3 10 10 6
b-5 14
a->expon > b->expon
3 10
d
2 8
Polynomial Subtraction (contd)
7/27/2019 Apllications of Linked Lists
12/19
3 14 2 8 1 0
a
8 14 -3 10 10 6
b-5 14
a->expon > b->expon
3 10
d
2 8
Polynomial Subtraction (contd)
-10 6
7/27/2019 Apllications of Linked Lists
13/19
3 14 2 8
1 0
a
8 14 -3 10 10 6
b-5 14
a->expon > b->expon
3 10
d
2 8
Polynomial Subtraction (contd)
-10 6
1 0
7/27/2019 Apllications of Linked Lists
14/19
poly_pointer padd(poly_pointer a, poly_pointer b){
poly_pointer front, rear, temp;int sum;rear =(poly_pointer)malloc(sizeof(poly_node));if (IS_FULL(rear)) {
fprintf(stderr, The memory is full\n);exit(1);
}front = rear;while (a && b) {
switch (COMPARE(a->expon, b->expon)) {
Polynomial Subtraction(Pseudo code)
7/27/2019 Apllications of Linked Lists
15/19
case -1: /* a->expon < b->expon */attach(-b->coef, b->expon, &rear);
b= b->next;break;
case 0: /* a->expon == b->expon */sum = a->coef - b->coef;if (sum) attach(sum,a->expon,&rear);
a = a->next; b = b->next;break;case 1: /* a->expon > b->expon */
attach(a->coef, a->expon, &rear);a = a->next;
}
}for (; a; a = a->next)attach(a->coef, a->expon, &rear);
for (; b; b=b->next)attach(-b->coef, b->expon, &rear);
rear->next = NULL;temp = front; front = front->next; free(temp);
return front;}
7/27/2019 Apllications of Linked Lists
16/19
Sparse MatrixA sparse matrix is a very large
matrix that contains lot of zeros. Inorder to save storage space, a
sparse matrix stores only thenonzero elements.
7/27/2019 Apllications of Linked Lists
17/19
Linked list representationSparse Matrix
m n -- i j val i j val Null
=
0830
0000
9072
0500
A
4 4 -- 0 2 5 3 2 8 Null
7/27/2019 Apllications of Linked Lists
18/19
Linked list representationSparse Matrix
aij
i j
ROWLINK
COLLINK
7/27/2019 Apllications of Linked Lists
19/19
Linked list representationSparse Matrix
=
0830
0000
9072
0500
A
0
1
2
3
5 2
2 0 7 1 9 3
3 1 8 2