顺序栈-数组实现
2017-05-27 18:19
1186 查看
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");
}
}
在main函数执行
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。
相关文章推荐
- 顺序栈(数组实现)
- Java实现基于数组的顺序栈
- 《面向对象程序设计与VC++实现》--liwei_2_2(以递增顺序对数组排序)
- 在BCB中使用向量实现控件数组
- 利用数组实现数据库记录的批量录入
- ZT:C++Builder中实现控件数组的功能
- vb6中用zlib.dll实现压缩/解压缩字节数组
- 用字符串数组实现的大数运算
- C#实现数组的排序
- 用C++二级指针实现像C#一样的动态交错数组
- JavaScript中实现数组的排序、乱序和搜索
- asp中利用数组实现数据库记录的批量录入方法
- C++实战:一个轻型数组类的实现
- 实现对中文字符串数组按照音序排列
- 数据结构+C++_№4 抽象数组的实现 代码补充及更正
- 【库】c实现图(数组表示法)
- BCB高手进阶(十)如何实现控件数组
- 【库】c实现数组
- 数组下标是字符串的实现
- C# 没有动态数组,实现动态数组 && local copy(dll)