-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqStack.c
More file actions
106 lines (84 loc) · 1.49 KB
/
SqStack.c
File metadata and controls
106 lines (84 loc) · 1.49 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*************************************************************************
> File Name: SqStack.c
> Author: moyu
> Mail:
> Created Time: 2020年04月13日 星期一 09时35分58秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 //栈空间容量
typedef int data_t;
typedef struct
{
data_t arr[MAXSIZE];
int top; //栈顶指针,用来遍历和返回长度
}SqStack;
//创建
SqStack* create()
{
SqStack* S = (SqStack*)malloc(sizeof(SqStack));
if (S == NULL)
{
perror("malloc");
return NULL;
}
S->top = -1; //栈顶指针初始化-1,第一个数据下标为0
return S;
}
//清空
int clear(SqStack* S)
{
S->top = -1;
return 1;
}
//求长度
int length(SqStack* S)
{
return (S->top + 1); //初始化为-1,长度要加1
}
//入栈
int push(SqStack* S, data_t data)
{
//判断是否栈满
if (S->top + 1 == MAXSIZE)
{
puts("Stack full");
return -1;
}
++S->top; //指针向上移动,指向新值
S->arr[S->top] = data; //给新值赋值
return 1;
}
//出栈
int pop(SqStack* S)
{
//判断栈是否为空
if (S->top == -1)
{
puts("Stack empty");
return -1;
}
--S->top;
return 1;
}
//打印
void show(SqStack* S)
{
int i = 0;
while (i <= S->top)
{
printf("%d ", S->arr[i++]);
}
printf("\n");
}
int main()
{
SqStack* S = create();
for (int i = 1; i <= 10; ++i)
push(S, i);
show(S);
printf("%d\n", length(S));
pop(S);
show(S);
printf("%d\n", length(S));
}