3.3Queue Using Linked List
3.3Queue 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
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
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;
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