Applications of linked list
• Used to implement stacks and queues.
• Used to implement the Adjacency list representation of graphs.
• Used to perform dynamic memory allocation.
• Used to maintain directory of names.
• Used to perform arithmetic operations on long integers.
• Used to manipulate polynomials to store constants.
• Used to represent sparse matrices.
Polynomials
• 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:
• Where the ai are nonzero coefficients and the ei are nonnegative
integer exponents such that
em-1 > em-2 > … > e1 > e0 0 .
≧
• We will represent each term as a node containing coefficient and
exponent fields, as well as a pointer to the next term.
0
1
0
1
)
( e
e
m x
a
x
a
x
A m





 

Polynomials
• Assuming that the coefficients are integers, the
type declarations are:
struct link{
int coeff;
int pow;
struct link *next;
};
• Draw poly_nodes as:
coef expon link
1
2
3 8
14


 x
x
a
b x x x
  
8 3 10
14 10 6
Polynomial Additions
• 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.
• Figure 4.12 generating the first three term of
d = a+b (next page)
Polynomials
Polynomial- Creation
void create(struct link *node)
{
char ch;
do
{
printf("n enter coeff:");
scanf("%d",&node->coeff);
printf("n enter power:");
scanf("%d",&node->pow);
node=(struct link*)malloc(sizeof(struct link));
node->next=NULL;
printf("n continue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
Polynomial- Display
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
Polynomial Add
void polyadd(struct link *poly1,struct link
*poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct
link));
poly=poly->next;
poly->next=NULL;
}
Contd…
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the
sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence
(next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following image.
In C, structure of a node in doubly linked list can be given as :
struct node
{
struct node *prev;
int data;
struct node *next;
}

Data structure and algorithm list structures

  • 1.
    Applications of linkedlist • Used to implement stacks and queues. • Used to implement the Adjacency list representation of graphs. • Used to perform dynamic memory allocation. • Used to maintain directory of names. • Used to perform arithmetic operations on long integers. • Used to manipulate polynomials to store constants. • Used to represent sparse matrices.
  • 2.
    Polynomials • Representing PolynomialsAs Singly Linked Lists • The manipulation of symbolic polynomials, has a classic example of list processing. • In general, we want to represent the polynomial: • Where the ai are nonzero coefficients and the ei are nonnegative integer exponents such that em-1 > em-2 > … > e1 > e0 0 . ≧ • We will represent each term as a node containing coefficient and exponent fields, as well as a pointer to the next term. 0 1 0 1 ) ( e e m x a x a x A m        
  • 3.
    Polynomials • Assuming thatthe coefficients are integers, the type declarations are: struct link{ int coeff; int pow; struct link *next; }; • Draw poly_nodes as: coef expon link 1 2 3 8 14    x x a b x x x    8 3 10 14 10 6
  • 4.
    Polynomial Additions • AddingPolynomials • 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. • Figure 4.12 generating the first three term of d = a+b (next page)
  • 5.
  • 6.
    Polynomial- Creation void create(structlink *node) { char ch; do { printf("n enter coeff:"); scanf("%d",&node->coeff); printf("n enter power:"); scanf("%d",&node->pow); node=(struct link*)malloc(sizeof(struct link)); node->next=NULL; printf("n continue(y/n):"); ch=getch(); } while(ch=='y' || ch=='Y'); }
  • 7.
    Polynomial- Display void show(structlink *node) { while(node->next!=NULL) { printf("%dx^%d",node->coeff,node->pow); node=node->next; if(node->next!=NULL) printf("+"); } }
  • 8.
    Polynomial Add void polyadd(structlink *poly1,struct link *poly2,struct link *poly) { while(poly1->next && poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } else if(poly1->pow<poly2->pow) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->pow=poly1->pow; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; }
  • 9.
  • 10.
    Doubly linked list Doublylinked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure. A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following image. In C, structure of a node in doubly linked list can be given as : struct node { struct node *prev; int data; struct node *next; }