-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue10845.cpp
More file actions
112 lines (110 loc) · 1.42 KB
/
queue10845.cpp
File metadata and controls
112 lines (110 loc) · 1.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
using namespace std;
struct Queue
{
int data[10000];
int begin, end;
Queue()
{
begin = 0;
end = 0;
}
void push(int num)
{
data[end] = num;
end += 1;
}
bool empty()
{
if (begin == end)
{
return true;
}
else
{
return false;
}
}
int size()
{
return end - begin;
}
int front()
{
return data[begin];
}
int back()
{
return data[end - 1];
}
int pop()
{
if (empty())
{
return -1;
}
begin += 1;
return data[begin - 1];
}
};
int main()
{
int n;
cin >> n;
Queue q;
while (n--)
{
string cmd;
cin >> cmd;
if (cmd == "push")
{
int num;
cin >> num;
q.push(num);
}
else if (cmd == "pop")
{
if (q.empty())
{
cout << -1 << '\n';
}
else
{
cout << q.front() << '\n';
q.pop();
}
}
else if (cmd == "size")
{
cout << q.size() << '\n';
}
else if (cmd == "empty")
{
cout << q.empty() << '\n';
}
else if (cmd == "front")
{
if (q.empty())
{
cout << -1 << '\n';
}
else
{
cout << q.front() << '\n';
}
}
else if (cmd == "back")
{
if (q.empty())
{
cout << -1 << '\n';
}
else
{
cout << q.back() << '\n';
}
}
}
return 0;
}