QUEUE IMPLEMENTATION USING SINGLE LINKED LIST


 /*

Author: B.Vinod Kumar

email: vinod_cse2008@yahoo.com  This is a program for implementation of the queue using single linked list.

  The operations performed on a queue are  1)enqueue(): This is the function which is for insertion(enqueueing)of an element into queue

              

      It is similar to the insertion of an element at the end of a single linked list

     

               see  the function inset_end() in the program for operations of single linked list

  2)dequeue(): This is the function which is for deletion(dequeueing) of an element from the queue

               It is similar to the deletion of an element at front of a single linked list

      see  the function delete_front() in the program for operations of single linked list

  3)queue_display():This is the function which is for displaying the elements of a queue

    

      It is similar to the forward traversal of a single linked list

      see  the function ftraverse() in the program for operations of single linked list

note:

Queue is a list of elements in which insertions are at one end of the list called rear end

and the deletions are at the other end of the list called FRONT end.

Insertion is also known as Enqueue,deletion is also known as Dequeue*/

#include<iostream.h>

class queue

{

 int element;

 queue* next;

public:

 queue* enqueue(queue*,int);

 queue* dequeue(queue*);

 void queue_display(queue*);

}*head,*tail,object;queue* queue::enqueue(queue* head,int key)

{

 queue* temp;

 temp=new queue;

 temp->element=key;

 temp->next=NULL;

 if(head==NULL)

  head=temp;

 else

  tail->next=temp;

 tail=temp;

 return head;

}

queue* queue::dequeue(queue* head)

{

queue* temp;

if(head==NULL)

{

 cout<<“\nit is impossible to dequeue an element as “;

 return NULL;

}

else if(head->next==NULL)

{

 cout<<“\nthe element dequeued from the queue is: “<<head->element<<endl;

 return NULL;

}

else

{

 cout<<“\nthe element dequeued from the queue is “<<head->element<<endl;

 temp=head->next;

 head=temp;

 cout<<“\nthe elements of queue after dequeueing are \n”;

 return head;

}

}

void queue::queue_display(queue* head)

{

 if(head!=NULL)

 {

  while(head->next!=NULL)

  {

   cout<<head->element<<“->”;

   head=head->next;

  }

  cout<<head->element;

  cout<<endl;

 }

 else

  cout<<“the queue is empty\n”;

}

void choice()

{

 int key,ch;

 head=tail=NULL;

 cout<<“\nchoose the operation\n”;

 cout<<“\n1.enqueue\t2.dequeue\t3.exit\n\n”;

 cin>>ch;

 while(ch!=3)

 {

  switch(ch)

  {

  case 1:

  cout<<“\nenter the key to be inserted\n”;

  cin>>key;

  head=object.enqueue(head,key);

  cout<<“\nthe elements of queue after inserting “<<key<<” are\n”;

  object.queue_display(head);

  break;

  case 2:

   head=object.dequeue(head);   

   object.queue_display(head);

   break;

  case 3:

   break;

  default:

   cout<<“\nenter correct choice\n”;

   break;

  }

  cout<<“\n——————————————————————————\n”;

 cout<<“\nchoose the operation\n”;

 cout<<“\n1.enqueue\t2.dequeue\t3.exit\n\n”;

 cin>>ch;

  cout<<“\n——————————————————————————\n”;

 }

}

void main()

{

 choice();

}

4 thoughts on “QUEUE IMPLEMENTATION USING SINGLE LINKED LIST”

Leave a comment