blob: e47dd81ecf85dc4fa0c8e8cac13f0bbb77a00914 (
plain)
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
|
#include "stack.h"
bool InitStack(Stack * s) {
*s = NULL;
return true;
}
bool DestroyStack(Stack * s) {
Stack p;
while (*s) {
p = *s;
*s = (*s)->next;
free (p);
}
return true;
}
bool Push(Stack * s, ElemType n) {
Stack p = (stack *) malloc (sizeof(stack));
if (!p) {
fprintf(stderr,"No enough memory\n");
return false;
}
p->data = n;
p->next = *s;
*s = p;
return true;
}
bool Pop(Stack * s, ElemType * n) {
if (!*s) {
fprintf(stderr,"No element in stack!\n");
return false;
}
*n = (*s)->data;
Stack p = *s;
*s = (*s)->next;
free(p);
return true;
}
ElemType GetTop(Stack * s) {
if (*s)
return (*s)->data;
return -1;
}
bool StackIsEmpty (Stack * s) {
if (!*s)
return true;
return false;
}
|