0% found this document useful (0 votes)
2 views

3.3Queue Using Linked List

The document explains how to implement a queue using a linked list, detailing the operations of insertion, deletion, and display. It provides step-by-step instructions and code snippets for each operation, including handling cases for empty queues. The insertion process involves adding nodes at the rear, while deletion removes nodes from the front, and the display function iterates through the queue to show its contents.

Uploaded by

karanam bharggav
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3.3Queue Using Linked List

The document explains how to implement a queue using a linked list, detailing the operations of insertion, deletion, and display. It provides step-by-step instructions and code snippets for each operation, including handling cases for empty queues. The insertion process involves adding nodes at the rear, while deletion removes nodes from the front, and the display function iterates through the queue to show its contents.

Uploaded by

karanam bharggav
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

QUEUE USING LINKED LIST

Queue Representation
A Queue can be represented using linked list
which is the dynamic representation of queue.
 Operations on Queue using Linked list

 Insert (Insertion at rear-end)

 Deletion (Deletion at front-end)

 Display
Insertion
Insertion
1. create a new node ( say temp) containing
the information to be inserted.
2. node temp;
3. temp->info=data;
4. temp->next=NULL;
Insertion
5. If the queue is empty i.e. head = NULL then
make the new node as the head node in the
list
Insertion
5. If the queue is empty i.e. head = NULL then
make the new node as the head node in the
queue
6. head=temp; head
Insertion
7. If the queue is not empty then
 -create a temporary pointer (say curn) and

store the address of the head node and move


to the end of the list.

 -After reaching the last node store the address


of the new node in the next part of the last
node
Insertion
 slist curn =head;
Insertion
While (curn->next!=NULL)
Curn=curn->next;

curn-> next is 200 which is not equal to NULL so curn moves to next node
Insertion
While (curn->next!=NULL)
Curn=curn->next;

curn-> next is 300 which is not equal to NULL so curn moves to next node
Insertion
While (curn->next!=NULL)
Curn=curn->next;

curn-> next is NULL which is not equal to NULL so we stop here


Insertion
 Curn->next=temp;
CODE
void Enqueue()
{ int data;
printf("\nEnter value\n"); else
{
scanf("%d",&info); curn = head;
temp->info = data; while (curn -> next != NULL)
{
temp->next=NULL; curn = curn -> next;
if(head == NULL) }
curn->next = temp;
{
head=temp; printf("\nNode inserted");
}
}
}
}
Deletion
Deletion
void dequeue(slist *head)
{
if(head==NULL) //CASE-1
printf(“Queue IS EMPTY");
else //CASE 2 & 3
{
printf("\nDeleted element is %d",head->info) ;
head=head->next ;
}
}
Deletion
CASE-1
1. If the Queue is empty i.e. head=NULL just
display a message that queue is empty.

Print Queue empty


Deletion
CASE-2 & 3
1. Print deleted node is head->info;

2. Head=head->next
Display
Display
void display(slist *head)
{
slist * curn;
if(head==NULL) // CASE-1
printf(“Queue IS EMPTY");
else // CASE-2
{
curn=head;
while(curn !=NULL)
{
Printf(“\t%d”,curn->info );
curn =curn->next ;
}
}
}
Display
CASE-1
1. if(head==NULL)

printf(“Queue is empty”);
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 100 its not equal to NULL, so curn->info that’s 10 is displayed and
curn moves to next node

10
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 200 its not equal to NULL, so curn->info that’s 20 is displayed and
curn moves to next node

10 20
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 300 its not equal to NULL, so curn->info that’s 30 is displayed and
curn moves to next node

10 20 30
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Now Curn is NULL so while is terminated.

10 20 30

You might also like