0

我有一个带有队列的程序,我需要根据条件从队列中删除值。条件是

如果先前从队列中删除的值大于要删除的值,则应从队列中删除该值,否则将该值再次放回队列中(循环实现)。

这是我到目前为止所做的:

    #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);
      }
   } 
}

我无法获得所需的输出,这似乎是什么问题?

感谢您的时间。

4

2 回答 2

0

只需取一个变量并将其值最初设置为零。

并在删除之前的弹出功能中,只需检查是否

值 < 变量

. 如果为真,则 POP 否则返回。

于 2013-05-28T07:22:00.123 回答
0

在您的 while 循环中,当您只想删除最多一个元素时,您会多次调用 removes(&s)。

根据您的情况,while循环可以是:

while(!isEmpty(&s)) 
{  
    int tmp=removes(&s);
    if(k>tmp) {
        printf("%d should be remove,and k is %d\n",tmp,k);
        k = tmp;
        printf("k changed.\n");
    }
    else
    {
        printf("%d insert again,and k is %d\n",tmp,k);
        insert(&s,tmp);
    }
}

不幸的是,由于您的状况和数据,这是一个无限循环。

在您的初始队列中,

40 20 75 30 40 20 75 30 40 20 75 30

k=40,第一个 40 再次插入。

到 20,去掉 20,现在 k=20。

其余不少于20个,将被一遍又一遍地检查和插入。

此外,即使队列为空,您的 remove() 也应该返回一个错误值,以便您处理调用 remove() 的这种情况。

于 2013-05-28T07:50:43.147 回答