C 从堆栈弹出时输出错误
我用堆栈制作了一个程序,但是我得到的输出有点错误。我得到的输出,有我需要的答案,也有2个不期望的值 以下是我所做的:C 从堆栈弹出时输出错误,c,structure,stack,C,Structure,Stack,我用堆栈制作了一个程序,但是我得到的输出有点错误。我得到的输出,有我需要的答案,也有2个不期望的值 以下是我所做的: #include <stdio.h> #include<malloc.h> #define MAX 180 struct cakes{ int spongecake; int meringue; int chocalate; int red_velvet; struct cakes *next; }; struc
#include <stdio.h>
#include<malloc.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct cakes *next;
};
struct stack{
int top;
int cake[10];
};
int isFull(struct stack *);
int isEmpty(struct stack *);
void push(struct stack *,int);
int pop(struct stack *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes));
cake_order(head); //this is a seperate function, it works perfectly.
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(head->next);
}
int isFull(struct stack *q)
{
if(q->top==10-1)
{
return 1;
}
else
{
return 0;
}
}
void push(struct stack *sptr,int x)
{
if(!isFull(sptr))
{
sptr->top++;
sptr->cake[sptr->top]=x;
}
}
int isEmpty(struct stack *q)
{
if(q->top==-1)
{
return 1;
}
else
{
return 0;
}
}
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
}
void order_out(struct cakes *theorder)
{
struct stack s;
s.top=-1;
int k=0;
int i=0;
int p=0;
int r=0;
int value1,value2;
int items[10];
theorder->spongecake=1;
theorder->meringue=2;
theorder->chocalate=3;
theorder->red_velvet=4;
for(;i<10;i++)
{
push(&s,theorder->spongecake);
push(&s,theorder->meringue);
push(&s,theorder->chocalate);
push(&s,theorder->red_velvet);
}
while(!isEmpty(&s))
{
printf("\n%d",pop(&s));
}
}
#包括
#包括
#定义最大值180
结构蛋糕{
海绵蛋糕;
蛋白酥皮;
内巧克力;
内红色天鹅绒;
结构蛋糕*下一步;
};
结构堆栈{
int top;
int-cake[10];
};
int是完整的(结构堆栈*);
int-isEmpty(结构堆栈*);
void push(结构堆栈*,int);
int-pop(结构堆栈*);
无效订单(结构蛋糕*);
main()
{
结构蛋糕*头;
head=(结构蛋糕*)malloc(结构蛋糕的尺寸);
cake_order(head);//这是一个单独的函数,它工作得很好。
head->next=(结构蛋糕*)malloc(sizeof(结构蛋糕));
下单(头->下一步);
}
int isFull(结构堆栈*q)
{
如果(q->top==10-1)
{
返回1;
}
其他的
{
返回0;
}
}
无效推送(结构堆栈*sptr,int x)
{
如果(!isFull(sptr))
{
sptr->top++;
sptr->蛋糕[sptr->顶部]=x;
}
}
int isEmpty(结构堆栈*q)
{
如果(q->top==-1)
{
返回1;
}
其他的
{
返回0;
}
}
int pop(结构堆栈*sptr)
{
int-ret=NULL;
如果(!isEmpty(sptr))
{
ret=sptr->蛋糕[sptr->顶部];
sptr->顶部--;
返回ret;
}
}
作废订单(结构蛋糕*订单)
{
结构栈;
s、 top=-1;
int k=0;
int i=0;
int p=0;
int r=0;
int值1、值2;
国际项目[10];
theorder->海绵蛋糕=1;
theorder->蛋白酥=2;
theorder->chocalate=3;
theorder->红色天鹅绒=4;
对于(;ispongecake);
推送(&s,theorder->蛋白酥);
推送(s,theorder->chocalate);
推送(&s,theorder->红色天鹅绒);
}
而(!isEmpty(&s))
{
printf(“\n%d”,弹出(&s));
}
}
我得到的结果如下:
如您所见,它首先打印2和1。有什么问题吗?在我看来,您的程序工作如下:
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
return 0;
}
下面的循环尝试插入40个值
for(;i<10;i++)
{
push(&s,theorder->spongecake);
push(&s,theorder->meringue);
push(&s,theorder->chocalate);
push(&s,theorder->red_velvet);
}
否则,当sptr
为空时,函数返回随机值
语句
if(q->top==10-1)
应该是if(q->top==9)
除了当堆栈为空时返回NULL值的pop实现之外,堆栈没有问题。最好返回一个无效的数字作为约定(如0,-1,-999),由您选择
将堆栈大小限制为10时,只插入10个值。因此,在插入时,仅插入以下内容1 2 3 4 1 2 3 4 1 2并按顺序弹出。
pop()
如果(!isEmpty(sptr))==False,则返回一个未指定的值。对不起,我不明白您想说什么。你能解释一下吗。谢谢。你能指定order_out()的输出从哪里开始吗?它从顶部开始,随着值的删除,顶部逐渐减小。按名称命名的函数在哪里?