-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
53 lines (46 loc) · 1.03 KB
/
Stack.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
#include <stdio.h>
#include <cstdlib>
#define INIT_SIZE 100
#define INCREMENT 10
typedef unsigned int ELEM;
typedef struct{
ELEM *top;
ELEM *base;
int size;
}Stack;
void InitStack(Stack &S){
S.base = (ELEM *)malloc(INIT_SIZE * sizeof(ELEM));
if(!S.base){exit(0);}
S.top = S.base;
S.size = INIT_SIZE;
}
void GetTop(Stack &S, ELEM &e){
if(S.top == S.base){
printf("stack is empty!\n");
return ;
}
e = *(S.top - 1);
}
void Push(Stack &S, ELEM e){
if(S.top - S.base >= S.size){
S.base = (ELEM *)realloc(S.base, (S.size + INCREMENT) * sizeof(ELEM));
if(!S.base)exit(0);
S.top = S.base + S.size;//deletable
S.size += INCREMENT;
}
*S.top++ = e;//指针加减
}
void Pop(Stack &S, ELEM &e){
if(S.top == S.base)return;
e = *--S.top;
}
int main(){
Stack test;
InitStack(test);
Push(test, 1);
Push(test, 2);
Push(test, 3);
ELEM e;
Pop(test, e);
printf("%d\n", e);
}