将值从队列推送到堆栈

时间:2013-05-30 12:48:44

标签: c while-loop queue stack

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

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

如何解决此错误?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

问题是你只需要将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)永远不会再评估为真。

因此,您的堆栈永远不会被填充,您永远不会离开while循环。

相关问题