write a funtion to reverse queue in both array and link list

write a funtion to reverse queue in both array and link list

Add void ReverseQueue(queueADT queue);

Implement this function under both array

and list representations. That is, add its

implementation into both

qarray.c

and

qlist.c

files

When Implementing this function, make sure that you will use the original

memory cells and do not allocate allocate any additional storage

*
* File: qarray.c
* --------------
* This file implements the queue.h abstraction using an array.
*/

#include <stdio.h>
#include \"genlib.h\"
#include \"queue.h\"

#define MaxQueueSize   100
#define QueueArraySize (MaxQueueSize + 1)

struct queueCDT {
    queueElementT elements[QueueArraySize];
    int head;
    int tail;
};

void ReverseQueue(queueADT queue){

}

//qlist.c

#include <stdio.h>
#include \"genlib.h\"
#include \"queue.h\"


/*

typedef struct cellT {
    queueElementT value;
    struct cellT *link;
} cellT;

void ReverseQueue(queueADT queue){


}

Solution

qarray.c is as below:

#include <stdio.h>

#define MaxQueueSize 100
#define QueueArraySize (MaxQueueSize + 1)

// Used for testing. For actual replace with def from queue.h
typedef int queueElementT;

typedef struct {
queueElementT elements[QueueArraySize];
int head;
int tail;
} queueADT;

///Printing the queue

void traverseQueue(queueADT queue){
   int i;
for(i=queue.head; i<queue.tail; i++){
    printf(\"%d \ \",queue.elements[i]);
}

}

//Reversing the queue

void ReverseQueue(queueADT *queue){
    int tail = queue->tail;
    int head = queue->head;
int size = tail - head +1;
int halfSize = size/2;
int index;
for(index = head; index <(head+halfSize); index++) {
    int temp = queue->elements[index];
    queue->elements[index] = queue->elements[tail-index];
    queue->elements[tail-index] = temp;
}
}

// Main for testing

int main() {
   printf(\"hello ...\");
   queueADT myq;
   int i=0;
   for(i=0; i<10; i++) {
       myq.elements[i]=i;
   }
   myq.head = 0;
   myq.tail = 9;
   traverseQueue(myq);
   ReverseQueue(&myq);
   traverseQueue(myq);
}

Similarly this can be done for List also.

write a funtion to reverse queue in both array and link list Add void ReverseQueue(queueADT queue); Implement this function under both array and list representa
write a funtion to reverse queue in both array and link list Add void ReverseQueue(queueADT queue); Implement this function under both array and list representa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site