Chapter 5
Chapter 5
Learning Outcomes:
Students will be able to understand linked list application.
Students will have a clarity operation on linked list.
Students will be able to learn generalized linked list.
Structure
5.1. Operations on Linked List
Knowledge Check 1
Outcome Based Activity
5.2. Applications of Linked List
5.3. Generalized linked list
Knowledge Check 2
Outcome Based Activity
5.4. Summary
5.5. Self-Assessment Questions
5.6. References
5.1. Operations on Linked List
The various operation that can be performed on linked list are given below.
1. Create: create a list of a node.
2. Traverse: Visit each node of the list.
3. Length: Count the total number of node in the list.
4. Insert: A node can be inserted at the beginning, end or in between two node of the
list.
5. Delete: Deletion from the list may be done either position-wise or element-wise.
6. Search: This process searches for a specific elements in the list.
7. Reverse: This process reverse the order of node in the list.
8. Concatenation: This operation appends the node of the second list at the end of the
first list i.e. it join two list.
9. Merge: This operation merge two sorted linked list into the third list such that the
third list also sorted order.
The above operation on the list an extra node at the beginning allows operation to be
performed more efficiently. This is called as header node.
Header Node
A header node is an extra node that is added at the beginning of the list. This node
does not store any data element but can be used to store some control information like the
number of element etc.
The use of header node simplifies list operation.
Creating node:
All linked list operation are performed using a pointer. The syntax to declared a pointer to
a node is.
Syntax:
struct node *pointer;
Example:
struct node *head, *temp, *newnode;
To create a node dynamically during run-time we use functions like malloc(), calloc(). To
free the node, use function free().
Singly Linked list
tmp=malloc(sizeof(struct node) );
tmp->addr_next=q->addr_next;
tmp->data=num;
q->addr_next=tmp;
}
Let the node to be inserted is temp as shown in below figure 4.8 (d) after the 2nd node in the
list. This means there should be some form of counter to count the number of nodes so as to
reach at the desired position
Node to be Appended
Temp points to the node 30 that is to be appended. This is shown in below Figure
Linked list after appending a node
void insert_middle(int num,int mid)
{
int i;
q=start;
for(i=0;i<mid-2;i++)
{
q=q->addr_next;
if(q==NULL)
{
printf("Invalid Position");
return;
}
}
tmp=malloc(sizeof(struct node) );
tmp->addr_next=q->addr_next;
tmp->data=num;
q->addr_next=tmp;
}
Now, advance front pointer to point to the next node so that after deleting the first node, the
node that the front points now will be the first node of the list. This is illustrated in below
Figure
Use a variable count to store the count of the number of node traversed and a pointer ‘delptr’.
When count less than or equal to the position (third), the delptr-> next should point where the
next pointer of the node to be deleted is pointing. Let ‘nodeptr’ be the pointer pointing to this
node as shown in Figure
while(f!=NULL)
{
if(f->data==x)
{
t=f;
f=f->next;
free(t);
}
else
{
break;
}
}
if(f==NULL)
return f;
for(s=f;s!=NULL&&s->next!=NULL;s=s->next)
{
if(s->next->data==x)
{
t=s->next;
s->next=t->next;
free(t);
}
}
return f;
}
struct node * search(struct node *f,char x)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
if(s->data==x)
return s;
}
return NULL;
}
int main()
{
struct node *head=NULL;
int choice,pos,n;
char x;
while(1)
{
printf("\nMENU\n\n1:DISPLAY SINGLY LINKED LIST\n2:INSERTING
NODE AT GIVEN POSITION OF LINKED LIST\n3:DELETING NODE
FROM GIVEN POSITION OF THE LINKED LIST\n4:SEARCH\n5:EXIT");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nLINKED LIST = ");
display(head);
break;
case 2: printf("\nENTER POSITION FOR INSERTION : ");
scanf("%d",&pos);
printf("\nENTER THE CHARACTER : ");
scanf("%c",&x);
head=insert(head,x,pos);
break;
case 3: printf("\nENTER CHARACTER FOR DELETION : ");
scanf("%c",&x);
head=delete(head,x);
break;
case 4: printf("\nENTER DATA FOR SEARCH : ");
scanf("%c",&x);
if(search(head,x)==NULL)
{
printf("Given character %c is not Found....\n",x);
}
else
{
printf("Given Character %c is Found....\n",x);
}
break;
case 5: exit(0);
default:printf("\nINVALID CHOICE,Please RE-ENTER AGAIN");
}//switch
}//while
}//main
Output:
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 2
ENTER POSITION FOR INSERTION : 1
ENTER THE CHARACTER : 3
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 1
LINKED LIST = 3
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 3
ENTER CHARACTER FOR DELETION : 1
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 4
ENTER DATA FOR SEARCH : 5
Given character 5 is not Found....
Advantages:
1. Traversal in both direction is possible.
2. Searching an element is faster.
3. Operation like insertion and deletion can be performed easily.
Disadvantages:
1. Extra storage is needed for the pointer.
a) Creating a Doubly Linked List:
The node of a doubly linked list is a structure with fields; data which stores the value of
the node, *prev which stores the address of the previous node and *next stores the address of
the next node. Two nodes *front which always points to the first node of the linked list and
*rear which is used to point to the last node of the linked list are initialized. Initially temp-
>prev = NULL and temp->next = NULL.
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
tmp->addr_prev=NULL;//assign null for previous address as it the first
element
start->addr_prev=tmp;
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
tmp->addr_prev=q;
}
}
newnode->next=dlist;
dlist->prev=newnode;
dlist=newnode;
doubly.c
#include<stdio.h>
#include "doublylist.h"
int main()
{
struct node *head=NULL;
int choice,pos,n,x;
while(1)
{
// system("clear");// this is standered liabrary function which clears the screen.
printf("\nMENU\n\n1:DISPLAY SINGLY LINKED LIST\n2:INSERTING NODE AT
GIVEN POSITION OF LINKED LIST\n3:DELETING NODE FROM GIVEN POSITION
OF THE LINKED LIST\n4:SEARCH\n5:EXIT");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nLINKED LIST = ");
display(head);
break;
case 2: printf("\nENTER POSITION FOR INSERTION : ");
scanf("%d",&pos);
printf("\nENTER THE DATA : ");
scanf("%d",&x);
head=insert(head,x,pos);
break;
case 3: printf("\nENTER DATA FOR DELETION : ");
scanf("%d",&x);
head=delete(head,x);
break;
case 4: printf("\nENTER DATA FOR SEARCH : ");
scanf("%d",&x);
if(search(head,x)==NULL)
{
printf("Given Number %d is not Found....\n",x);
}
else
{
printf("Given Number %d is Found....\n",x);
}
break;
case 5: exit(0);
default:printf("\nINVALID CHOICE,Please RE-ENTER AGAIN");
}//switch
}//while
}//main
doublylist.h
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define NEWNODE (struct node *)malloc(sizeof(struct node))
struct node
{
struct node *prev;
int data;
struct node *next;
};
void display(struct node *f)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf(" %d ",s->data);
}
}
struct node * insert(struct node *f,int x,int pos)
{
struct node* s,*t;
int i;
t=NEWNODE;
t->data=x;
t->next=NULL;
t->prev=NULL;
if(pos==1)
{
t->next=f;
if(f!=NULL)
f->prev=t;
f=t;
return f;
}
else
{
s=f;
for(i=1;i<=pos-2 && s!=NULL;i++)
{
s=s->next;
}
if(s==NULL)
{
printf("\nERROR : INVALID POSITION");
return f;
}
t->next=s->next;
if(s->next!=NULL)
s->next->prev=t;
t->prev=s;
s->next=t;
return f;
}
}
struct node * delete(struct node *f,int x)
{
struct node *s,*t;
int i;
if(f==NULL)
{
printf("Linked List is Empty....!!!\n");
return f;
}
while(f!=NULL)
{
if(f->data==x)
{
t=f;
f=f->next;
free(t);
}
else
{
break;
}
}
if(f==NULL)
return f;
f->prev=NULL;
for(s=f;s!=NULL && s->next!=NULL;s=s->next)
{
if(s->next->data==x)
{
t=s->next;
s->next=t->next;
free(t);
}
}
return f;
}
struct node * search(struct node *f,int x)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
if(s->data==x)
return s;
}
return NULL;
}
Output:
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 2
ENTER POSITION FOR INSERTION : 1
ENTER THE DATA : 23
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 3
ENTER DATA FOR DELETION : 2
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 4
ENTER DATA FOR SEARCH : 23
Given Number 23 is Found....
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 1
LINKED LIST = 23
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 3
ENTER DATA FOR DELETION : 1
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 5
The two pointers front and rear can be used to associate with the first and last node
respectively.
For convenient traversal of the list and to simplify the insertion and deletion of nodes, a
header node can be placed at the front of the list.
Knowledge Check 1
1. In linked list each node contains a minimum of two fields. One field is data field to
store the data second field is…………
2. Differentiates a circular linked list from a normal linked list…………
3. ………….. list does insert operation at end take O(1) time.
4. ………. Linked list the last node points to the first.
Outcome Based Activity
1. Write a program to count the number of nodes in a circular doubly linked list.
if(p!=NULL)
{
while(p!=NULL)
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=p->pow;
s->coeff=p->coeff;
p=p->next;
f=f->next;
}
}
if(q!=NULL)
{
while(q!=NULL)
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=q->pow;
s->coeff=q->coeff;
q=q->next;
f=f->next;
}
}
newnode=newnode->next;
return(newnode);
int main()
{
int n;
printf("\nEnter no in poly one:");
scanf("%d",&n);
head=create(n);
display(head);
printf("\nEnter no in poly two:");
scanf("%d",&n);
head1=create(n);
display(head1);
printf("Addition of two polylomial is");
h=addpoly(head,head1);
display(h);
return 0;
}
Output:
Enter no in poly one:2
Enter the coeff & power:2 2
Enter the coeff & power:3 1
2x^2+3x^1
Enter no in poly two:3
Enter the coeff & power:1 2
Enter the coeff & power:3 1
Enter the coeff & power:5 0
1x^2+3x^1+5x^0
Addition of two polynomial is:
3x^2+6x^1+5x^0
To represent a list of items there are certain assumptions about the node structure.
Flag = 1 implies that down pointer exists
Flag = 0 implies that next pointer exists
Data means the atom
Down pointer is the address of node which is down of the current node
Next pointer is the address of node which is attached as the next node
Why Generalized Linked List?
Generalized linked lists are used because although the efficiency of polynomial
operations using linked list is good but still, the disadvantage is that the linked list is unable
to use multiple variable polynomial equation efficiently. It helps us to represent multi-
variable polynomial along with the list of elements.
Typical ‘C’ structure of Generalized Linked List
In the above example the head node is of variable x. The temp node shows the first
field as 2 means coefficient and exponent are present.
Knowledge Check 2
1. The ……….helps us to represent multi-variable polynomial along with the list of
elements.
2. Generalized linked lists are used ……………
3. Polynomial is a one variable can be represented as…………….
4. Disadvantage is that the linked list is…………..
Outcome Based Activity
1.
5.4. Summary
A header node is an extra node that is added at the beginning of the list. This node
does not store any data element but can be used to store some control information
like the number of element etc.
All linked list operation are performed using a pointer.
To create a node dynamically during run-time we use functions like malloc(),
calloc(). To free the node, use function free().
A doubly linked list is a linked list in which each node contains two links- one
pointing to the previous node and one pointing to the next node.
A circular list is one in which the next field of the last node points to the first node
of the list or the next field of the last node contains the address of the first node of
the list.
Polynomial is a one variable can be represented as a linked list where each node
of the list represent one term of the polynomial.
A Generalized Linked List L, is defined as a finite sequence of n>=0 elements, l1,
l2, l3, l4,…, ln, such that li are either atom or the list of atoms. Thus L = (l1, l2, l3,
l4, …, ln) where n is total number of nodes in the list.