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