Chapter - 5 Applications of Linked Listsds
Chapter - 5 Applications of Linked Listsds
Example:
int coeff;
int expo;
};
};
Creating and representing a polynomial using Single linked lists
Algorithm insertpoly(NODE * head)
{
NODE * head, newNode,current;
int coeff=c;
int expo=e;
1) if (head)
{
headcoeff=null;
headexpo=null;
headcount=0;
return(head);
}
2) Repeat steps while (currentnext!=null)
{
newNode = ((NODE *)malloc(sizeof(NODE));
Prepared by Dr.B.GOPI, Asst.Professor, KIST, KAKUTUR Page 2
newNodedata=num;
newNodecoeff=c;
newNodeexpo=e;
newNodenext=null;
current = currentnext;
// Displaying the nodes in the list
printf(“%d”, currentdata);
printf(“%d”, currentcoeff);
printf(“%d”, currentexpo);
}// End of insertpoly
POLYNOMIAL ADDITION
Input two polynomial expressions and compare the powers of both the expressions. If the powers are
equal then add the coefficients of the formal parameter x.
POLYNOMIAL MULTIPLICATION
Prepared by Dr.B.GOPI, Asst.Professor, KIST, KAKUTUR Page 5
Method:
1) In this approach we will multiply the 2nd polynomial with each term of 1st polynomial.
3) Then we will add the coefficients of elements having the same power in resultant polynomial.
Example:
Poly2: 6x^1 + 8
On multiplying each element of 1st polynomial with elements of 2nd polynomial, we get
A matrix in which number of zero entries are much higher than the number of non zero entries is
called sparse matrix. The natural method of representing matrices in memory as two-dimensional
arrays may not be suitable for sparse matrices. One may save space by storing for only non zero
entries.
Sparse Matrix Representations
A sparse matrix can be represented by using TWO representations, those are as follows...
1. Triplet Representation
2. Linked Representation
1. Triplet Representation
In this representation we consider only non-zero values along with their row and column index
values. In this representation the 0th row stores total rows, total columns and total non-zero values in
the matrix.
2. Linked Representation
In linked list representation, linked list data structure is used to represent a sparse matrix.
In linked list representation, each node consists of four fields whereas, in array representation, there
are three fields, i.e., row, column, and value.
Value: It is the value of the non-zero element which is located at the index (row, column).
#include <stdio.h>
int main()
int sparse_matrix[4][5] =
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 3 , 0 , 0 }
};
// size of matrix
int size = 0;
if(sparse_matrix[i][j]!=0)
size++;
int matrix[3][size];
int k=0;
{
Prepared by Dr.B.GOPI, Asst.Professor, KIST, KAKUTUR Page 10
for(int j=0; j<5; j++)
if(sparse_matrix[i][j]!=0)
matrix[0][k] = i;
matrix[1][k] = j;
matrix[2][k] = sparse_matrix[i][j];
k++;
printf("\t");
printf("\n");
return 0; }
int rows;
int columns;
int value;
};
int count;
NODE *head;
};
// This algorithm creates head structure for a linked list head node
// Local declarations
NODE *head;
3) If (head)
headàcount = 0;
6) Stop
1) Start
2) if (current == NULL)
current = createsparsematrix(r,c,val);
else
newnode à rows = r;
Prepared by Dr.B.GOPI, Asst.Professor, KIST, KAKUTUR Page 13
newnodeàcolumns = c;
newnodeàvalue = num;
current = currentànext;
printf(“%d”, currentàrows);
printf(“%d”, currentàcolumns);
printf(“%d”, currentàvalue);
count++;
4) Stop
1. In order to add two sparse matrices the lists are traversed until the end of one of the lists is reached.
2. In the process of traversal, the row indices stored in the nodes of these lists are compared. If they
don't match, a new node is created and inserted into the resultant list.
3. If the row indices match, column indices for the corresponding row positions are compared. If they
don't match, a new node is created and inserted into the resultant list.
4. If the column indices match, a new node is created and inserted into the resultant list by copying
the row and column indices from any of the nodes and the value equal to the sum of the values in the
two nodes.
5. After this, the pointers in both the lists are advanced to make them point to the next nodes in the
respective lists. This process is repeated in each iteration. After reaching the end of any one of the
lists, the iterations come to an end and the remaining nodes in the list whose end has not been reached
are copied, as it is in the resultant list.
Prepared by Dr.B.GOPI, Asst.Professor, KIST, KAKUTUR Page 14
Example
If the procedures add is applied to the above linked list representations then we get the resultant list,
as shown in Figure below.
1) Start
3) if(t1àrow== t2àrow)
tempàrow = t1àrow;
tempàcolumns = t1àcolumns;
tempànext = NULL;
t1 = t1ànext;
t2 = t2ànext;
tempàrow = t1àrow;
tempàcolumns = t1àcolumns;
tempàvalue= t1àvalue;
re=insertSparseMatrix(temp);
t1 = t1ànext;
else
5) tempàrow = t2àrow;
tempàcolumns = t2àcolumns;
tempàvalue= t2àvalue;
re=insertSparseMatrix(temp);
t2 = t2ànext;
else
6) if(t1àrows< t2àrows)
tempàcolumns = t1àcolumns;
tempàvalue= t1àvalue;
re=insertSparseMatrix(temp);
t1 = t1ànext;
else
7) tempàrow = t1àrow;
tempàcolumns = t1àcolumns;
tempàvalue= t1àvalue;
re=insertSparseMatrix(temp);
tempàrow = t1àrow;
tempàcolumns = t1àcolumns;
tempàvalue= t1àvalue;
t1 = t1ànext;
tempàrow = t2àrow;
tempàvalue= t2àvalue;
t2 = t2ànext;
10) Stop
A complex number contains real and imaginary part. The structure defined for a complex number can
be
float real;
float imaginary;
};
To gain access to individual structure members, a structure membership operator must be used.
Example:
complexnum.real = 3.2;
To access the address of structure variables then the structure membership operator used will be
(&complexnum)àreal = 3.2;