I need to modify this program to use a linked-list implementation of a queue. All of the queue functions must be rewritten. The queue length needs to be defined as 6. Arrange a queue of 6 items, try to enter one more item (make sure that your program produces the warning message about overflow), then delete 3 items, enter 1 more item and display the queue. We just barely covered queues the other day so I really don't understand this too much at all. We never covered how to implement queues with linked-lists either, so this is all new to me. Here is the code I need to rewrite. Please help.
Code:
/* queue2.c */
/* a program to implement a queue using an array of int */
#include <stdio.h>
#define QSIZE 5
typedef struct{
int elem[QSIZE];
int front;
int rear;
int count;
}QType;
/* function prototypes */
void initialize(QType *);
int empty(QType *);
int full(QType *);
int rem(QType *);
void insert(QType *, int);
main()
{
QType Q; /* queue declaration */
int code, code1, i, n;
int i1, i2;
initialize(&Q);
printf("initially: is empty?\n %d\n", empty(&Q));
printf("is full?\n %d\n", full(&Q));
printf("Enter 1 to continue, 0 to stop: ");
scanf("%d", &code);
while(code!=0)
{ printf("Enter 2 to insert an item, 3 to remove an item: ");
scanf("%d", &code1);
switch(code1)
{ case 2: printf("Enter an integer to be inserted: ");
scanf("%d", &i1);
/* printf("case2: i1 is %d\n", i1); */
insert(&Q, i1);
break;
case 3: i2=rem(&Q);
if(i2==0)
printf("Nothing is removed\n");
else
printf("%d is removed\n", i2);
break;
default: printf("Wrong code\n");
}
printf("Currently in the queue:\n");
if(Q.count==0)
printf("Nothing\n");
else
{
i=Q.front; n=0;
do{
printf("%d ", Q.elem[i]);
n++;
if(i>=QSIZE-1)
i=0;
else
i++;
}while(n<Q.count);
printf("\n");
}
printf("Enter 1 to continue, 0 to stop: ");
scanf("%d", &code);
}
return 0;
}
void initialize(QType *q)
/* set head counter and tail counter to 0; */
/* set counter of elements in the queue to 0 */
{ q->front=q->rear=0;
q->count=0;
}
int empty(QType * q)
/* This function will determine if the queue is empty by */
/* testing the value of a counter of elements in the queue. */
/* If the queue is empty, this function will print the message */
/* "queue is empty" and return a value of 1. */
/* Otherwise, no message is displayed, and zero is returned. */
{
if (q->count == 0)
{ printf("QUEUE IS EMPTY\n");
return 1;
}
else return 0;
}
int full(QType * q)
/* This function will determine if the queue is full by */
/* testing the value of a counter of elements in the queue.*/
/* If the queue is full, prints "queue is full" and returns*/
/* a value of 1. Otherwise, does not print anything */
/* and returns zero. */
{
if (q->count == QSIZE)
{ printf("QUEUE IS FULL\n");
return 1;
}
else return 0;
}
int rem(QType *q)
/* This function will return the item currently located */
/* in the head of the queue. Then, the head counter */
/* will be incremented. */
{
int temp;
if (empty(q)) return 0;
temp = q->elem[q->front];
q->front=(q->front+1) % QSIZE;
(q->count)--;
return temp;
}
void insert(QType *q, int item)
/* The integer 'item' is inserted at the tail of the */
/* queue and the tail counter is incremented. */
{
if (full(q)) return;
q->elem[q->rear] = item;
/* printf("in the insert: %d inserted\n", q->elem[q->rear]);*/
q->rear = (q->rear + 1) % QSIZE;
(q->count)++;
return;
}