我正在尝试使用队列将值推入堆栈,但问题是没有从堆栈中获得任何弹出(无输出)。这是我所做的:
#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 循环上。
我该如何解决这个错误?
感谢您的时间。