-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_queue.cpp
83 lines (73 loc) · 1.41 KB
/
my_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
83
#define DEBUG
#include <iostream>
using namespace std;
template<class T>
class _queue
{
private:
T* q;
int begin, end;
int total;
public:
_queue(){
q = new T[100];
begin = -1;
end = -1;
total = 0;
}
inline bool empty() {return (total)?false:true;};
inline int size() {return total;};
T& front() {
if (empty()) throw "empty!!!";
return q[begin];
}
T& back() {
if (empty()) throw "empty!!!";
return q[end];
}
void push(const T& item);
void pop();
};
int main ()
{
#ifdef DEBUG
freopen("in.in", "r", stdin);
#endif
_queue<int > t;
t.pop();
t.pop();
t.push(3);
cout << t.front() << endl;
cout << t.back() << endl;
t.push(4);
cout << t.front() << endl;
cout << t.back() << endl;
t.pop();
cout << t.front() << endl;
cout << t.back() << endl;
cout << t.empty();
t.pop();
cout << t.front() << endl;
cout << t.back() << endl;
return 0;
}
template<class T>
void _queue<T>::push (const T& item)
{
if (total == 100) {
cout << "full" << endl;
return;
}
if (begin == -1) {
begin = 0;
}
q[++end] = item;
total++;
}
template<class T>
void _queue<T>::pop ()
{
if (empty()) return ;
begin++;
total--;
}