rootUser

Circular Single Linked List MAIN QUEUE

May 27th, 2016
36
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.     cout<<" "<<cursor->data;
  72.     while(cursor!=NULL && cursor->next!=tail)
  73.     {
  74.         cout<<" "<<(cursor->next)->data;
  75.         cursor=cursor->next;
  76.     }
  77.  
  78.     cout<<endl;
  79. }
  80. void search(int x)
  81. {
  82.     node *cursor=tail;
  83.     int flag = 0 , counter = 1;
  84.     if(head->data==x ||tail->data==x)
  85.     {
  86.         flag=1;
  87.     }
  88.     while(cursor!=NULL && cursor->next!=tail)
  89.     {
  90.         if(cursor->data==x)
  91.         {
  92.             flag=1;
  93.             break;
  94.         }
  95.         counter++;
  96.         cursor=cursor->next;
  97.     }
  98.     if(flag==1)
  99.     {
  100.         cout<<"Found . The element "<<x<<" is in the position "<<counter<<" in the queue"<<endl;
  101.     }
  102.     else
  103.     {
  104.         cout<<"The element "<<x<<" is not found in the queue"<<endl;
  105.     }
  106. }
Add Comment
Please, Sign In to add comment