c语言堆栈编程实现问题

lpf@lpf:~$ cat -n stack.c
1 #define maxlen 1000
2 #define empty -1
3 #define full (maxlen-1)
4 #include<stdio.h>
5 typedef enum boolean {false,true} boolean;
6 typedef struct stack{
7 char s[maxlen];
8 int top;
9 } stack;
10 void reset(stack *stk)
11 {
12 stk->top=empty;
13 }
14 void push(char c,stack *stk)
15 {
16 stk->top++;
17 stk->s[stk->top]=c;
18 }
19 void pop(stack *stk)
20 {
21 return(stk->s[stk->top--]);
22 }
23 void top(const stack *stk)
24 {
25 return(stk->s[stk->top]);
26 }
27 boolean empty(const stack *stk)
28 {
29 return((boolean)(stk->top==empty));
30 }
31 boolean full(const stack *stk)
32 {
33 return((boolean)(stk->top==full));
34 }
35 int main(void)
36 {
37 char str[]="my name is laura";
38 int i;
39 stack s;
40 reset(&s);
41 printf("in the string: %s\n",str);
42 for(i=0;str[i]!='\0';++i)
43 if(!full(&s))
44 push(str[[i],&s);
45 printf("from the stack: ");
46 while(!empty(&s))
47 putchar(pop(&s));
48 putchar('\n');
49 return 0;
50 }
lpf@lpf:~$ gcc stack.c
stack.c: In function ‘pop’:
stack.c:21: warning: ‘return’ with a value, in function returning void
stack.c: In function ‘top’:
stack.c:25: warning: ‘return’ with a value, in function returning void
stack.c: At top level:
stack.c:27: error: expected identifier or ‘(’ before ‘-’ token
stack.c:31: error: expected identifier or ‘(’ before numeric constant
stack.c: In function ‘main’:
stack.c:43: error: called object ‘999’ is not a function
stack.c:44: error: expected expression before ‘[’ token
stack.c:44: error: array subscript is not an integer
stack.c:44: error: too few arguments to function ‘push’
stack.c:46: error: called object ‘1’ is not a function
stack.c:47: error: invalid use of void expression
不知道为什么运行不了...我在它提示出错的地方找不到错误 - -!

修改完成:
#define MAXLEN 1000
#define EMPTY -1
#define FULL (MAXLEN-1)
#include <stdio.h>
typedef enum boolean
{
false, true
} boolean;
typedef struct stack
{
char s[MAXLEN];
int top;
} stack;
void reset(stack *stk)
{
stk->top = EMPTY;
}
void push(char c, stack *stk)
{
stk->top++;
stk->s[stk->top] = c;
}
char pop(stack *stk)
{
return (stk->s[stk->top--]);
}
void top(const stack *stk)
{
stk->s[stk->top];
}
boolean empty(const stack *stk)
{
return((boolean)(stk->top==EMPTY));
}
boolean full(const stack *stk)
{
return((boolean)(stk->top==FULL));
}
int main(void)
{
char str[] = "my name is laura";
int i;
stack s;
reset(&s);
printf("in the string: %s\n", str);
for (i = 0; str[i] != '\0'; ++i)
if(!full(&s))
push(str[i],&s);
printf("from the stack: ");
while (!empty(&s))
putchar(pop(&s));
putchar('\n');
return 0;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-07-05
你这个是自己敲进去的吗?还是在网上拷贝的,如果是在网上拷贝的,就要注意一下,里面的字符问题。最好是自己敲进去。
第2个回答  2012-07-05
21、25行错误,pop、top函数定义无返回数据,实际应该是想要返回的吧,定义不对
27,31行的错误,去掉5行的定义,或者改成其他名字的,定义不对
44行多了个'['
相似回答