-
Notifications
You must be signed in to change notification settings - Fork 0
/
스텍_큐.c
92 lines (74 loc) · 1.22 KB
/
스텍_큐.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
#include <stdio.h>
#include<stdlib.h>
#define SIZE 10
typedef struct StackType
{
char data[SIZE];
int top;
}StackType;
void init(StackType* S)
{
S->top = -1;
}
int isEmpty(StackType* S)
{
return S->top == -1;
}
int isFull(StackType* S)
{
return S->top == SIZE - 1;
}
void push(StackType* S, char e)
{
if(isFull(S))
{
printf("Overflow\n");
return;
}
S->top++;
S->data[S->top] = e;
}
char pop(StackType* S)
{
if(isEmpty(S))
{
printf("Empty\n");
return 0;
}
char temp = S->data[S->top];
S->top--;
return temp;
}
void enqueue(StackType* S1, char e)
{
push(S1, e);
}
char dequeue(StackType* S1, StackType* S2)
{
char e;
while(S1->top != -1)
{
push(S2, pop(S1));
}
e = pop(S2);
while(S2->top != -1)
{
push(S1, pop(S2));
}
return e;
}
int main()
{
StackType S1, S2;
init(&S1); init(&S2);
enqueue(&S1, 'A');
enqueue(&S1, 'B');
enqueue(&S1, 'C');
printf("[%c] ", dequeue(&S1, &S2));
enqueue(&S1, 'D');
enqueue(&S1, 'E');
printf("[%c] ", dequeue(&S1, &S2));
printf("[%c] ", dequeue(&S1, &S2));
printf("[%c] ", dequeue(&S1, &S2));
printf("[%c] ", dequeue(&S1, &S2));
}