Hi Guys
Just wondering if someone on here can help a uni student out.
Using the lecture example below write two new que functions:
emptyQ(que) returns 1 if queue que is empty and 0 otherwise
fullQ(que) returns 1 if queue que is full and 0 otherwise
Code:
#include <stdio.h>
#define MAX 5
typedef struct
{
int list[MAX]; /*Queue of integers*/
int size; /*Size of queue in use*/
int front; /*Index representing the front of the queue*/
int rear; /*Index representing the rear of the queue*/
}queue;
/* Prototypes */
int menu_display();
void initQ(queue *);
int insertQ(queue *, int);
int deleteQ(queue *);
int empty(queue);
int full(queue);
void printQ(queue);
int main(void)
{
int choice, item, ok;
queue myqueue;
initQ(&myqueue);
choice = menu_display();
while (choice != 0)/*0 - quit*/
{
switch(choice)
{
case 1:/*Insert*/
printf("\nEnter a value to insert:");
scanf("%d", &item);
ok = insertQ(&myqueue, item);
if (ok)
printf("\n%d inserted\n\n", item);
else
puts("\nQUEUE FULL\n");
break;
case 2:/*Remove*/
item = deleteQ(&myqueue);
if (item >= 0)
printf("\n%d removed\n\n", item);
else
puts("\nQUEUE EMPTY\n");
break;
case 3:/* empty? */
ok = empty(myqueue);
if (ok)
puts("QUEUE IS EMPTY\n");
else
puts("QUEUE NOT EMPTY\n");
break;
case 4:/* full? */
ok = full(myqueue);
if (ok)
puts("QUEUE IS FULL\n");
else
puts("QUEUE NOT FULL\n");
break;
case 5:
printQ(myqueue);
break;
default:/*Invalid*/
puts("\nINVALID choice – re-enter");
}/*end switch*/
choice = menu_display();
}/* end while */
return 0;
}/*end main*/
int menu_display()
{
int choice;
puts("\nMenu selection\n");
puts("Enter 1 insert");
puts("Enter 2 remove");
puts("Enter 3 empty?");
puts("Enter 4 full?");
puts("Enter 5 print");
puts("Enter 0 for EXIT\n");
scanf("%d",&choice);
return choice;
}/*end menu_display*/
/*Initialise the queue*/
void initQ(queue *qu)
{
qu->size = MAX; /* max size of queue qu */
qu->front = 0; /* queue qu is initially empty */
qu->rear = -1; /* set rear to -1 (will be incremented by 1 before use)*/
}/*end initQ*/
int insertQ(queue *qu, int item)
{
/*Remember the size of an array is always 1 more than its upper index
*so if the size of the array is 3 then the elements are 0 to 2.
*The rear index represents the LAST USED index position for an item in the queue
*/
if (qu->rear < qu->size - 1)
{
/*Increment rear by 1 so we don't overwrite the last position used*/
qu->rear++;
/*then add item value at index rear*/
qu->list[qu->rear] = item;
/* return 1 (true) to indicate insertion was successful*/
return 1;
}/*end if*/
else
{
return 0;/* return 0 (false) to indicate insertion failed - queue was full*/
}/*end else*/
}/*end insertQ*/
int deleteQ(queue *qu)
{
/*Check if the index of the item at the front isn't greater than the rear*/
if (qu->front <= qu->rear)
{
/*Return the item at index front. The item has now left the queue so
*increase front by 1 (after returning the value) to point to the next item
*/
return qu->list[qu->front++];
}/*end if*/
else/*Queue is empty*/
{
return -1;
}/*end else*/
}/*end deleteQ*/
int empty(queue qu)
{
if(queue.top > 0);
{
return 0;
}
return 0;
}
int full(queue qu)
{
return 0;
}
void printQ(queue qu)
{
puts("not yet implemented");
}