-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.queue.cpp
84 lines (79 loc) · 1.71 KB
/
2.queue.cpp
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
78
79
80
81
82
/*************************************************************************
> File Name: 2.queue.cpp
> Author: rEn Chong
> Mail: [email protected]
> Created Time: 一 11/21 10:05:51 2022
************************************************************************/
#include <iostream>
#include <vector>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
class Queue {
public :
Queue(int n = 10) : arr(n), head(0), tail(0) {}
void push(int x) {
if (full()) {
cout << "queue full" << endl;
return ;
}
arr[tail] = x;
tail += 1;
return ;
}
void pop() {
if (empty()) {
return ;
}
head += 1;
}
bool empty() {
return head == tail;
}
bool full() {
return tail == arr.size();
}
int front() {
if (empty()) return 0;
return arr[head];
}
int size() {
return tail - head;
}
void output() {
cout << "Queue : ";
for (int i = head; i < tail; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void clear() {
head = tail = 0;
return ;
}
private :
int head, tail;
vector<int> arr;
};
int main() {
string op;
int value;
Queue q(5);
while (cin >> op) {
if (op == "insert") {
cin >> value;
q.push(value);
} else if (op == "front") {
cout << "front element : " << q.front() << endl;
} else if (op == "pop") {
q.pop();
} else if (op == "size") {
cout << "queue size : " << q.size() << endl;
}
q.output();
}
return 0;
}