Advertisement
i_love_rao_khushboo

Untitled

Jan 9th, 2023
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAX = 10;
  5.  
  6. class MyQueue {
  7.     int L, R;
  8.     int a[MAX];
  9.    
  10.     public:
  11.         MyQueue();
  12.         void push(int num);
  13.         void pop();
  14.         int front();
  15.         int rear();
  16.         int size();
  17.         bool is_empty();
  18. };
  19.  
  20. MyQueue::MyQueue() {
  21.     L = R = -1;
  22. }
  23.  
  24. void MyQueue::push(int num) {
  25.     if(R >= MAX - 1) {
  26.         cout << "Queue overflow!\n";
  27.         return;
  28.     }
  29.    
  30.     a[++R] = num;
  31.    
  32.     if(L == -1) L = R;
  33. }
  34.  
  35. void MyQueue::pop() {
  36.     if(L == -1) {
  37.         cout << "Queue empty!\n";
  38.         return;
  39.     }
  40.    
  41.     L += 1;
  42.    
  43.     if(L > R) {
  44.         L = -1;
  45.         R = -1;
  46.     }
  47. }
  48.  
  49. int MyQueue::front() {
  50.     if(L == -1) {
  51.         cout << "Queue empty!\n";
  52.         return INT_MAX;
  53.     }
  54.    
  55.     return a[L];
  56. }
  57.  
  58. int MyQueue::rear() {
  59.     if(R == -1) {
  60.         cout << "Queue empty!\n";
  61.     }
  62.    
  63.     return a[R];
  64. }
  65.  
  66. int MyQueue::size() {
  67.     if(L == -1) return 0;
  68.     else return (R - L + 1);
  69. }
  70.  
  71. bool MyQueue::is_empty() {
  72.     if(L == -1) return 1;
  73.    
  74.     int siz = R - L + 1;
  75.     return siz == 0;
  76. }
  77.  
  78. void solve()
  79. {
  80.     MyQueue q;
  81.     q.push(10);
  82.     q.push(20);
  83.     q.push(30);
  84.     q.push(49);
  85.     q.push(-107);
  86.     q.push(13);
  87.    
  88.     cout << q.front() << " is the front element of queue\n";
  89.     cout << q.rear() << " is the rear element of queue\n";
  90.    
  91.     q.pop();
  92.     cout << "Size of queue: " << q.size() << "\n";
  93.    
  94.     // print all elements in stack :
  95.     cout << "Elements present in queue: ";
  96.    
  97.     while(!q.is_empty()) {
  98.         // print top element in stack
  99.         cout << q.front() << " ";
  100.         // remove top element in stack
  101.         q.pop();
  102.     }
  103. }
  104.  
  105. int main()
  106. {
  107.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  108.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  109.  
  110.     // #ifndef ONLINE_JUDGE
  111.     //     freopen("input.txt", "r", stdin);
  112.     //     freopen("output.txt", "w", stdout);
  113.     // #endif
  114.    
  115.     // #ifndef ONLINE_JUDGE
  116.     //      freopen("error.txt", "w", stderr);
  117.     // #endif
  118.    
  119.     int t = 1;
  120.     // int test = 1;
  121.     // cin >> t;
  122.     while(t--) {
  123.         // cout << "Case #" << test++ << ": ";
  124.         solve();
  125.     }
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement