【发布时间】:2013-05-28 06:30:28
【问题描述】:
我有一个带有队列的程序,我需要根据条件从队列中删除值。条件是
如果先前从队列中删除的值大于要删除的值,则应从队列中删除该值, 如果不是,则将值再次放回队列中(循环实现)。
这是我到目前为止所做的:
#include<stdio.h>
#include<malloc.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void order_out(struct cakes *);
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
main()
{
struct cakes *head;
head=(struct cakes*)malloc(sizeof(struct cakes));
order_out(head);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *theorder)
{
struct Queue s;
int i,k;
int p=0;
theorder->spongecake=20;
theorder->meringue=75;
theorder->chocalate=40;
theorder->red_velvet=30;
k=theorder->chocalate;
init(&s);
for(i=0;i<10;i++)
{
insert(&s,theorder->chocalate);
insert(&s,theorder->spongecake);
insert(&s,theorder->meringue);
insert(&s,theorder->red_velvet);
}
while(!isEmpty(&s))
{
if(k>removes(&s)) //here i check whether the the value am going to remove is less than the chocalate value
{
printf("%d",removes(&s));
k=removes(&s); //i make k the value which was removed so it will be compared in the next time.
}
else
{
p=removes(&s);
insert(&s,p);
}
}
}
我无法获得所需的输出,这似乎是什么问题?
感谢您的宝贵时间。
【问题讨论】:
-
我建议您在调试器中逐行逐行检查您的代码。但是,在
order_outwhile 循环中,如果k大于您删除的值,您会执行另外两次删除,这看起来很可疑。在这种情况下,您确定要删除三个项目吗? -
不,我只想删除一个。我会检查一下。
标签: c queue structure conditional-statements