-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkStack.c
86 lines (77 loc) · 1.46 KB
/
LinkStack.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
/*
* =====================================================================================
*
* Filename: LinkStack.c
*
* Description:
*
* Version: 1.0
* Created: 11/29/2014 09:06:28 PM
* Revision: none
* Compiler: gcc
*
* Author: 张世龙 (mn), [email protected]
* Company: free
*
* =====================================================================================
*/
#include "Public.h"
#include "LinkStack.h"
void InitStack(LinkStack *s)
{
assert(s);
s->count = 0;
s->top = NULL;
}
void DestoryStack(LinkStack *s)
{
assert(s);
StackNode *stackPtr = s->top;
while(stackPtr){
StackNode *tmpStackPtr = stackPtr->next;
free(stackPtr);
stackPtr = tmpStackPtr;
}
s->top = NULL;
s->count = 0;
}
void ClearStack(LinkStack *s);
int StackEmpty(LinkStack *s)
{
assert(s);
return s->count == 0;
}
int GetTop(LinkStack *s,ElemType *e)
{
if(s->count == 0)
return ERROR;
*e = s->top->data;
return SUCCESS;
}
void Push(LinkStack *s,ElemType *e)
{
assert(s || e);
StackNode *nodePtr = (StackNode*)malloc(sizeof(StackNode));
assert(nodePtr);
nodePtr->data = *e;
nodePtr->next = s->top;
s->top = nodePtr;
s->count++;
}
int Pop(LinkStack *s,ElemType *e)
{
assert(s || e);
if(s->count == 0)
return ERROR;
*e = s->top->data;
StackNode *nodePtr = s->top;
s->top = s->top->next;
s->count--;
free(nodePtr);
return SUCCESS;
}
int StackLength(LinkStack *s)
{
assert(s);
return s->count;
}