forked from xiufengcheng/DATASTRUCTURE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkStack.h
More file actions
66 lines (60 loc) · 1.56 KB
/
LinkStack.h
File metadata and controls
66 lines (60 loc) · 1.56 KB
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
# include "LinkList.h"
typedef LinkList LinkStack; // 链栈的结点结构和单链表相同
void InitStack_L(LinkStack &S)
{
S=NULL;
}// InitStack_L
int StackLength_L(LinkStack S)
{
int k=0;
LinkStack p=S;
while(p)
{ k++;
p=p->next; // 访问下一个结点。
}
return k;
}// StackLength_L
bool Push_L( LinkStack &S, ElemType e)
{ //在链栈的栈顶插入元素e
LinkStack p;
if((p=(LNode *)malloc(sizeof(LNode)))==NULL) return false; // 存储分配失败
p->data=e;
p->next=S; // 插入新的栈顶元素
S=p; // 修改栈顶指针
return true;
}// Push_L
bool Pop_L( LinkStack &S, ElemType &e)
{ // 删除链栈栈顶元素,并让e返回其值
LinkStack p;
if(S) // 栈非空
{ p=S;S=S->next; // 修改栈顶指针
e=p->data; // 元素e返回其值
free(p); // 释放结点空间
return true;
}
else return false; // 栈空,出栈失败
}// Pop_L
bool GetTop_L(LinkStack S,ElemType &e)
{
if(S) // 栈非空
{ e=S->data; // 元素e返回其值
return true;
}
else return false; // 栈空,取栈顶元素失败
}// GetTop_L
bool StackEmpty_L(LinkStack S)
{
if(!S) return true;
else return false;
}// StackEmpty_L
void DestroyStack_L(LinkStack &S )
{
LinkStack p,p1;
p=S;
while(p)
{ p1=p;
p=p->next;
free(p1); // 释放p1所指的空间
}
S=NULL; // S置空
}// DestroyStack_L