我正在尝试使用队列将值推入堆栈,但事情是没有从堆栈中获取任何弹出消息(无输出)。
这是我所做的:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define MAX 180
#define TRUE 1
#define FALSE 0
struct cakes {
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct cakes *next;
};
struct stack{
int top;
int cake[10];
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void conveyer_order(struct cakes *);
void baking_order(struct cakes *);
int isFull(struct stack *);
int isEmpty(struct stack *);
void push(struct stack *,int);
int pop(struct stack *);
void init(struct Queue *);
int isqueueFull(struct Queue*);
void insert(struct Queue*,int);
int isqueueEmpty(struct Queue *);
int removes(struct Queue *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes));
conveyer_order(head);
head->next=(struct cakes *)malloc(sizeof(struct cakes));
baking_order(head->next);
}
int isFull(struct stack *k)
{
if(k->top==10-1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int isEmpty(struct stack *k)
{
if(k->top==-1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
}
void push(struct stack *sptr,int x)
{
if(!isFull(sptr))
{
sptr->top++;
sptr->cake[sptr->top]=x;
}
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isqueueFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isqueueFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isqueueEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int cakeempty=NULL;
if(!isqueueEmpty(q))
{
cakeempty=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return cakeempty;
}
}
void baking_order(struct cakes *theorder)
{
int v=0;
struct stack baking;
struct Queue belt;
baking.top=-1;
int value1=0;
int value2=0;
theorder->spongecake=20;
theorder->chocalate=40;
theorder->red_velvet=30;
theorder->meringue=75;
init(&belt);
while(!isqueueFull(&belt))
{
insert(&belt,theorder->meringue);
insert(&belt,theorder->chocalate);
insert(&belt,theorder->red_velvet);
insert(&belt,(theorder->spongecake));
}
value1=removes(&belt);
while(!isqueueEmpty(&belt))
{
while(!isFull(&baking))
{
value2=removes(&belt);
if(value1>=value2)
{
push(&baking,value2);
value1=value2;
}
else
{
insert(&belt,value2);
}
}
}
while(!isEmpty(&baking))
{
printf("\n%d",pop(&baking));
}
}
我尝试打印值而不传递到堆栈中并且它可以工作,我认为问题出在2个while循环上。
我该如何解决此错误?
感谢您的时间。
最佳答案
问题是您只将4个项目压入堆栈,因此堆栈永远不会满,您的while(!isFull(&baking))
成为无限循环。
让我们看看我能否解释一下原因:
在开始while循环之前,value1为75。
然后,将value2读取为40。
检查if(value1>=value2)
?是的,因此您按40并将value1设置为40。
然后重新启动循环,将value2读取为30。
检查if(value1>=value2)
?是的,因此您按30并将value1设置为30。
然后重新启动循环,将value2读取为20。
检查if(value1>=value2)
?是的,因此您按20并将value1设置为20。
然后重新启动循环,将value2读取为75。
检查if(value1>=value2)
?不,因此您将75重新排入队列。
然后重新启动循环,将value2读取为40。
检查if(value1>=value2)
?不,所以您将40重新排入队列。
然后重新启动循环,将value2读取为30。
检查if(value1>=value2)
?不,所以您将30重新排入队列。
然后重新启动循环,将value2读取为20。
检查if(value1>=value2)
?是的,因此您按20并将value1设置为20。
此时,您已将40、30、20、20推入。
但是,队列中剩下的所有内容都是30、40或75,并且if(value1>=value2)
永远不会再为true。
因此,您的堆栈永远不会被填充,并且您永远不会退出while循环。