-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path05-動態陣列實作佇列.c
159 lines (146 loc) · 3.86 KB
/
05-動態陣列實作佇列.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <stdlib.h>
/*
本範例利用大量 C 語言技巧,僅供參考,初學者建議跳過
用靜態陣列和鍵結串列實作在演算法效能上都不是好方法
因此此支程式盡可能著重在佇列的效能及封裝上
*/
typedef struct e* Element;
typedef struct q* Queue;
struct e {
char* name;
int length;
};
// 開頭變數 _ 表示 private
struct q {
int _cap;
int _rear;
int _len;
int _front;
Element* list;
void (*enqueue)(Queue, Element);
Element (*dequeue)(Queue);
int (*isEmpty)(Queue);
void (*print)(Queue);
};
void enqueue(Queue this, Element e) {
if ((this->_len + 1) == (this->_cap)) {
int newCap = this->_cap << 1;
Element* newList = malloc(sizeof(*newList) * newCap);
int i;
for (i = 0; i < this->_len; i++) {
newList[i] = this->list[(this->_rear + i) % this->_cap];
}
this->list = newList;
this->_front = this->_len;
this->_rear = 0;
this->_cap = newCap;
}
this->list[this->_front] = e;
this->_front = (this->_front + 1) % this->_cap;
this->_len++;
}
Element dequeue(Queue this) {
if (this->isEmpty(this)) {
fprintf(stderr, "Queue is empty");
exit(EXIT_FAILURE);
}
Element tmp = this->list[this->_rear];
this->_rear = (this->_rear + 1) % this->_cap;
this->_len--;
return tmp;
}
int isEmpty(Queue this) {
return this->_len == 0;
}
void queuePrint(Queue this) {
int i;
Element e;
for (i = 0; i < this->_len; i++) {
e = this->list[(this->_rear + i) % this->_cap];
printf("%s (%d min %d s) -> ", e->name, e->length / 60, e->length % 60);
}
printf("NULL\n");
}
Queue __Queue__() {
Queue this = malloc(sizeof(*this));
this->_cap = 8;
this->_len = 0;
this->_rear = 0;
this->_front = 0;
this->list = malloc(this->_cap * sizeof(*(this->list)));
this->enqueue = enqueue;
this->dequeue = dequeue;
this->isEmpty = isEmpty;
this->print = queuePrint;
return this;
}
Element createElement(char* name, int len) {
Element e = malloc(sizeof(*e));
e->name = name;
e->length = len;
return e;
}
int main() {
Queue q = __Queue__();
Element song1 = createElement("如果我們不曾相遇", 229);
Element song2 = createElement("盛夏光年", 349);
Element song3 = createElement("說散就散", 219);
Element song4 = createElement("金曜日のおはよう", 242);
Element song5 = createElement("SOLO", 169);
Element song6 = createElement("體面", 278);
Element song7 = createElement("可愛くなりたい", 252);
Element song8 = createElement("如煙", 319);
q->enqueue(q, song1);
q->print(q);
printf("dequeue\n");
q->dequeue(q);
q->print(q);
q->enqueue(q, song2);
q->print(q);
printf("dequeue\n");
q->dequeue(q);
q->print(q);
q->enqueue(q, song3);
q->print(q);
q->enqueue(q, song4);
q->print(q);
printf("dequeue\n");
q->dequeue(q);
q->print(q);
q->enqueue(q, song5);
q->print(q);
q->enqueue(q, song6);
q->print(q);
q->enqueue(q, song7);
q->print(q);
q->enqueue(q, song8);
q->print(q);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->enqueue(q, song8);
q->print(q);
return 0;
}