-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path02-陣列實作佇列-結構體.c
77 lines (65 loc) · 1.57 KB
/
02-陣列實作佇列-結構體.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
#include <stdio.h>
#include <stdlib.h>
//指定這個佇列的長度
#define MAX_QUEUE_SIZE 100
// 佇列的型態不一定只放整數,而可能是結構體
// 因此我們用結構體改寫一次
struct e {
char* name;
int length;
};
typedef struct e element;
// 宣告一個長度為 MAX_QUEUE_SIZE 的全域變數 queue
element queue[MAX_QUEUE_SIZE];
int rear = -1;
int front = -1;
// 從 rear 插入元素,從 front 取出元素
void queueFull() {
fprintf(stderr, "Queue is full\n");
exit(EXIT_FAILURE);
}
void queueEmpty() {
fprintf(stderr, "Queue is Empty\n");
exit(EXIT_FAILURE);
}
// 插入元素
void enqueue(element value) {
if (rear == MAX_QUEUE_SIZE - 1) {
queueFull(); // 呼叫佇列已滿函式
}
queue[++rear] = value;
}
element dequeue() {
if (front == rear) {
queueEmpty(); // 呼叫佇列為空的函式
}
return queue[++front];
}
// 方便我們觀察 Queue 變化
void printQueueLn() {
int i;
for (i = front + 1; i <= rear; i++) {
printf("%s(%d s)\t", queue[i].name, queue[i].length);
}
printf("\n");
}
int main() {
element song1 = {"如果我們不曾相遇", 229};
element song2 = {"盛夏光年", 349};
element song3 = {"說散就散", 219};
element song4 = {"金曜日のおはよう", 242};
enqueue(song1);
printQueueLn();
enqueue(song2);
printQueueLn();
enqueue(song3);
printQueueLn();
dequeue();
printQueueLn();
dequeue();
printQueueLn();
enqueue(song4);
printQueueLn();
system("pause");
return 0;
}