Advertisement
splash365

circular queue (linked list)

Feb 9th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. #define SIZE 100
  4.  
  5. using namespace std;
  6.  
  7. class node
  8. {
  9. public:
  10.     node()
  11.     {
  12.         next = NULL;
  13.     }
  14.   int data;
  15.   node *next;
  16. }*front=NULL,*rear=NULL,*n,*temp,*temp1;
  17.  
  18. class cqueue
  19. {
  20. public:
  21.     void insertion();
  22.     void deletion();
  23.     void display();
  24. };
  25.  
  26. int main()
  27. {
  28.     cqueue cqobj;
  29.   int ch;
  30.   do
  31.   {
  32.      cout<<"\n\n\tMain Menu";
  33.      cout<<"\n##########################";
  34.      cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Your Choice: ";
  35.      cin>>ch;
  36.      switch(ch)
  37.      {
  38.         case 1:
  39.           cqobj.insertion();
  40.           cqobj.display();
  41.           break;
  42.         case 2:
  43.           cqobj.deletion();
  44.           break;
  45.         case 3:
  46.           cqobj.display();
  47.           break;
  48.         case 4:
  49.           break;
  50.         default:
  51.           cout<<"\n\nWrong Choice!!! Try Again.";
  52.      }
  53.   }while(ch!=4);
  54.   return 0;
  55. }
  56.  
  57. void cqueue::insertion()
  58. {
  59.   n=new node[sizeof(node)];
  60.   cout<<"\nEnter the Element: ";
  61.   cin>>n->data;
  62.   if(front==NULL)
  63.   {
  64.       front=n;
  65.   }
  66.   else
  67.   {
  68.       rear->next=n;
  69.   }
  70.   rear=n;
  71.   rear->next=front;
  72. }
  73.  
  74. void cqueue::deletion()
  75. {
  76.   int x;
  77.   temp=front;
  78.   if(front==NULL)
  79.   {
  80.       cout<<"\nCircular Queue Empty!!!";
  81.   }
  82.   else
  83.   {
  84.      if(front==rear)
  85.      {
  86.        x=front->data;
  87.        delete(temp);
  88.        front=NULL;
  89.        rear=NULL;
  90.      }
  91.      else
  92.      {
  93.         x=temp->data;
  94.         front=front->next;
  95.         rear->next=front;
  96.         delete(temp);
  97.      }
  98.      cout<<"\nElement "<<x<<" is Deleted";
  99.      display();
  100.   }
  101. }
  102.  
  103. void cqueue::display()
  104. {
  105.   temp=front;
  106.   temp1=NULL;
  107.   if(front==NULL)
  108.   {
  109.     cout<<"\n\nCircular Queue Empty!!!";
  110.   }
  111.   else
  112.   {
  113.     cout<<"\n\nCircular Queue Elements are:\n\n";
  114.     while(temp!=temp1)
  115.     {
  116.        cout<<temp->data<<"  ";
  117.        temp=temp->next;
  118.        temp1=front;
  119.     }
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement