-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_by_Array.cpp
59 lines (49 loc) · 1.27 KB
/
queue_by_Array.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
#include <iostream>
using namespace std;
template<typename T>
class Queue{
private:
int Maxsize;
int curidx;
T *queue;
public:
Queue(int size){
Maxsize = size;
curidx = -1;
queue = new T[Maxsize];
}
bool isEmpty(){return (curidx == -1);}
bool isFull(){return (curidx == Maxsize - 1);}
void add(T data){
if (isFull()) cout << "queue is Full\n";
else queue[++curidx] = data;
}
T pop(){
if (isEmpty()) cout << "queue is Empty\n";
else{
T temp = queue[0];
for (int i = 0; i < curidx; i++) queue[i] = queue[i + 1];
curidx--;
return temp;
}
}
};
int main(){
Queue<int> q(5);
cout << "q.isEmpty(): " << q.isEmpty() << "\n";
cout << "q.isFull(): " << q.isFull() << "\n";
cout << "q.pop(): " << q.pop() << "\n";
q.add(123);
q.add(234);
q.add(345);
q.add(456);
q.add(567);
q.add(678);
cout << "q.isEmpty(): " << q.isEmpty() << "\n";
cout << "q.isFull(): " << q.isFull() << "\n";
cout << "q.pop(): " << q.pop() << "\n";
cout << "q.pop(): " << q.pop() << "\n";
cout << "q.pop(): " << q.pop() << "\n";
cout << "q.pop(): " << q.pop() << "\n";
cout << "q.pop(): " << q.pop() << "\n";
}