Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

Queue->Linked List implementation->Delete operation

Every time we delete an element, we check whether it is the last element left in the queue or not. If it is then we delete it and make our 'front', 'rear' and 'queue' pointers to point to NULL; to depict an empty queue. If the queue is empty, we give a "queue empty" message.

Algorithm:-
Step-1: Check for the "queue empty" condition. If it is then go to step-2 or else step-3
Step-2: Give away a "queue empty" message and quit
Step-3: Delete the element. If this was the last element to be deleted then go to step-a or else step-b
           step-a: make our 'front', 'rear' and 'queue' pointers NULL
            step-b: shift your 'front' and 'queue' pointer ahead to point to the next element in
                       the queue

C implementation:-
struct node
{
 int value;
 struct node *next;
}
struct node *queue, *front, *rear;

main( )
{
 queue = front = rear = NULL; /*queue is initially empty*/
..
..
 del_value = delete( );
..
 del_value = delete( );
..
}

int delete( )
{
 int delval = 0;
 if(front == NULL) printf("queue empty");  /*step-2*/
   else
     {               /*step-3*/
      delval = front->value;
      if(front->next == NULL)
       {
        free(front);
        queue = front = rear = NULL; /*step-a*/
       }
      else    /*step-b*/
       {
        front = front->next;
        free(queue);
        queue = front;
       }
     }
}

Related Operations:
Queues - Concepts
Array implementation - add( ) and delete( )
Linked List implementation - add( ) and delete( )
Circular queues - add( ) and delete( )

Index || Doubts / Clarifications || Related Topics || Web Links