-
Notifications
You must be signed in to change notification settings - Fork 0
/
栈--顺序栈指针形式.c
108 lines (95 loc) · 1.82 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#define ERROR -2
#define OVERFLOW 2
#define true 1
#define false 0
#define STACK_INIT_SIZE 10
#define STACK_SIZE 5
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
//初始化一个空栈S
int InitStack(SqStack *S)
{
S->base = malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S->base) exit(OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return true;
}
//判断栈是否空,空返回true
int StackEmpty(SqStack S)
{
if(!S.base) exit(OVERFLOW);
if(S.top==S.base){
return true;
}
else
return false;
}
//进栈,若栈未满
int Push(SqStack *S,ElemType e)
{
if(S->top-S->base==S->stacksize){
S->base =realloc(S->base,(S->stacksize+STACK_SIZE)*sizeof(ElemType));
if(!S->base) exit(OVERFLOW);
S->stacksize+=STACK_SIZE;
}
S->top++;
*S->top =e;
//e=*(S->top++);
//printf("s->top=%d\n",*S->top);
return true;
}
//出栈,若栈不为空
int Pop(SqStack *S,ElemType *e)
{
if(!StackEmpty(*S)){
*e = *S->top;
S->top--;
//*e =*(S->top--);
}
return true;
}
//读栈顶元素,并返回元素
int GetTop(SqStack S,ElemType *e)
{
if(!StackEmpty(S)){
*e =*S.top;
printf("top=%d\n",*e);
}
return true;
}
//销毁栈,释放栈占用的空间
int DestoryStack(SqStack S)
{
if(!S.base) exit(ERROR);
free(S.base);
return true;
}
int main()
{
ElemType e;
SqStack S;
InitStack(&S);
StackEmpty(S);
Push(&S,1);
Push(&S,2);
Push(&S,3);
StackEmpty(S);
GetTop(S,&e);
Pop(&S,&e);
printf("del=%d\n",e);
Pop(&S,&e);
printf("del=%d\n",e);
Pop(&S,&e);
printf("del=%d\n",e);
Pop(&S,&e);
DestoryStack(S);
GetTop(S,&e);
}