我想在这里写一些堆栈实现代码,我挂了几个编译错误/寻找一些我看到的问题的澄清。堆栈执行错误
特别是我有困难搞清楚什么输入应该在我的功能运行的主要功能。他们期望指针作为输入,我如何给他们指针?
我的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) {
你可以发布编译器错误吗?这将有助于找出至少一些问题的原因 –
当然是,一秒 –