-
Notifications
You must be signed in to change notification settings - Fork 1
/
012_10.1_StackLinkList.c
89 lines (61 loc) 路 1.21 KB
/
012_10.1_StackLinkList.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Stack {
int nValue;
struct Stack *pNext;
}Stack, *PStack;
PStack CreateStack() {
PStack pStack = (PStack)malloc(sizeof(Stack));
pStack->nValue = 0;
pStack->pNext = NULL;
return pStack;
}
bool IsEmpty(PStack pStack) {
if (NULL == pStack) {
printf("鏍堟湭鍒涘缓鎴愬姛!\n");
}
return pStack->pNext == NULL;
}
PStack Push(PStack pStack, int nValue) {
if(NULL == pStack) {
printf("鏍堟湭鍒涘缓鎴愬姛!\n");
return NULL;
}
PStack pTmp = (PStack)malloc(sizeof(Stack));
pTmp->nValue = nValue;
pTmp->pNext = pStack->pNext;
pStack->pNext = pTmp;
return pStack;
}
int Pop(PStack pStack) {
if(NULL == pStack) {
printf("鏍堟湭鍒涘缓鎴愬姛!\n");
}
PStack pTmp = pStack->pNext;
pStack->pNext = pTmp->pNext;
int nValue = pTmp->nValue;
free(pTmp);
pTmp = NULL;
return nValue;
}
void Destroy(PStack pStack) {
while (!IsEmpty(pStack)) {
Pop(pStack);
}
free(pStack);
pStack = NULL;
}
void main()
{
PStack pStack = CreateStack();
for (int i = 0; i < 15; i++)
{
pStack = Push(pStack, i);
}
while (!IsEmpty(pStack))
{
printf("%d ", Pop(pStack));
}
Destroy(pStack);
}