1. Engineering
  2. Computer Science
  3. c program queue queue with circular array please implement a...

Question: c program queue queue with circular array please implement a...

Question details

c program queue

[Queue with Circular Array]

Please implement a Queue ADT using circular array, as demonstrated in the lecture notes. Please complete the code in the following template:

#include <stdio.h>

#include <stdlib.h>

typedef struct {

    int *circularArray;

    int maxSize;     // max size of queue; size of circular array

    int frontIndex; // index of the front of queue

    int rearIndex;   // index of the end of queue

    int queueLength; // lenght of queue at the moment; 0 means no one is in queue

} Queue;

void QueueInit(Queue* queue, int size) {

    printf("Initializing queue of max size %d\n",size);

   

    queue->circularArray = (int*) malloc(sizeof(int) * size);

    queue->maxSize = size;

    queue->frontIndex = 0;

    queue->rearIndex = -1;

    queue->queueLength = 0;

}

void Enqueue(Queue* queue, int data) {

    printf("Enqueueing %d\n",data);

   

    // TODO: implement

    // if queue is full, print "Queue full!" and do not enqueue

}

int Dequeue(Queue* queue) {

    // TODO: implement

    // if queue is empty, print "Queue empty!" and return -1

    return -1;

   

}

void QueuePrint(Queue* queue) {

    printf("Print queue: ");

    if (queue->queueLength == 0) {

        printf("(empty)\n");

        return;

    } else if (queue->queueLength <0) {

        printf("Queue length is negative! Something's wrong!\n");

        return;

    }

   

    int i;

    for (i=0;i<queue->queueLength;i++) {

        int circularIndex = (queue->frontIndex+i)%queue->maxSize;

        printf("%d ",queue->circularArray[circularIndex]);

    }

    printf("\n");

}

int main()

{

    Queue myQueue;

   

    QueueInit(&myQueue,6);

    QueuePrint(&myQueue);

    Enqueue(&myQueue,10);

    QueuePrint(&myQueue);

    Enqueue(&myQueue,11);

    Enqueue(&myQueue,12);

    Enqueue(&myQueue,13);

    Enqueue(&myQueue,14);

    Enqueue(&myQueue,15);

    Enqueue(&myQueue,16);

    QueuePrint(&myQueue);

    int i;

    for (i=0;i<4;i++) {

        int result = Dequeue(&myQueue);

        if (result != -1)

            printf("Dequeueing %d\n",result);

    }

    QueuePrint(&myQueue);

    Enqueue(&myQueue,16);

    for (i=0;i<5;i++) {

        int result = Dequeue(&myQueue);

        if (result != -1)

            printf("Dequeueing %d\n",result);

    }

    QueuePrint(&myQueue);

}

There are multiple ways to implement a circular array. Please follow the implementation in the template. In particular:

  1. QueuePrint has already been done for you; reading it will be essential for you to understand this template!
  2. You may find the Queue session of the lecture notes on "Abstract Data Type, Stack/Queue" helpful if you forgot what circular array is already.
  3. Do NOT modify the Queue struct; you will receive NO MARKS if you modify it
  4. Enqueue should print "Queue full!" if the queue is already full and you want to add one more to the queue
  5. Similarly, Dequeue should print "Queue empty!" and return -1 if queue has nothing to dequeue from
  6. The main output, if you do not modify the main, should look like this:

Initializing queue of max size 6
Print queue: (empty)
Enqueueing 10
Print queue: 10
Enqueueing 11
Enqueueing 12
Enqueueing 13
Enqueueing 14
Enqueueing 15
Enqueueing 16
Queue full!
Print queue: 10 11 12 13 14 15
Dequeueing 10
Dequeueing 11
Dequeueing 12
Dequeueing 13
Print queue: 14 15
Enqueueing 16
Dequeueing 14
Dequeueing 15
Dequeueing 16
Queue empty!
Queue empty!
Print queue: (empty)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution