简体   繁体   English

使用数组排队

[英]Queue using Arrays

Below is my implementation of a simple queue using arrays. 下面是我使用数组的简单队列的实现。

#include<stdio.h>
#include<stdlib.h>
#define QSIZE 5 //Limit size of queue to just 5 enteries

/*Beginning of prototype for queue functions: Insert, retrieve and display*/
void qdisp(); //Display to queue array contents
void qinsert(); //Insert a element into rear of queue
int qdelete(); //Remove an element from front of queue
/*End of prototyping*/

// Variables
int fe=0,re=0,q[QSIZE],item; //fe(front entry), re(rear entry), q[](queue array), item(element to i/p or delete)

void main()
{
  int choice;
  while(1)
  {
    printf("1.Insert element\n2.Remove element\n3.Display element(s)\n4.Exit program\nEnter number for appropriate choice:  ");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:   printf("Enter value of element: ");
            scanf("%d",&item);
            qinsert();
            break;
      case 2:   item=qdelete();
            printf("Removed \'%d\' from the queue\n",item);
            break;
      case 3:   qdisp();
            break;
      case 4:   exit(0);

      /*case default : printf("Wrong choice i/p!");
              break;*/
    }
  }
}
//End of main, beginning for function definitons

void qinsert()
{
  if(re==QSIZE-1)
  {
    printf("Queue Overflow\n");
    exit(0);
  }
  q[re]=item;
  re++;
}

int qdelete()
{
  if(fe>re)
  {
    printf("Queue is empty!\n");
    exit(0);
  }

  item=q[fe];
  fe++;
  return item;
}

void qdisp()
{
  int i; //i is loop counter variable
  if(fe>re)
  {
    printf("Queue is empty!\n");
    exit(0);
  }
  printf("Queue items are: \n");
  for(i=fe;i<=re;i++)
    printf("%d\n",q[i]);
}

I have used initial front and rear entry as 0 since initially in a queue any entry that goes first becomes the last entry as well. 我将初始的前后条目都设为0,因为最初在队列中,任何先行的条目也将成为最后的条目。 However my teacher says I should keep the rear entry as '-1' and while inserting an element into queue, first increment the rear entry index and then add opposing my code of first adding then incrementing. 但是我的老师说我应该将后方条目保持为“ -1”,并且在将元素插入队列时,首先增加后方条目索引,然后添加与我相反的代码,先添加然后递增。 I looked into it and online and till now I don't find how I'm wrong. 我在网上进行了调查,直到现在我都没有发现自己错了。

Provide me information if I'm wrong or my teacher is? 如果我错了或者我的老师给我信息?

Both pre-incrementing and post-incrementing can be used in a queue. 可以在队列中使用前递增和后递增。 What changes however is the full and empty conditions. 但是,变化的是满空条件。 With pre-increment the full condition is QSIZE-1 , with post-incrementing the full condition is QSIZE . 递增前,完整条件为QSIZE-1 ,递增后,完整条件为QSIZE With pre-increment the empty condition is fe==re , with post-increment fe>re . 对于前递增,空条件为fe==re ,对于后递增, fe>re

Using pre-increment can save a temporary variable in delete. 使用预增量可以在delete中保存一个临时变量。 Notice how you must save the current element into item, then increment the index, then return item. 注意必须如何将当前元素保存到item中,然后递增索引,然后返回item。

item=q[fe];
fe++;
return item;

You can do away with the item variable by incrementing the index, then returning the element. 您可以通过增加索引, 然后返回元素来取消item变量。

fe++;
return q[fe];

Just remember, if you pre-increment to insert, you need to pre-increment to delete. 请记住,如果您要预先增加要插入的内容,则需要先要增加要删除的内容。

Ok, consider this. 好吧,考虑一下。 What happens fe and re are both QSIZE-1 ? fere都是QSIZE-1会发生什么? The queue is empty but your code will interpret it as full (because re==QSIZE-1 ). 队列为空,但是您的代码会将其解释为已满(因为re==QSIZE-1 )。

Any teachers around? 周围有老师吗? Okay, good. 好的。

Teachers aren't always right, but there's probably a good chance you simply misunderstood him. 老师并不总是正确的,但是您很可能只是误解了他。 As long as you understand that when fe == re the queue is empty, both being 0 initially is just fine. 只要您了解到fe == re队列为空,则最初都为0就可以了。

However, some of your functions need to change: 但是,您的某些功能需要更改:

Delete 删除

void qdisp(void)
{
    int i;
    if(fe == re)                     // 1
    {
        printf("Queue is empty!\n");
        return;                      // 2
    }
    printf("Queue items are:\n");
    for(i = fe; i < re; i++)         // 3
        printf("%d\n", q[i]);
}
  1. == instead of > ==代替>
  2. return instead of exit(0) return而不是exit(0)
  3. < instead of <= <代替<=

Insert 插入

if(re == QSIZE)                 // 1
{
    printf("Queue Overflow\n");
    return;                     // 2
}
  1. QSIZE instead of QSIZE-1 QSIZE而不是QSIZE-1
  2. return instead of exit(0) return而不是exit(0)

Delete 删除

There's no reason to call exit(0) . 没有理由调用exit(0) Nothing fatal with trying to delete from an empty queue. 尝试从空队列中删除没有任何致命性。

in insert function when first to check queue is empty or not condition is: 在插入功能中,当首先检查队列为空或不满足时,条件是:

if(r==0)

because when queue is empty rear is always at 0.. then check wheather queue has only 1 element or more than 1.. 因为当队列为空时,后方始终为0。.所以请检查小麦队列中只有1个元素或大于1。

if(f==r-1)
 {f=0;r=0;}
 else
 {f++;}

in insert function when u insert last element your rear will incremented and set to 5 then when u delete element front is incremented and for last element front is 4 which is less than rear by 1...so condition to check whether only 1 element left or not is f==r-1 and if it is satisfied then array becomes completely empty so we set front and rear to 0.. 在插入功能中,当您插入最后一个元素时,您的后方会增加并设置为5,然后当您删除元素前部时,您的后方会增加,并且对于最后一个元素,前方是4,比后方小1。是否为f == r-1,如果满足,则数组将完全为空,因此将前和后设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM