-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdemo_array_stack_1.c
More file actions
172 lines (136 loc) · 3.09 KB
/
demo_array_stack_1.c
File metadata and controls
172 lines (136 loc) · 3.09 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "demo_array_stack_1.h"
// 创建并初始化顺序栈
arrayStack *create(int size) {
arrayStack *stack = (arrayStack *)malloc(sizeof(arrayStack));
if (stack == NULL) {
return NULL;
}
stack->size = size;
stack->pos = -1;
stack->array = (int *)malloc(sizeof(int) * size);
if (stack->array == NULL) {
free(stack);
return NULL;
}
return stack;
}
// 销毁顺序栈
void destory(arrayStack *stack) {
if (stack == NULL) {
return;
}
if (stack->array == NULL) {
free(stack->array);
}
free(stack);
return;
}
// 入栈
int push(arrayStack *stack, int data) {
if (stack == NULL) {
return -1;
}
if (_is_full(stack)) {
return -1;
}
stack->pos++;
stack->array[stack->pos] = data;
return 0;
}
// 入栈(可扩容)
int push_dilatation(arrayStack *stack, int data) {
if (stack == NULL) {
return -1;
}
// 如果栈不满,直接插入
if (!_is_full(stack)) {
return push(stack, data);
}
// 如果栈已经满,申请内存
int *ptmp = (int *)malloc(2 * sizeof(int) * stack->size);
if (ptmp == NULL) {
return -1;
}
memcpy(ptmp, stack->array, stack->size * sizeof(int));
free(stack->array);
stack->array = ptmp;
stack->size = 2 * stack->size;
stack->pos++;
stack->array[stack->pos] = data;
}
// 出栈
int pop(arrayStack *stack) {
int data = 0;
if (_is_empty(stack)) {
return -1;
}
data = stack->array[stack->pos];
stack->pos--;
return data;
}
void print(arrayStack *stack) {
if (stack == NULL) {
printf("\r\n Stack Is NULL.");
return;
}
if (_is_empty(stack)) {
printf("\r\n Stack is empty");
return;
}
printf("\r\n Stack size = %d, pos = %d", stack->size, stack->pos);
int i = 0;
for (i = 0; i <= stack->pos; i++) {
printf("\r\n array[%d] = %d", i, stack->array[i]);
}
printf("\r\n");
}
void test_stack() {
arrayStack *stack = NULL;
stack = create(4);
if (stack == NULL) {
printf("\n Stack is NULL \n");
}
int i = 1;
for (i = 1; i < 5; i++) {
if (push(stack, i) != 0) {
printf("\r\n %d failed to push the Stack \r\n", i);
}
}
print(stack);
push(stack, 5);
pop(stack);
print(stack);
destory(stack);
}
void test_stack_dilatation()
{
arrayStack *stack = NULL;
stack = create(4);
if (stack == NULL) {
printf("\n Stack is NULL \n");
}
int i = 1;
for (i = 1; i < 5; i++) {
if (push(stack, i) != 0) {
printf("\r\n %d failed to push the Stack \r\n", i);
}
}
print(stack);
push(stack, 5);
pop(stack);
print(stack);
for (i = 5; i < 10; i++) {
if (push_dilatation(stack, i) != 0) {
printf("\r\n %d failed to push the Stack \r\n", i);
}
}
print(stack);
destory(stack);
}
int main () {
test_stack_dilatation();
return 0;
}