-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStack.c
More file actions
85 lines (79 loc) · 1.27 KB
/
LinkedStack.c
File metadata and controls
85 lines (79 loc) · 1.27 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by ys on 2019/4/11.
//
#include "LinkedStack.h"
#include "../tools.h"
//初始化
void Init(LinkedStack **S)
{
(*S) = (LinkedStack *)malloc(sizeof(LinkedStack));
(*S)->top = NULL;
}
//判空操作
int Empty(LinkedStack *S)
{
if(S->top == NULL)
{
return true;
} else
{
return true;
}
}
//入栈操作
void Push(LinkedStack *S, ElemType x)
{
LStackNode *ls;
ls = (LStackNode *)malloc(sizeof(LStackNode));
if(ls == NULL)
{
printf("申请空间失败\n");
exit(0);
}
ls->data = x;
ls->next = S->top;
S->top = ls;
}
//出栈操作
ElemType Pop(LinkedStack *S)
{
LStackNode *p;
ElemType x;
if(S->top ==NULL)
{
printf("栈空不能进行出栈操作\n");
exit(0);
}
x = S->top->data;
p = S->top;
S->top = S->top->next;
free(p);
return x;
}
//获取栈顶元素
ElemType Get(LinkedStack *S)
{
if(S->top ==NULL)
{
printf("栈空,无法进行取元素操作");
exit(0);
}
return S->top->data;
}
//测试
void testLinkedList()
{
LinkedStack *S;
Init(&S);
ElemType x;
Push(S, 12);
Push(S,13);
Push(S,123);
Pop(S);
x = Pop(S);
printf("%d\n",x);
x = Pop(S);
printf("%d\n",x);
x = Pop(S);
printf("%d\n",x);
}