在下面的函数中。我尝试使用堆栈和实现使用数组。我做了创建堆栈,推,弹出,功能。但编译时显示错误。请帮我弄清楚问题是什么?代码中的错误是什么?
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
struct stack_struct {
char a[100];
int top;
};
typedef struct stack_struct *stack;
stack charc;
stack createstack() {
stack s = (stack) malloc(sizeof(struct stack_struct));
s->top = -1;//initialize the stack
return s;
}
void push(stack s, char x) {
s->top++;
s->a[s->top] = x;
}
char pop(stack s) {
assert(s->top > 0);
char x;
x = s->a[s->top];
s->top--;
return x;
}
void printstack(stack s) {
while (s->top != -1) {
printf("%c", s->a[s->top]);
s-> top--;
}
void main() {
charc = createstack();
push(charc, 3);
printstack(charc);
pop(charc);
printstack(charc);
push(charc, 4);
printstack(charc);
push(charc, 5);
printstack(charc);
push(charc, 6);
printstack(charc);
push(charc, 7);
printstack(charc);
}
}
什么是错误和行号? –
也许不相关,但你不应该''malloc' – UnholySheep
隐藏指针'typedef'容易出错,被认为是不好的做法,使代码难以阅读... – chqrlie