1 #include <stdio.h> /*数组顺序栈*/
2 #include <stdlib.h>
3 #define TURE 1
4 #define FALSE -1 /**/
5 #define Stack_Size 50
6
7 typedef int StackDateType;
8
9 struct stack
10 {
11 StackDateType Date[Stack_Size];
12 int top;
13 };
14 typedef struct stack* SeqStack;
15
16 int IsEmpty(SeqStack S); /*判断栈是否为空*/
17 int IsFull(SeqStack S); /*判断栈是否溢出了*/
18 int Push(SeqStack S, StackDateType x); /*进栈*/
19 int Pop(SeqStack S, StackDateType *x); /*出栈*/
20 int GetTop(SeqStack S, StackDateType *x); /*读出栈顶的值*/
21 void print(struct stack S); /*打印出顺序栈的值*/
22 void InitStack(SeqStack S); /*初始化*/
23
24 int main()
25 {
26 struct stack S;
27 int i, ret;
28 StackDateType x;
29 InitStack(&S);
30
31 for (i = 1; i <= 10; i++)
32 {
33 Push(&S, i);
34 }
35 print(S);
36
37 Pop(&S, &x);
38 printf("Pop value = %d\n", x);
39 print(S);
40
41 GetTop(&S, &x);
42 printf("Top value = %d\n", x);
43 print(S);
44
45 return 0;
46 }
47
48 void print(struct stack S) /*void pint(SeqStack S) lead to S->top changing形参不可以为指针*/
49 {
50 if (S.top == -1)
51 {
52 printf("none value\n");
53 }
54 else
55 {
56 while (S.top != -1)
57 {
58 printf("%d ", S.Date[S.top]);
59 S.top--;
60 }
61 printf("\n");
62 }
63 }
64
65 void InitStack(SeqStack S)
66 {
67 S->top = -1;
68 }
69
70 int IsEmpty(SeqStack S)
71 {
72 if (-1 == S->top)
73 {
74 return TURE;
75 }
76 else
77 {
78 return FALSE;
79 }
80 }
81
82 int IsFull(SeqStack S)
83 {
84 if (S->top == Stack_Size - 1)
85 {
86 return TURE;
87 }
88 else
89 {
90 return FALSE;
91 }
92 }
93
94 int Push(SeqStack S, StackDateType x)
95 {
96 if (1 == IsFull(S)) /*if (IsEmpty(S) is error,'-1' 亦是真 或者#define 0 FALSE*/
97 {
98 printf("Stack Full\n");
99 }
100 else
101 {
102 S->top++;
103 S->Date[S->top] = x;
104 return TURE;
105 }
106 }
107
108 int Pop(SeqStack S, StackDateType *x)
109 {
110 if (1 == IsEmpty(S))
111 {
112 printf("stack is empty");
113 return FALSE;
114 }
115 else
116 {
117
118 *x = S->Date[S->top];
119 S->top--;
120 return TURE;
121 }
122 }
123
124 int GetTop(SeqStack S, StackDateType *x)
125 {
126 if (1 == IsEmpty(S))
127 {
128 return FALSE;
129 }
130 else
131 {
132 *x = S->Date[S->top];
133 return TURE;
134 }
135 }
[root@localhost 1]# vim stack.c
[root@localhost 1]# gcc stack.c
[root@localhost 1]# ./a.out
10 9 8 7 6 5 4 3 2 1
Pop value = 10
9 8 7 6 5 4 3 2 1
Top value = 9
9 8 7 6 5 4 3 2 1
写该程序中出了两个脑残的错误:
(1) 在写print函数时起初形参使用了指针:
void print(struct stack *S)
{
if (S->top == -1)
{
printf("none value\n");
}
else
{
while (S->top != -1)
{
printf("%d ", S->Date[S->top]);
S->top--;
}
printf("\n");
}
}
for (i = 1; i <= 10; i++)
Push(&, i);
print(&S);
没错的确可以正儿八经的输出:
10 9 8 7 6 5 4 3 2 1
完全没毛病。
分析:形参为指针,值可以原样输出,但是每执行一次print,main函数中的S->top就会指向栈底(S->top == -1),这样会出现一系列逻辑错误;
结论:print函数形参为struct stack S 而不是struct stack * S。
(2)毛病if(表达式),当表达式 等于-1时,也为真,
只有表达式等于0时才为假。(这个error完全因为宏定义#define -1 FALSE坑了自己)。
例如:程序96,110行 应该是if (1 == IsFull(S)) 和if (1 == IsEmpty(S))而不是if (IsEmpty(S))和if (IsFull(S));
由于#define -1 FALSE,执行语句 if (IsEmpty(S)),经过IsEmpty函数调用结果为:
if(-1)
{
printf("stack is empty\n ");
}由于-1为真,就一直显示stack isempty。