Advertisement
saske_7

class_queue.cpp

Nov 3rd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std ;
  4.  
  5. #define M 500
  6. class _queue
  7. {
  8.     int arr[M], top, rear;
  9. public:
  10.     _queue()
  11.     {
  12.         top   =  rear = -1;
  13.     };
  14.     void push(int a)
  15.     {
  16.         if(rear ==  M )
  17.         {
  18.             cout <<"overflow\n";
  19.             return ;
  20.         }
  21.         if(top ==  -1)
  22.             top++;
  23.  
  24.         rear++ ;
  25.         arr[rear ] =  a;
  26.         cout << "Item inserted to queue : " << a << endl ;
  27.  
  28.     }
  29.     void pop(void )
  30.     {
  31.         if(top >  rear  || rear == -1)
  32.         {
  33.             cout << "Queue empty\n";
  34.             top =  rear = -1;
  35.             return ;
  36.         }
  37.         cout<< "Deleting item : " << arr[top ] << endl;
  38.         top++;
  39.     }
  40.     void front()
  41.     {
  42.         if(top ==  -1 || top > rear )
  43.             cout << "queue empty" << endl;
  44.         else
  45.             cout << arr[top ] << endl ;
  46.     }
  47. };
  48.  
  49. void input()
  50. {
  51.     _queue q ;
  52.  
  53.     int i, j,k ;
  54.     while(1)
  55.     {
  56.         int choice, item ;
  57.         cout <<"Enter choice (0/1)" << endl;
  58.         cout <<"0 - to delete from queue :" << endl;
  59.         cout <<"1  -  to insert into queue or " << endl;
  60.         cout <<"2 - to show front  :" << endl;
  61.  
  62.         cin >> choice ;
  63.         if(choice ==  1)
  64.         {
  65.             cout <<"Enter item\n";
  66.             cin >> item ;
  67.             q.push(item );
  68.         }
  69.  
  70.         else if(choice ==  0) q.pop();
  71.         else q.front();
  72.         cout <<"\n" << endl;
  73.     }
  74. }
  75.  
  76. int main()
  77. {
  78.     input();
  79.     /*
  80.         _queue q ;
  81.         q.push(11);
  82.         q.push(12);
  83.         q.push(13);
  84.         q.front();
  85.         q.pop();
  86.         q.pop();
  87.         q.pop();
  88.         q.pop();
  89.  
  90.     */
  91.  
  92.     return 0 ;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement