在下面的函數中。我嘗試使用堆棧和實現使用數組。我做了創建堆棧,推,彈出,功能。但編譯時顯示錯誤。請幫我弄清楚問題是什麼?代碼中的錯誤是什麼?
#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