-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path顺序栈.cpp
121 lines (109 loc) · 1.55 KB
/
顺序栈.cpp
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
#include <iostream>
#define MAXSIZE 100
using namespace std;
typedef struct Stack
{
int *top;
int *base;
int StackSize;
}SqStack;
void CreateStack(SqStack &S);
void Push(SqStack &S, int data);
int Pop(SqStack &S);
int IsEmpty(SqStack S);
int StackLength(SqStack S);
void ClearStack(SqStack S);
void DestroyStack(SqStack &S);
void Print(SqStack S);
int main()
{
SqStack S; //创建栈
CreateStack(S);
cout << "入栈中..." << endl;
Push(S, 1); //入栈
Push(S, 2);
Push(S, 3);
cout << "出栈中..." << endl;
Pop(S);
Print(S); //打印
cout << "清空顺序栈..." << endl;
ClearStack(S);
cout << "入栈中..." << endl;
Push(S, 5);
Push(S, 6);
Push(S, 7);
Print(S);
DestroyStack(S);
return 0;
}
//创建顺序栈
void CreateStack(SqStack &S)
{
S.base = new int[MAXSIZE];
S.top = S.base;
S.StackSize = MAXSIZE;
}
//入栈
void Push(SqStack &S, int data)
{
if (S.top - S.base == S.StackSize)
return;
else
{
*S.top = data;
S.top++;
}
}
//出栈
int Pop(SqStack &S)
{
if (S.base == S.top)
return -1;
else
{
--S.top;
return *S.top;
}
}
//判断栈空
int IsEmpty(SqStack S)
{
if (S.base == S.top)
return 1;
else
return 0;
}
//求顺序栈长度
int StackLength(SqStack S)
{
return S.top - S.base;
}
//清空栈
void ClearStack(SqStack S)
{
if (S.base)
S.top = S.base;
else
return;
}
//销毁栈
void DestroyStack(SqStack &S)
{
if (S.base)
{
delete[]S.base;
S.StackSize = 0;
S.base = S.top = NULL;
}
}
//打印
void Print(SqStack S)
{
--S.top;
while (S.top != S.base - 1)
{
cout << *S.top << " -> ";
S.top--;
}
cout << "NULL" << endl;
}