-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqStack.cpp
More file actions
219 lines (158 loc) · 3.7 KB
/
SqStack.cpp
File metadata and controls
219 lines (158 loc) · 3.7 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/********************************************************************
Time: 2015/09/02
Filename: SqStack
Author: dinglj
Purpose: 顺序栈的实现
*********************************************************************/
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
#define MAXSIZE 20 // 最大的栈空间大小
// 元素定义
typedef struct
{
int data;
}ElemType;
typedef struct
{
ElemType e[MAXSIZE];
int top; // 栈顶指针
}SqStack;
//////////////////////////////////////////////////////////////////////
// 基本函数申明
bool InitStack(SqStack &S);
bool DestroyStack(SqStack &S);
bool ClearStack(SqStack &S);
bool isFull(SqStack S);
bool isEmpty(SqStack S);
bool Push(SqStack &S, ElemType e);
bool Pop(SqStack &S, ElemType &e);
bool GetTop(SqStack S, ElemType &e);
//////////////////////////////////////////////////////////////////////
/********************************************************************
Method: InitStack
Parameter:
Returns:
Purpose: 初始化空栈
*********************************************************************/
bool InitStack(SqStack &S)
{
S.top = -1; // top = -1表示栈空
return true;
}
/********************************************************************
Method: ClearStack
Parameter:
Returns:
Purpose: 清空栈
*********************************************************************/
bool ClearStack(SqStack &S)
{
S.top = -1;
return true;
}
bool DestroyStack(SqStack &S)
{
ClearStack(S);
return true;
}
/********************************************************************
Method: isEmpty
Parameter:
Returns:
Purpose: 判断是否为空栈
*********************************************************************/
bool isEmpty(SqStack S)
{
return S.top == -1;
}
/********************************************************************
Method: isFull
Parameter:
Returns:
Purpose: 顺序栈入栈时需要判断栈是否满
*********************************************************************/
bool isFull(SqStack S)
{
return S.top == MAXSIZE - 1;
}
/********************************************************************
Method: Push
Parameter:
Returns:
Purpose: 入栈
*********************************************************************/
bool Push(SqStack &S, ElemType e)
{
if (isFull(S))
{
return false;
}
S.top++;
S.e[S.top].data = e.data;
return true;
}
/********************************************************************
Method: Pop
Parameter:
Returns:
Purpose: 出栈
*********************************************************************/
bool Pop(SqStack &S, ElemType &e)
{
if (isEmpty(S))
{
return false;
}
e.data = S.e[S.top].data;
S.top--;
return true;
}
/********************************************************************
Method: GetTop
Parameter:
Returns:
Purpose: 获取栈顶元素
*********************************************************************/
bool GetTop(SqStack S, ElemType &e)
{
if (isEmpty(S))
{
return false;
}
e.data = S.e[S.top].data;
return true;
}
/********************************************************************
Method: OperationTest
Parameter:
Returns:
Purpose: 测试函数
*********************************************************************/
bool OperationTest()
{
SqStack S;
InitStack(S);
ElemType e;
e.data = 1;
Push(S, e);
e.data = 2;
Push(S, e);
Pop(S, e);
printf("%d\n", e.data);
e.data = 3;
Push(S, e);
Pop(S, e);
printf("%d\n", e.data);
return true;
}
/********************************************************************
Method: main
Parameter:
Returns:
Purpose:
*********************************************************************/
int main()
{
OperationTest();
return true;
}