Reputation: 57
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. 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?
Upvotes: 0
Views: 1314
Reputation: 11
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..
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..
Upvotes: 0
Reputation: 1674
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
.
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];
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
? The queue is empty but your code will interpret it as full (because re==QSIZE-1
).
Upvotes: 1
Reputation: 8861
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.
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]);
}
==
instead of >
return
instead of exit(0)
<
instead of <=
Insert
if(re == QSIZE) // 1
{
printf("Queue Overflow\n");
return; // 2
}
QSIZE
instead of QSIZE-1
return
instead of exit(0)
Delete
There's no reason to call exit(0)
. Nothing fatal with trying to delete from an empty queue.
Upvotes: 0