-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeqStack.c
More file actions
89 lines (77 loc) · 1.34 KB
/
SeqStack.c
File metadata and controls
89 lines (77 loc) · 1.34 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
86
87
88
89
//
// Created by ys on 2019/4/11.
//
#include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"
//顺序栈的初始化
void SeqStackInit(SeqStack *S)
{
S->top = -1;
}
//顺序栈的判空操作
int SeqStackEmpty(SeqStack S)
{
if(S.top == -1)
{
return true;
} else
{
return false;
}
}
//入栈操作
void SeqStackPush(SeqStack *S, ElemType x)
{
if(S->top == MAXSIZE - 1)
{
printf("栈已满\n");
exit(0);
}
S->top++;
S->data[S->top] = x;
}
//出栈操作
ElemType SeqStackPop(SeqStack *S)
{
if(SeqStackEmpty(*S))
{
printf("栈已空,无法进行出栈操作\n");
exit(0);
} else
{
ElemType x = S->data[S->top];
S->top--;
return x;
}
}
//取栈顶的元素
ElemType SeqStackGet(SeqStack S)
{
if(SeqStackEmpty(S))
{
printf("栈已空,无法获取栈顶元素\n");
return 0;
}
return S.data[S.top];
}
//测试顺序栈的操作
void testSeqStack()
{
SeqStack S;
SeqStackInit(&S);
int b = SeqStackEmpty(S);
SeqStackPush(&S, 1);
b = SeqStackEmpty(S);
ElemType x = SeqStackPop(&S);
for (int i = 0; i < 49; ++i) {
SeqStackPush(&S, i);
}
x = SeqStackGet(S);
x = SeqStackPop(&S);
x = SeqStackPop(&S);
x = SeqStackPop(&S);
x = SeqStackPop(&S);
x = SeqStackPop(&S);
x = SeqStackPop(&S);
}