forked from Vencenter/C_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path栈_链栈_.cpp
89 lines (81 loc) · 1.23 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
#include<stdlib.h>
#include<stdio.h>
#define SElemType int
#define STACK_INIT_SIZE 50
typedef struct stack
{
SElemType *top,*base;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType ));
if(!S.base)
{
exit(-1);
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}
int Push(SqStack &S,int e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,STACK_INIT_SIZE*sizeof(SElemType ));
if(!S.base)
{
exit(-1);
}
S.top=S.base+S.stacksize;
S.stacksize+=S.stacksize;
}
*S.top++=e;
printf("----> %d\n",e);
return 0;
}
int GetTop(SqStack S)
{
if(S.top==S.base)
{
return -1;
}
return *(S.top-1);
}
int StackLength(SqStack S)
{
return S.top-S.base;
}
int StackEmpty(SqStack S)
{
if(S.top==S.base)
{
return 1;
}
return 0;
}
int Pop(SqStack &S,int &e)
{
if(S.top==S.base)
{
return -1;
}
e=*--S.top;
printf("<---- %d\n",e);
return e;
}
int main()
{
SqStack p;
int i,e;
InitStack(p);
for(i=0;i<10;i++)
{
Push(p,i);
}
printf("stack_len->%d,stack_top->%d\n",StackLength(p),GetTop(p));
while(not StackEmpty(p))
{
Pop(p,e);
}
return 0;
}