arox14

Push & Pop in a Queue implemented as an array

Jan 23rd, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. //Push & Pop in a Queue implemented as an array
  2.  
  3. #include<iostream.h>
  4. #include<conio.h>
  5. #include<stdio.h>
  6. void main()
  7. {clrscr();
  8. int rear=-1, front=-1, Size=5;
  9. void InsertQ(int Queue[])
  10. {
  11.     int Insert;
  12.     cout<<"Enter the number you want to insert"<<endl;
  13.     cin>>Insert;
  14.     if(rear==Size-1)
  15.     {
  16.         cout<<"Overflow"<<endl;
  17.     }
  18.     else if(rear==-1)
  19.     {
  20.         rear=front=0;
  21.         Queue[0]=Insert;
  22.     }
  23.         else
  24.         {
  25.             rear++;
  26.             Queue[rear]=Insert;
  27.         }
  28. }
  29.  
  30. void DeleteQ(int Queue[])
  31. {
  32.     if(front==-1)
  33.     {
  34.         cout<<"Underflow"<<endl;
  35.     }
  36.     else
  37.         if(front==rear)
  38.         {
  39.             cout<<Queue[front]<<" Was Deleted"<<endl;
  40.             front=rear=-1;
  41.         }
  42.         else
  43.         {
  44.             cout<<Queue[front]<<" Was Deleted"<<endl;
  45.             front++;
  46.         }
  47. }
  48. void Display(int Queue[])
  49. {
  50.     int i;
  51.     cout<<"The Queue is: ";
  52.     for(i=front;i<=rear;i++)
  53.     {
  54.         cout<<Queue[i]<<" ";
  55.     }
  56. }
  57. int main()
  58. {
  59.     int Q[50],o1;
  60.     char o2;
  61.     do
  62.     {
  63.         cout<<"MENU"<<endl;
  64.         cout<<"1. Insert \n2. Delete \n3. Display \n4. Exit"<<endl;
  65.         cin>>o1;
  66.         switch(o1)
  67.         {
  68.             case 1:
  69.                 InsertQ(Q);
  70.                 break;
  71.             case 2:
  72.                 DeleteQ(Q);
  73.                 break;
  74.             case 3:
  75.                 Display(Q);
  76.                 break;
  77.             case 4:
  78.                 exit(0);
  79.             default:
  80.                 cout<<"Please Enter A Valid Option"<<endl;
  81.                 break;
  82.                
  83.         }
  84.         cout<<"Would You Like to Continue?(Y/N)"<<endl;
  85.         cin>>o2;
  86.     }while(o2=='Y' || o2=='y');
  87. }
  88.  
  89. /*
  90.  MENU
  91.  1. Insert
  92.  2. Delete
  93.  3. Display
  94.  4. Exit
  95.  1
  96.  Enter the number you want to insert
  97.  2
  98.  Would You Like to Continue?(Y/N)
  99.  Y
  100.  MENU
  101.  1. Insert
  102.  2. Delete
  103.  3. Display
  104.  4. Exit
  105.  1
  106.  Enter the number you want to insert
  107.  3
  108.  Would You Like to Continue?(Y/N)
  109.  Y
  110.  MENU
  111.  1. Insert
  112.  2. Delete
  113.  3. Display
  114.  4. Exit
  115.  3
  116.  The Queue is: 2 3
  117. Would You Like to Continue?(Y/N)
  118.  Y
  119.  MENU
  120.  1. Insert
  121.  2. Delete
  122.  3. Display
  123.  4. Exit
  124.  2
  125.  3 Was Deleted
  126.  Would You Like to Continue?(Y/N)
  127.  N
  128.  */
Add Comment
Please, Sign In to add comment