gpt4 book ai didi

c - 将值从队列插入堆栈

转载 作者:行者123 更新时间:2023-11-30 19:22:20 25 4
gpt4 key购买 nike

我正在尝试使用队列将值插入堆栈,但问题是没有从堆栈中获取任何弹出内容(无输出)。这是我所做的:

#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 循环。

关于c - 将值从队列插入堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16836682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com