使用数组队列 [英] 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]);
}
我在队列中,因为最初使用初始前后条目0则先成为最后一个条目以及任何条目。但是我的老师说我应该保持后条目-1,并同时将元素插入队列中,先增加后进入指数,然后加入反对的第一个再加入我的递增code。我看着它,并在网上和到现在为止我没有找到我怎么错了。
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. 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. 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?
推荐答案
这两个pre-递增和递增后可以在队列中使用。然而什么改变是全空的条件。随着pre-增量全部条件是 QSIZE-1
,具有后递增,充分条件是 QSIZE
。随着pre-增量空的条件是 FE ==再
,后增 FE&GT;再
。
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
. With pre-increment the empty condition is fe==re
, with post-increment fe>re
.
使用pre-增量可以保存删除一个临时变量。请注意如何你必须保存当前的元素融入项目,然后递增指标,然后返回项目。
Using pre-increment can save a temporary variable in delete. Notice how you must save the current element into item, then increment the index, then return item.
item=q[fe];
fe++;
return item;
您可以通过增加指数与项目
变量做掉,然后的返回的元素。
You can do away with the item
variable by incrementing the index, then returning the element.
fe++;
return q[fe];
记住,如果你pre-增量插入,你需要pre-增量删除。
Just remember, if you pre-increment to insert, you need to pre-increment to delete.
好吧,考虑这一点。会发生什么 FE
和重新
都是 QSIZE-1
?队列是空的,但你的code会间preT它作为完整的(因为重新== QSIZE-1
)。
Ok, consider this. What happens fe
and re
are both QSIZE-1
? The queue is empty but your code will interpret it as full (because re==QSIZE-1
).
这篇关于使用数组队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!