forked from dharmanshu1921/porject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
77 lines (68 loc) · 1.04 KB
/
queue.cpp
File metadata and controls
77 lines (68 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <climits>
using namespace std;
class Queue {
int* arr;
int size;
int f = -1;
int r = -1;
public:
Queue (int size) {
this->size = size;
arr = new int[size];
}
void push(int x) {
if (f == -1) {
f = 0;
r = 0;
} else if (r == size-1) {
cout << "Overflow" << endl;
return;
} else {
r++;
}
arr[r] = x;
}
int front() {
if (f == -1) {
cout << "Empty" << endl;
return INT_MIN;
}
return arr[f];
}
int back() {
if (r == -1) {
cout << "Empty" << endl;
return INT_MIN;
}
return arr[r];
}
void pop() {
if (f == -1) {
cout << "Underflow" << endl;
return;
}
f++;
}
void display() {
for (int i=f; i<=r; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main() {
int n;
cin >> n;
Queue q(n);
int x;
for (int i=0; i<n; i++) {
cin >> x;
q.push(x);
}
cout << q.front() << endl;
cout << q.back() << endl;
q.display();
q.pop();
q.display();
}