Advertisement
shimul07

queue using linked list

Mar 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.     int data;
  9.     struct node *next;
  10. }*front=NULL,*rear,*temp;
  11.  
  12. void ins()
  13. {
  14.     temp=new node;
  15.     cout<<"Enter data:";
  16.     cin>>temp->data;
  17.     temp->next=NULL;
  18.    
  19.     if(front==NULL)
  20.         front=rear=temp;
  21.     else
  22.     {
  23.         rear->next=temp;
  24.         rear=temp;
  25.     }
  26. }
  27.  
  28. void del()
  29. {
  30.     if(front==NULL)
  31.         cout<<"Queue is empty\n";
  32.     else
  33.     {
  34.         temp=front;
  35.         front=front->next;
  36.         cout<<"Deleted node is "<<temp->data<<"\n";
  37.         delete(temp);
  38.     }
  39. }
  40.  
  41. void dis()
  42. {
  43.     if(front==NULL)
  44.         cout<<"Queue is empty\n";
  45.     else
  46.     {
  47.         temp=front;
  48.         while(temp!=NULL)
  49.         {
  50.             cout<<temp->data<<"->";
  51.             temp=temp->next;
  52.         }      
  53.     }
  54. }
  55.  
  56. int main()
  57. {
  58.     int ch;
  59.     while(1)
  60.     {
  61.         cout<<"\n\n*** Menu ***"<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
  62.         cout<<"\n\nEnter your choice(1-4):";
  63.         cin>>ch;
  64.         cout<<"\n";
  65.        
  66.         switch(ch)
  67.         {
  68.             case 1: ins();
  69.                     break;
  70.             case 2: del();
  71.                     break;
  72.             case 3: dis();
  73.                     break;
  74.             case 4: exit(0);
  75.                     break;
  76.             default: cout<<"Wrong Choice!!!";
  77.         }
  78.     }
  79.    
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement