-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclass-6-1012-queue-basic.c
More file actions
76 lines (75 loc) · 1.15 KB
/
inclass-6-1012-queue-basic.c
File metadata and controls
76 lines (75 loc) · 1.15 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
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 10
int queue[MAX_SIZE]={0};
int s=0, e=0;
int size() {
return e-s;
}
void refresh() {
if (s == 0) return;
printf("run refresh\n");
int sz = size();
for(int q=0; q<sz; q++) {
queue[q] = queue[s+q];
}
s = 0;
e = sz;
}
int push(int n) {
if (e == MAX_SIZE) {
refresh();
}
if (e == MAX_SIZE) {
printf("push fail\n");
return 0;
}
queue[e++]=n;
return 1;
}
int front() {
if (size()) {
return queue[s];
} else {
return -1;
}
}
int pop() {
if (size()) {
s++;
return 1;
}
return 0;
}
int back() {
if (size()) {
return queue[e-1];
} else {
return -1;
}
}
int main() {
char cmd[10];
int n;
while(~scanf("%s", cmd)) {
if (strcmp(cmd, "push") == 0) {
scanf(" %d", &n);
push(n);
} else if (strcmp(cmd, "front") == 0) {
printf("%d\n", front());
} else if (strcmp(cmd, "pop") == 0) {
pop();
} else if (strcmp(cmd, "back") == 0) {
printf("%d\n", back());
} else if (strcmp(cmd, "size") == 0) {
printf("%d\n", size());
} else {
printf("no command\n");
}
int sz = size();
for (int q=s; q < s+sz; q++) {
printf("%d ", queue[q]);
}
printf("\n");
}
}