Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MAX = 10;
- class MyQueue {
- int L, R;
- int a[MAX];
- public:
- MyQueue();
- void push(int num);
- void pop();
- int front();
- int rear();
- int size();
- bool is_empty();
- };
- MyQueue::MyQueue() {
- L = R = -1;
- }
- void MyQueue::push(int num) {
- if(R >= MAX - 1) {
- cout << "Queue overflow!\n";
- return;
- }
- a[++R] = num;
- if(L == -1) L = R;
- }
- void MyQueue::pop() {
- if(L == -1) {
- cout << "Queue empty!\n";
- return;
- }
- L += 1;
- if(L > R) {
- L = -1;
- R = -1;
- }
- }
- int MyQueue::front() {
- if(L == -1) {
- cout << "Queue empty!\n";
- return INT_MAX;
- }
- return a[L];
- }
- int MyQueue::rear() {
- if(R == -1) {
- cout << "Queue empty!\n";
- }
- return a[R];
- }
- int MyQueue::size() {
- if(L == -1) return 0;
- else return (R - L + 1);
- }
- bool MyQueue::is_empty() {
- if(L == -1) return 1;
- int siz = R - L + 1;
- return siz == 0;
- }
- void solve()
- {
- MyQueue q;
- q.push(10);
- q.push(20);
- q.push(30);
- q.push(49);
- q.push(-107);
- q.push(13);
- cout << q.front() << " is the front element of queue\n";
- cout << q.rear() << " is the rear element of queue\n";
- q.pop();
- cout << "Size of queue: " << q.size() << "\n";
- // print all elements in stack :
- cout << "Elements present in queue: ";
- while(!q.is_empty()) {
- // print top element in stack
- cout << q.front() << " ";
- // remove top element in stack
- q.pop();
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- srand(chrono::high_resolution_clock::now().time_since_epoch().count());
- // #ifndef ONLINE_JUDGE
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- // #endif
- // #ifndef ONLINE_JUDGE
- // freopen("error.txt", "w", stderr);
- // #endif
- int t = 1;
- // int test = 1;
- // cin >> t;
- while(t--) {
- // cout << "Case #" << test++ << ": ";
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement