-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueArraySequential.cpp
More file actions
122 lines (105 loc) · 2.65 KB
/
QueueArraySequential.cpp
File metadata and controls
122 lines (105 loc) · 2.65 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
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using std::cout;
class QueueArraySequential{
private:
int capacity, front, back;
int *queue;
void DoubleCapacity();
public:
QueueArraySequential():capacity(5),front(-1),back(-1){
queue = new int[capacity];
};
void Push(int x);
void Pop();
bool IsEmpty();
bool IsFull();
int getFront();
int getBack();
int getSize();
int getCapacity(); // 驗證用, 可有可無
};
void QueueArraySequential::DoubleCapacity(){
capacity *= 2;
int *newQueue = new int[capacity];
int j = -1;
for (int i = front+1; i <= back; i++) {
j++;
newQueue[j] = queue[i];
}
front = -1; // 新的array從0開始, 把舊的array"整段平移", front跟back要更新
back = j;
delete [] queue;
queue = newQueue;
}
void QueueArraySequential::Push(int x){
if (IsFull()) {
DoubleCapacity();
}
queue[++back] = x;
}
void QueueArraySequential::Pop(){
if (IsEmpty()) {
std::cout << "Queue is empty.\n";
return;
}
front++;
}
bool QueueArraySequential::IsFull(){
return (back + 1 == capacity);
}
bool QueueArraySequential::IsEmpty(){
return (front == back);
}
int QueueArraySequential::getFront(){
if (IsEmpty()) {
std::cout << "Queue is empty.\n";
return -1;
}
return queue[front+1];
}
int QueueArraySequential::getBack(){
if (IsEmpty()) {
std::cout << "Queue is empty.\n";
return -1;
}
return queue[back];
}
int QueueArraySequential::getSize(){
return (back - front);
}
int QueueArraySequential::getCapacity(){
return capacity;
}
void printSequentialQueue (QueueArraySequential queue){
cout << "front: " << queue.getFront() << " back: " << queue.getBack() << "\n"
<< "capacity: " << queue.getCapacity() << " number of elements: " << queue.getSize() << "\n\n";
}
int main(){
QueueArraySequential q;
if (q.IsEmpty()) {
cout << "Queue is empty.\n\n";
}
q.Push(24);
cout << "After push 24: \n";
printSequentialQueue(q);
q.Push(8);
q.Push(23);
cout << "After push 8, 23: \n";
printSequentialQueue(q);
q.Pop();
cout << "After pop 24: \n";
printSequentialQueue(q);
q.Push(13);
cout << "After push 13: \n";
printSequentialQueue(q);
q.Pop();
cout << "After pop 8: \n";
printSequentialQueue(q);
q.Push(35);
cout << "After push 35: \n";
printSequentialQueue(q);
q.Push(9);
cout << "After push 9: \n";
printSequentialQueue(q);
return 0;
}