-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSEQStack.c
95 lines (82 loc) · 1.8 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
#include<stdio.h>
#define MaxSize 520
#define DT char
#define PrintControl "%c"
typedef struct SEQ_Stack
{
DT data[MaxSize];
long int vernier; /* long long int vernier=-1;*/ //TO COUNT THE ELEMENT NUMBER
// DT *Array; //For My another algorithm
}StackArray;
//ALGORITHM 1 from our textbook
//(1)
StackArray* StackInit(StackArray *s)
{
DT ch;
s->vernier=-1;
while((ch=getchar())!='$')
s->data[++(s->vernier)]=ch;
return s;
}
//(2)
void StackPush(StackArray *s,DT x)
{
if((s->vernier)+1<MaxSize)
s->data[++(s->vernier)]=x;
else
printf("Error:Stack has been full of data!");
}
//(3)
int IsEmpty(StackArray *s)
{if(s->vernier==-1) return 1;return 0;}
DT StackPop(StackArray *s)
{
DT x;
if(IsEmpty(s))
{
printf("Error:Could not Pop Empty stack!");
return 0;
}
else
{
x=s->data[(s->vernier)];
s->data[(s->vernier)]=0; /*此句可以省略,关键在下句的自减*/
(s->vernier)--;
return x;
}
}
//(4)输出stack中的元素
void StackArrayOutput(StackArray *s)
{
long int i=0;
printf("输出栈中元素:");
for(;i<=(s->vernier);i++)
printf(PrintControl,s->data[i]);
printf("\n");
}
//(5)计算stack中元素个数
long int StackCountArray(StackArray *s)
{
return (s->vernier)+1;
}
//split all functions
int main()
{
StackArray tmp,*test;
//begin to test Init function;
test=StackInit(&tmp);
StackArrayOutput(test);
printf("初始化后,元素个数是%ld\n",StackCountArray(test));
printf("\n");
//begin to test PUSH function;
StackPush(test,'Q');
printf("入栈后,元素个数是%ld\n",StackCountArray(test));
StackArrayOutput(test);
printf("\n");
//begin to test POP function;
printf("出栈的元素是");printf(PrintControl,StackPop(test));
printf("\n出栈后,元素个数是%ld\n",StackCountArray(test));
StackArrayOutput(test);
printf("\n");
return 0;
}ents here