-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
203 lines (188 loc) · 3.64 KB
/
stack.c
File metadata and controls
203 lines (188 loc) · 3.64 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdlib.h>
#include <stdio.h>
#define MaxSize 40 // 数组容量
#define PUSH 0
#define POP 1
#define GET_TOP 2
#define STACK_EMPTY 3
#define EXIT -1
typedef int DataType;
typedef struct
{
DataType data[MaxSize];
int top; // 栈顶指针
} Stack; // 结构体类型名
/**
* 初始化一个栈
* @param S
*/
void InitStack(Stack *s)
{
s->top = -1;
}
/**
* 向栈插入一个元素
* @param S 操作栈
* @param e 操作数
* @return 成功返回1,否则返回0
*/
int Push(Stack *s, int data)
{
s->top++;
s->data[s->top] = data;
}
/**
* 从栈中弹出一个元素
* @param S 操作栈
* @param e 接受栈弹出的值
* @return 成功返回1,否则返回0
*/
int Pop(Stack *S, int *data)
{
if (S->top != -1) {
*data = S->data[S->top];
S->top = S->top - 1;
return 1;
}
else {
return 0;
}
}
/**
* 获取栈顶元素,不删除该元素
* @param S 操作栈
* @param e 接受栈顶元素
* @return 成功返回1,否则返回0
*/
int GetTop(Stack S, DataType *e)
{
if (S.top == -1) {
return 0;
}
else {
*e = S.data[S.top];
return 1;
}
}
/**
* 判断栈是否空,为空返回1,否则返回0
* @param S
* @return
*/
int StackEmpty(Stack S)
{
if (S.top == -1) {
return 1;
}
else {
return 0;
}
}
/**
* 获取栈的一个数组拷贝
* @param S 操作栈
* @param seq 栈中元素的一个拷贝
*/
void StackToArray(Stack S, DataType *seq)
{
for (int i = 0; i <= S.top; i++)
{
seq[i] = S.data[S.top-i]; // 自栈顶至栈底
}
}
/**
* 返回栈已使用长度
* @param S 操作栈
* @return 返回栈的长度
*/
int StackLength(Stack S)
{
return S.top + 1;
}
void outputStack(Stack *S)
{
int seq[MaxSize], i = 0;
StackToArray(*S, seq);
printf("Stack: ");
for (i = 0; i < StackLength(*S); i++)
{
printf("%d", seq[i]);
if(i<StackLength(*S)-1){
printf(" ");
}
}
printf("\n");
}
int main(int argc, char **argv)
{
Stack *S = (Stack *)malloc(sizeof(Stack));
InitStack(S);
int mode, num, e, status;
while (scanf("%d", &mode))
{
if (mode == EXIT)
{
break;
}
switch (mode)
{
case PUSH:
num = 0;
scanf("%d", &num);
while (num > 0)
{
num--;
scanf("%d", &e);
status = Push(S, e);
}
outputStack(S);
break;
case POP:
num = 0;
scanf("%d", &num);
while (num > 0)
{
num--;
status = Pop(S, &e);
if(status)
{
printf("Pop: %d\n", e);
outputStack(S);
}
else
{
printf("Pop failed\n");
}
}
break;
case GET_TOP:
status = GetTop(*S, &e);
if(status)
{
printf("GetTop: %d\n", e);
outputStack(S);
}
else
{
printf("GetTop failed\n");
}
break;
case STACK_EMPTY:
status = StackEmpty(*S);
if(status)
{
printf("The Stack is Empty\n");
}
else
{
printf("The Stack is not Empty\n");
outputStack(S);
}
break;
default:
break;
}
}
free(S);
return 0;
}