rootUser

Circular Single Linked List DEMO Queue

May 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void push(int x);
  5. void pop();
  6. void search(int x);
  7.  
  8. typedef struct alvi
  9. {
  10.     int data;
  11.     struct alvi *next;
  12. }node;
  13.  
  14. node *head = NULL;
  15. node *tail = NULL;
  16.  
  17. int main(void)
  18. {
  19.     int choice,number;
  20.     while(1)
  21.     {
  22.         cout<<"Enter 1 to PUSH , 2 to POP , 3 to SEARCH , 4 to QUIT : ";
  23.         cin>>choice;
  24.         switch(choice)
  25.         {
  26.             case 1:
  27.                 cout<<"Enter a number to insert in the Linked list : ";
  28.                 cin>>number;
  29.                 push(number);
  30.                 break;
  31.             case 2:
  32.                 pop();
  33.                 break;
  34.             case 3:
  35.                 cout<<"Enter a number to search in the Linked list : ";
  36.                 cin>>number;
  37.                 search(number);
  38.                 break;
  39.             case 4:
  40.                 return 0;
  41.             default:
  42.                 cout<<"Wrong Input"<<endl;
  43.                 break;
  44.         }
  45.     }
  46.     return 0;
  47. }
  48. void push(int x)
  49. {
  50.     node *newNode = new node;
  51.     newNode->data=x;
  52.     newNode->next=NULL;
  53.     if(head==NULL)
  54.     {
  55.         tail = newNode;
  56.         cout<<"First node pushed"<<endl;
  57.     }
  58.     else
  59.     {
  60.         newNode->next=tail;
  61.         node *cursor = head;
  62.         cursor->next=newNode;
  63.         cout<<"Another node pushed"<<endl;
  64.     }
  65.     head=newNode;
  66. }
  67. void pop()
  68. {
  69.     node *cursor=tail;
  70.     cout<<"Queue :";
  71.     while(cursor!=NULL)
  72.     {
  73.         cout<<" "<<cursor->data;
  74.         cursor=cursor->next;
  75.     }
  76.     cout<<endl;
  77. }
  78. void search(int x)
  79. {
  80.     node *cursor=tail;
  81.     int flag = 0 , counter = 1;
  82.     while(cursor!=NULL)
  83.     {
  84.          if(cursor->data==x)
  85.          {
  86.              flag=1;
  87.              break;
  88.          }
  89.         counter++;
  90.         cursor=cursor->next;
  91.     }
  92.     if(flag==1)
  93.     {
  94.         cout<<"Found . The element "<<x<<" is in the position "<<counter<<" in the queue"<<endl;
  95.     }
  96.     else
  97.     {
  98.         cout<<"The element "<<x<<" is not found in the queue"<<endl;
  99.     }
  100. }
Add Comment
Please, Sign In to add comment