我想在這裏寫一些堆棧實現代碼,我掛了幾個編譯錯誤/尋找一些我看到的問題的澄清。堆棧執行錯誤
特別是我有困難搞清楚什麼輸入應該在我的功能運行的主要功能。他們期望指針作爲輸入,我如何給他們指針?
我的isEmpty()和我的push()不喜歡我的輸入。 :(
這裏是我的代碼 -
#include <stdio.h>
#include <conio.h>
#define MAX 100
typedef struct {
int * st; // array of the data on the stack
int size;
int T;
int x;
int a[];
} stack;
void push(stack *st, int item) {
printf("\nT value%d",st->T);
st->T++;
if (st->T >= st->size) {
printf("\nStack size limit reached, can't push.");
return;
}
st->T++;
printf("\nT value%d",st->T);
st->a[st->T]=item;
printf("\n array value at position T %d", st->a[st->T]);
}
int pop(stack *st) {
int item;
printf("\nT value%d", st->T);
item=st->a[st->T];
printf("\n item= %d", item);
st->T--;
printf("\nT value%d", st->T);
return(item);
}
int size(stack *st){
int size_of_stack=0;
size_of_stack = st->T + 1;
printf ("\n size of stack = %d", size_of_stack);
return size_of_stack;
}
int isEmpty(stack *st)
{
if(st->T == -1)
return(1);
else
return(0);
}
int top(stack *st, stack T){
int value= st->T;
return(value);
}
void print(stack *st){
int i;
if (isEmpty(*st)==1)
printf("\n Stack is empty! \n");
else {
for (i=st->T; i>= 0 ; i--)
printf ("\n%d",st->a[i]);
}
}
int main(){
int st[MAX],item;
int T=-1;
int a[6]={10,20,30,40,50,60};
push(* st,2);
}
這裏是編譯我得到這個錯誤。
λ gcc a3.c
a3.c: In function 'print':
a3.c:60:6: error: incompatible type for argument 1 of 'isEmpty'
if (isEmpty(*st)==1)
^
a3.c:45:5: note: expected 'struct stack *' but argument is of type 'stack'
int isEmpty(stack *st)
^
a3.c: In function 'main':
a3.c:72:7: warning: passing argument 1 of 'push' makes pointer from integer without a cast
push(* st,2);
^
a3.c:14:6: note: expected 'struct stack *' but argument is of type 'int'
void push(stack *st, int item) {
你可以發佈編譯器錯誤嗎?這將有助於找出至少一些問題的原因 –
當然是,一秒 –