-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.c
More file actions
118 lines (102 loc) · 2.59 KB
/
source.c
File metadata and controls
118 lines (102 loc) · 2.59 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
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int max;
int num;
int front;
int rear;
int *que;
} IntQueue;
int Initialize(IntQueue *q, int max) {
q->num = q->front = q->rear = 0;
if ((q->que == calloc(max, sizeof(int))) == NULL) {
q->max = 0;
return -1;
}
q->max = max;
return 0;
}
void Terminate(IntQueue *q) {
if (q->que != NULL) {
free(q->que);
q->max = q->num = q->front = q->rear = 0;
}
}
int Enquq(IntQueue *q, int x) {
if (q->num >= q->max)
return -1;
else {
q->num++;
q->que[q->rear++] = x;
if (q->rear == q->max) q->rear = 0;
return 0;
}
}
int Deque(IntQueue *q, int *x) {
if (q->num <= 0)
return -1;
else {
q->num--;
*x = q->que[q->front++];
if (q->front == q->max) q->front = 0;
return 0;
}
}
int Peek(const IntQueue *q, int *x) {
if (q->num <= 0)
return -1;
*x = q->que[q->front];
return 0;
}
int Capacity(const IntQueue *q) {
return q->max;
}
int Size(const IntQueue *q) {
return q->num;
}
void Print(const IntQueue *q) {
int i;
for (i = 0; i < q->num; i++)
printf("%d ", q->que[(i + q->front) % q->max]);
putchar('\n');
}
int main(void) {
IntQueue que;
if (Initialize(&que, 6) == -1) {
puts("キューの生成に失敗しました。");
return 1;
}
while (1) {
int m, x;
printf("現在のデータ数:%d/%d\n", Size(&que), Capacity(&que));
printf("(1)エンキュー (2)デキュー (3)ピーク (4)表示 (0)終了:");
scanf("%d", &m);
// --------------①
if (m == 0) break;
switch (m) {
case 1:
printf("データ:");
scanf("%d", &x);
if (Enquq(&que, x) == -1)
puts("\aエラー:データのエンキューに失敗しました。");
break;
case 2:
if (Deque(&que, &x) == -1)
puts("\aエラー:デキューに失敗しました。");
else
printf("デキューしたデータは%dです。\n", x);
break;
case 3:
if (Peek(&que, &x) == -1)
puts("\aエラー:ピークに失敗しました。");
else
printf("ピークしたデータは%dです。\n", x);
break;
case 4:
Print(&que);
break;
}
}
Terminate(&que);
return 0;
}