Unit 2
Unit 2
2
➢In array, elements are stored in consecutive memory locations.
➢To occupy the adjacent space, block of memory that is required for the
array should be allocated before hand.
3
What is Linked List?
➢ A linked list is a linear collection of homogeneous data elements,
called nodes, where linear order is maintained by means of links
or pointers.
4
node
link
data
Start Nul
l
A B C D
5
Linked Lists
➢ Linked list
– Linear collection of self-referential structures, called nodes,
connected by pointer links.
– Link pointer in the last node is set to null to mark the end of list.
6
Types of linked lists
– Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
7
Dynamic Memory Allocation
8
Self-Referential Structures
• Self-referential structures
– Structure that contains a pointer to a structure of the same
type
– Can be linked together to form useful data structures such as
lists, queues, stacks and trees
– Terminated with a NULL pointer (0)
• Two self-referential structure objects linked together
Start Nul
l
A B C D
9
Singly linked list operations
Insertion:
• Insertion of a node at the front
• Insertion of a node at any position in the list
• Insertion of a node at the end
Deletion:
• Deletion at front
• Deletion at any position
• Deletion at end
Display:
• Displaying/Traversing the elements of a list
10
Singly linked lists
Node Structure
struct node
2000
{
int data; new
struct node *link;
}*new, *ptr, *header, *ptr1;
data link
10 NULL
Creating a node
11
Inserting a node at the beginning
Create a node that is to be inserted
2500
new
data link Algorithm:
1.Create a new node.
10 NULL 2.if (header = = NULL)
2500 3. new -> link = NULL;
4. header = new;
5.else
If the list is empty 6.{
7. new -> link = header;
NUL
8. header = new;
L
header header 9.}
2500 1200
header
new
5 1300 5 1330 4 1400 8 NULL
10 NULL 1200 1300 1330 1400
12 2500
header Inserting a node at the end
1500
1400
1500 1800 1200
ptr 50 NULL
1400
1800
1500
1200
2000
Algorithm:
new
1. new=malloc(sizeof(struct node));
2. ptr = header; 2000
3. while(ptr -> link!= NULL)
4. ptr = ptr -> link;
5. new -> link = NULL;
13 6. ptr -> link = new;
Inserting a node at the given position
header ptr
1500 1800
1500 Insert position : 3
10 1800 20 1200
2000 30 1400 40 NULL
1400
1500 1800 1200
50 NULL
1200
new Algorithm:
1. new=malloc(sizeof(struct node));
2000
2000 2. ptr and = header;
3. for(i=1;i < pos-1;i++)
4. ptr = ptr -> link;
5. new -> link = ptr -> link;
6. ptr -> link = new;
14
Deleting a node at the beginning
header ptr
1500
1800 1500
Algorithm:
1. if (header = = NULL)
2. print “List is Empty”;
3. else
4. {
5. ptr = header;
6. header = header -> link;
7. free(ptr);
15 8. }
header
Deleting a node at the end
ptr1 ptr1 ptr1
1500
1400
1500 1800 1200
ptr
1800
1200
1400
1500
Algorithm:
1. ptr = header;
2. while(ptr -> link != NULL)
3. ptr1=ptr;
4. ptr = ptr -> link;
5. ptr1 -> link = NULL;
16 6. free(ptr);
Deleting a node at the given position
ptr1
header ptr
1200
1500 1800
1500 Insert position : 3
10 1800 20 1400
1200 30 1400 40 NULL
1400
1500 1800 1200
Algorithm:
1. ptr = header ;
2. for(i=1;i<pos-1;i++)
3. ptr = ptr -> link;
4. ptr1 = ptr -> link;
5. ptr -> link = ptr1-> link;
17 6. free(ptr1);
Traversing an elements of a list
header ptr
1500
1500
Algorithm:
1. if(header = = NULL)
2. print “List is empty”;
3. else
4. for (ptr = header ; ptr != NULL ; ptr = ptr -> link)
5. print “ptr->data”;
18
/*Program to implement single linked list*/
#include<stdio.h> #include<malloc.h> #include<conio.h> #include<stdlib.h>
void traverse(); void deletion(); void insertion();
int choice,i,pos,item;
struct node {
int data;
struct node *link;
}*header,*ptr,*ptr1,*new;
void main() {
header=NULL;
ptr=header;
printf("****Menu****\n");
printf("\n 1.Insertion\n 2.Deletion\n 3.Traverse\n 4.Search\n 5.Exit\n");
while(1) {
printf("\nenter ur choice");
scanf("%d",&choice);
switch(choice) {
case 1: insertion(); break;
case 2: deletion(); break;
case 3: traverse(); break;
case 4: exit();
default: printf("\nwrong choice\n");
}/*end of switch*/
}/*end of while*/
}/*end of main*/
19
void insertion() {
new=malloc(sizeof(struct node));
printf("\n enter the item to be inserted\n");
scanf("%d",&item);
new->data=item;
if(header = = NULL)
{
new->link=NULL;
header=new;
}/*end of if*/
else
{
printf("\nEnter the place to insert the
item\n");
printf("1.Start\n 2.Middle\n 3.End\n");
scanf("%d",&choice);
if(choice = = 1)
{
new->link=header;
header=new;
20
}
if(choice = = 2)
{
ptr=header;
printf("Enter the position to place an item: ");
scanf("%d",&pos);
for(i=1;i<pos-1;i++)
ptr=ptr->link;
new->link=ptr->link;
ptr->link=new;
}
if(choice = = 3)
{
ptr=header;
while(ptr->link!=NULL)
ptr=ptr->link;
new->link=NULL;
ptr->link=new;
}
}/*end of else*/
}/*end of insertion*/
21
void deletion()
{
ptr=header;
if(header = = NULL)
{
printf("\nThe list is empty");
}
else
{
printf("\n1.Start \n2.Middle \n3.End");
printf("\nEnter the place to delete the element from list");
scanf("%d",&choice);
if(choice = = 1)
{
printf("\nThe deleted item from the list is ->
%d",ptr->data);
header=header->link;
}
22
if(choice = = 2)
{
printf("\nEnter the position to delete the element from the
list");
scanf("%d",&pos);
for(i=0;i<pos-1;i++)
{
ptr1=ptr;
ptr=ptr->link;
}
printf("\nThe deleted element is ->%d",ptr->data);
ptr1->link=ptr->link;
}
if(choice = = 3)
{
while(ptr->link!=NULL) {
ptr1=ptr;
ptr=ptr->link;
}//while
printf("\nThe deleted element from the list is ->%d",
ptr->data);
ptr1->link=NULL;
23 }
}/*end of else*/
void traverse()
{
if(header = = NULL)
printf("List is empty\n");
else
{
printf("\nThe elements in the list are");
for(ptr=header;ptr!=NULL;ptr=ptr->link)
printf("\n\tNode at %d is %d",++i,ptr->data);
}
}/*end of traverse*/
24
Representing Stack with Linked List
25
Example:
➢The following list consists of five cells, each of which holds a data object
and a link to another cell.
➢A variable, top, holds the address of the first cell in the list.
50 1500 1100
40 1800 1500
30 1200 1800
20 1400 1200
26
/* Write a c program to implement stack using linked list */
#include<stdio.h> #include<conio.h> #include<malloc.h>
#include<stdlib.h>
int push(); int pop(); int display(); int choice,i,item;
struct node {
int data;
struct node *link;
}*top,*new,*ptr;
main() { top=NULL;
printf("\n***Select Menu***\n");
while(1) {
printf("\n1.Push \n2.Pop \n3.Display \n4.Exit\n5.Count");
printf("\n\nEnter ur choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
case 5: count(); break;
default: printf("\nWrong choice");
}/* end of switch */
27 }/* end of while */
}/* end of main */
int push() int pop()
{ {
new=malloc(sizeof(struct node)); if(top = = NULL)
printf("\nEnter the item: "); {
scanf("%d",&item); printf("\n\nStack is empty");
new->data=item; return;
if(top = = NULL) }//if
{ else
new->link=NULL; {
} printf("\n\nThe deleted element
else is: %d",top->data);
{
new->link=top; top=top->link;
} }
top=new; return;
return; }/* end of pop() */
}/* end of insertion */
28
int display() int count()
{ {
ptr=top; int count=1;
if(top = = NULL) ptr=top;
{ if(top = = NULL)
printf("\nThe list is empty"); {
return; printf("\nThe list is empty");
} return;
printf("\nThe elements in the stact are: }
"); while(ptr->link!=NULL)
while(ptr!=NULL) {
{ ++count;
printf("\n ptr=ptr->link;
%d",ptr->data); }
ptr=ptr->link; printf("\n\nThe number of elements in
}/* end of while */ the stack are: %d",count);
return; return;
}/* end of display() */ }/* end of count */
29
Representing Queue with Linked List
➢ New items are added to the end of the list.
➢ Removing an item from the queue will be done from the front.
front rear
➢ The variables front points to the front item in the queue and rear
points to the last item in the queue.
30
/*Write a c program to implement queue using linked list*/
#include<stdio.h> #include<conio.h> #include<malloc.h>
#include<stdlib.h> int choice,i,item;
struct node {
int data;
struct node *link;
}*front,*rear,*new,*ptr;
main() {
front=NULL;
rear=NULL;
printf("\n\n MENU");
printf("\n1.Enqueue \n2.Dequeue \n3.Display \n4.Exit");
while(1) {
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1:enqueue(); break;
case 2:dequeue(); break;
case 3:display(); break;
case 4:exit(0);
default:printf("\nwrong choice");
}/*end of switch */
31 }/*end of while */
}/*end of main */
int enqueue()
{
new=malloc(sizeof(struct node));
printf("\nenter the item"); display()
scanf("%d",&item); {
new->data=item; if(front==NULL)
new->link=NULL; printf("\nThe list is emtpy");
if(front==NULL) else
{ {
front=new; for(ptr=front;ptr!=NULL;ptr=ptr->link)
} printf(" %d",ptr->data);
else }
{ return;
rear->link=new; }/* end of display */
}
rear=new;
return;
}/*end of enqueue */
32
dequeue()
{
if(front==NULL)
printf("\nThe list is empty");
else
if(front==rear) /*list has single element*/
{
printf("\nThe deleted element is: %d",front->data);
front=rear=NULL;
}
else
{
printf("\nThe deleted element is: %d",front->data);
front=front->link;
}
return;
}/*end ofdequeue*/
33
Doubly linked list
➢ In a singly linked list one can move from the header node to any node
in one direction only (left-right).
➢ A doubly linked list is a two-way list because one can move in either
direction. That is, either from left to right or from right to left.
➢ Where, DATA field - stores the element or data, PREV- contains the
address of its previous node, NEXT- contains the address of its next
node.
34
Doubly linked list operations
Insertion:
• Insertion of a node at the front
• Insertion of a node at any position in the list
• Insertion of a node at the end
Deletion:
• Deletion at front
• Deletion at any position
• Deletion at end
Display:
• Displaying/Traversing the elements of a list
35
header ptr
1010 1010
Before inserting a node at the beginning
NULL 50 NULL
2200 new
NULL 50 NULL
After inserting a node new
2200
at end of a list ptr 2000
new
NULL 10 2020 1010 20 1000 2020 30 2000 1000 40 2200
1010 2020 1000 2000
1. Create a new node
Algorithm:
2. Read the item
3. new->data=item
4. ptr= header
5. while(ptr->next!=NULL)
1. ptr=ptr->next; 2000 50 NULL
6. new->next=NULL;
2200
7. new->prev=ptr;
new
8. ptr->next=new;
37
Insertion of a node at any position in the list
Algorithm: 1. create a node new
2. read item
3. new->data=item
4. ptr=header;
5. Read the position where the element is to be
inserted
6. for(i=1;i<pos-1;i++)
6.1 ptr=ptr->next;
7. if(ptr->next = = NULL)
7.1 new->next = NULL;
7.2 new->prev=ptr;
7.3 ptr->next=new;
8. else
8.1 ptr1=ptr->next;
8.2 new->next=ptr1;
8.3 ptr1->prev=new;
8.4 new->prev=ptr;
8.5 ptr->next=new;
9. end
38
header
1010 1010
ptr ptr Before inserting a node at position 3
NULL 50 NULL
2200 new
header 2020
ptr ptr 1000
ptr ptr1
1010
2020 50 1000
39 2200 new
header
1010 Before deleting a node at beginning
Algorithm:
1.ptr=header
2.ptr1=ptr->next;
3.header=ptr1;
4.if(ptr1!=NULL)
1.ptr1->prev=NULL;
40 5. free(ptr);
header
1010 Before deleting a node at end
pt1 ptr
header
1000 2000
1010 After deleting a node at end
Algorithm:
1. ptr=header
2. while(ptr->next!=NULL)
1. ptr=ptr->next;
3. end while
4. ptr1=ptr->prev;
5. ptr1->next=NULL;
41
Deletion at any position
4. if(ptr = = header)
//if the deleted item is first
Algorithm: node
1. ptr=header; 4.1 ptr1=ptr->next;
2. while(ptr->next!=NULL) 4.2 ptr1->prev=NULL;
1.for(i=0;i<pos-1;i++) 4.3 header=ptr1;
1. 4.4 end if
ptr=ptr->next; 5.else
2.if(i = = pos-1) 5.1 ptr1=ptr->prev;
1. break; 5.2 ptr2=ptr->next;
3. end while 5.3 ptr1->next=ptr2;
5.4 ptr2->prev=ptr1;
6. end else
7. end if
42
header Before deleting a node at position 3
1010 1010
ptr ptr
43
Displaying elements of a list
Algorithm:
1. ptr=header;
2. if(header = = NULL)
1. printf("The list is empty\n");
3. else
1. print “The elements in farword order: “
2. while(ptr!=NULL)
1. print “ptr->data”;
2. if(ptr->next = = NULL)
1. break;
3. ptr=ptr->next;
3. print “The elements in reverse order: “
4. while(ptr!=header)
1. if(ptr->next = = NULL)
1. print “ptr->data”;
2. else
– print “ptr->data”;
end else
1. ptr=ptr->prev;
end while
44 4. print “ptr->data”;
header ptr
1010 1010
Forward Order : 10 20 30 40
Reverse Order : 40 30 20 10
45
/*Program to implement operations of double linked list*/
#include<stdio.h> #include<conio.h> #include<malloc.h>
void insertion(); void deletion(); void traverse(); int i,pos,item,choice;
struct node {
int data;
struct node *next;
struct node *prev;
}*new,*header,*ptr,*ptr1,*ptr2;
void main() {
header=NULL;
printf(" ***** MENU ****");
printf("\n1.Insertion \n2.Deletion \n3.Traverse \n4.Exit\n");
while(1) {
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: insertion(); break;
case 2: deletion(); break;
case 3: traverse(); break;
case 4: exit();
default: printf("\nWrong choice");
}/* end of switch */
46 }/* end of while */
}/* end of main */
void insertion() {
ptr=header;
new=malloc(sizeof(struct node));
printf("\nEnter the item to be inserted: ");
scanf("%d",&item);
new->data=item;
if(header==NULL) {
new->prev=NULL;
new->next=NULL;
header=new;
}
else {
printf("\nSelect the place:");
printf("\n1.Start \n2.Middle \n3.End\n");
scanf("%d",&choice);
if(choice==1) {
new->next=ptr;
ptr->prev=new;
new->prev=NULL;
header=new;
}/* choice1 */
47
if(choice==2)
{
printf("\nEnter the position to place the new element: ");
scanf("%d",&pos);
for(i=1;i<pos-1;i++)
ptr=ptr->next;
if(ptr->next==NULL) if(choice==3)
{ {
new->next=NULL; while(ptr->next!=NULL)
new->prev=ptr; ptr=ptr->next;
ptr->next=new; new->next=NULL;
} new->prev=ptr;
else ptr->next=new;
{ }
ptr1=ptr->next; }/* end of else */
new->next=ptr1; }/* end of insertion */
ptr1->prev=new;
new->prev=ptr;
ptr->next=new;
}
}/* choice2 */
48
void deletion()
{
ptr=header;
if(header==NULL)
printf("The list is empty\n");
else
{
printf("\Select the place:");
printf("\n1.Start \n2.Middle \n3.End\n");
scanf("%d",&choice);
if(choice==1)
{
printf("\nThe deleted item is: %d",ptr->data);
ptr1=ptr->next;
header=ptr1;
if(ptr1!=NULL)
ptr1->prev=NULL;
}/* choice1 */
49
if(choice==2) {
printf("\nEnter the position to delete the element:
");
scanf("%d",&pos);
while(ptr->next!=NULL) {
for(i=0;i<pos-1;i++)
ptr=ptr->next;
if(i==pos-1)
break;
}//while
printf("\n\nThe deleted node is:
%d",ptr->data);
if(ptr==header)//deleted item is starting node
if(choice==3)
{ {
ptr1=ptr->next; while(ptr->next!=NULL)
ptr1->prev=NULL; ptr=ptr->next;
header=ptr1; printf("\n\nThe deleted node is: %d",ptr->data);
}//if ptr1=ptr->prev;
else { ptr1->next=NULL;
ptr1=ptr->prev; }/* choice3 */
ptr2=ptr->next; }/*end of deletion */
ptr1->next=ptr2;
50 ptr2->prev=ptr1;
}
void traverse(){
ptr=header;
if(header==NULL)
printf("The list is empty\n");
else {
printf("\n\nThe elements in farword order: ");
while(ptr!=NULL){
printf(" %d",ptr->data);
if(ptr->next==NULL) {
break;
}
ptr=ptr->next;
}/* end of while */
printf("\n\nThe elements in reverse order: ");
while(ptr!=header) {
if(ptr->next==NULL)
printf(" %d",ptr->data);
else
printf(" %d",ptr->data);
ptr=ptr->prev;
}/* end of while */
printf(" %d",ptr->data);
51 }/* end of else */
}/* end of traverse() */
Circular linked list
➢ The linked list where the last node points the header node is called
circular linked list.
52
/* Write a c program to implement circular linked list*/
#include<stdio.h> #include<conio.h> #include<malloc.h>
#include<stdlib.h>
int choice,i,item;
struct node {
int data;
struct node *link;
}*front,*rear,*new,*ptr1,*ptr;
main() {
front=rear=NULL;
printf("\n select menu\n");
while(1) {
printf("\n1.Enqueue \n2.Dequeue \n3.Display \n4.Exit");
printf("\nEnter ur choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong choice.");
}/*end of switch*/
53 }/*end of while*/
}/*end of main*/
int enqueue()
{
new=malloc(sizeof(struct node));
printf("\nEnter the item: ");
scanf("%d",&item);
new->data=item; dequeue()
if(front==NULL) {
front=new; if(front==NULL)
else printf("\nThe circular list is empty.");
rear->link=new; else
rear=new; if(front==rear)
rear->link=front; {
return; printf("\nThe deleted element is: %d",front->data);
}/*end of enqueue()*/ front=rear=NULL;
}
else
{
printf("\nThe deleted element is: %d",front->data);
front=front->link;
rear->link=front;
}
return;
54 }/*end of dequeue*/
display()
{
ptr=front;
ptr1=NULL;
if(front==NULL)
printf("\nThe circular list is empty.");
else
{
printf("\nElements in the list are: ");
while(ptr!=ptr1)
{
printf(" %d",ptr->data);
ptr=ptr->link;
ptr1=front;
}/*end of while*/
return;
}/*end of else*/
}/*end of display*/
55