-
Notifications
You must be signed in to change notification settings - Fork 0
/
seqstack.c
175 lines (168 loc) · 3.53 KB
/
seqstack.c
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
173
174
175
#include"seqstack.h"
//初始化
void SeqStackInit(SeqStack* seq)
{
seq->capacity = 1000;
seq->data = (SeqStackType*)malloc(seq->capacity*sizeof(SeqStackType));
seq->size = 0;
}
//清空栈
void SeqStackDestroy(SeqStack* seq)
{
free(seq->data);
seq->size = 0;
seq->capacity = 1000;
}
//如果当前栈已经满了,就要动态扩容
SeqStackType* SeqStackExpand(SeqStack* seq)
{
if(seq==NULL){
//非法输入
return NULL;
}
SeqStackType* newseq = (SeqStackType*)malloc(2*(seq->capacity)+1);
size_t i = 0;
for(;i<seq->size;i++){
newseq[i] = seq->data[i];
}
seq->capacity = 2*(seq->capacity)+1;
free(seq->data);
return newseq;
}
//入栈(尾插)
void SeqStackPush(SeqStack* seq,SeqStackType value)
{
if(seq==NULL){
//非法输入
return;
}
if(seq->size>=seq->capacity){
seq->data = SeqStackExpand(seq);
}
seq->data[seq->size] = value;
seq->size++;
}
//出栈(尾删)
void SeqStackPop(SeqStack* seq)
{
if(seq==NULL){
//非法输入
return;
}
if(seq->size==0){
//空栈
return;
}
seq->size--;
}
//取栈顶元素
int SeqStackFindTop(SeqStack* Seq,SeqStackType* value)
{
if(Seq==NULL)
{
//非法输入
return 0;
}
if(Seq->size==0){
//空栈
return 0;
}
*value = Seq->data[Seq->size-1];
return 1;
}
/*
void SeqStackPrintChar(SeqStack* seq,const char* msg)
{
if(seq==NULL){
//非法输入
return;
}
size_t i = 0;
for(;i<seq->size;i++){
printf("[%c] ",seq->data[i]);
}
printf("\n");
printf("%s\n",msg);
}
*/
/*
void SeqStackAssgin(SeqStack* cur_path, SeqStack* short_path)
{
SeqStackDestroy(short_path);
short_path->size = cur_path->size;
short_path->capacity = cur_path->capacity;
short_path->data = (SeqStackType*)malloc(short_path->capacity * sizeof(SeqStackType));
size_t i = 0;
for(; i<cur_path->size; i++)
{
short_path->data[i] = cur_path->data[i];
}
return;
}
*/
//通常栈是不允许进行遍历的
//但是如果是进行调试或者测试,这个是例外
//因此在这里的函数虽然进行了遍历,但是仅用于调试
/*
* void SeqStackDebugPrint(SeqStack* stack, const char* msg)
{
printf("%s\n",msg);
size_t i = 0;
for(;i < stack->size;i++)
{
printf("[%d %d]\n",stack->data[i].row,stack->data[i].col);
}
}
*/
/*
void TestInit()
{
TESTHEAD;
SeqStack seq;
SeqStackInit(&seq);
}
void TestPush()
{
TESTHEAD;
SeqStack seq;
SeqStackInit(&seq);
SeqStackPush(&seq,'a');
SeqStackPush(&seq,'b');
SeqStackPush(&seq,'c');
SeqStackPush(&seq,'d');
SeqStackPrintChar(&seq,"入栈四个元素");
}
void TestPop()
{
TESTHEAD;
SeqStack seq;
SeqStackInit(&seq);
SeqStackPush(&seq,'a');
SeqStackPush(&seq,'b');
SeqStackPush(&seq,'c');
SeqStackPush(&seq,'d');
SeqStackPrintChar(&seq,"入栈四个元素");
SeqStackPop(&seq);
SeqStackPop(&seq);
SeqStackPrintChar(&seq,"出栈两个元素");
}
void TestGetTop()
{
TESTHEAD;
SeqStack seq;
SeqStackInit(&seq);
SeqStackPush(&seq,'a');
SeqStackPush(&seq,'b');
SeqStackPush(&seq,'c');
SeqStackPush(&seq,'d');
SeqStackPrintChar(&seq,"入栈四个元素");
SeqStackType value;
int ret = SeqStackFindTop(&seq,&value);
printf("ret: except: 1 , real: %d \n",ret);
printf("value: except: d, real: %c \n",value);
}
int main()
{
TestPop();
return 0;
}*/