Now you are asked to do the followings 1 Add void ReverseQue

Now, you are asked to do the followings: 1. Add void ReverseQueue(queueADT queue); prototype into queue.h interface: 2. 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 any additional storage (of course you can use local variables) 3. Modify qtest.c client/driver program so that a user can reverse the existing queue by calling the above function that is just added to the interface. 4. Finally say make and compile qtest.c with new qarray.c and qlist.c. Then test both arrayqtest and list-qtest programs....

---------qarray.c------------

--------------qlist.c------------

-------qtest.c--------

Solution

Without the full file, its difficult to check the correctness of algirthm. So next time it would be good if you zip all the header and c file and put it into dropbox so that we can get the access. Or either if possible paste it here.

But I will give you hint how to solve that. Take it in generalize way.

Suppose you have to reverse a subarray. How will you do that.

suppose array is [1, 2, 7, 4, 3, 9, 8] consider it as 0 index and you want to reverse the array subpart from index 1 to 5.

So new array will be [1, 9, 3, 4, 7, 2, 8]. We can see that we are just swapping tail and head.

So we can do that

int tail = queue->tail, head = queue->head;

while(head <= tail){

int temp = queue->elements[head];

queue->elements[head] = queue->elements[tail];

queue->elements[tail] = temp;

head++;

tail--;

}

For qlist it will be same but take it as linked list. In list case you need to cut out the current node, store its next in temp variable then point current node next to head node(previous cut node or if its first processing node then null), point head node to current node and assign temp to current.Do until current is not null.

suppose 1 - > 2 -> 3 -> null

current = 1 -> 2- > 3 - >null

temp - > 2->3->null

head -> null

current-> next = head = 1->null

head = current = 1->null

current = temp = 2->3->null

current = 2 -> 3 -> null

temp = 3-> null

head = 1 -> null

current->next = head = 2->1->null

head = current = 2->1->null

current = temp = 3->null

current = 3-> null

temp = null

head = 2->1->null

current->next = head = 3->2->1->null

head = current = 3->2->1->->null

current = temp = null

now you got one.

So here it go -

void reverseQueue(){

cellT *head = null, *tail = null, current = queue->head;

while(current != NULL){

cellT *temp = current->link;

current->link = head;

head = current;

if(head->link == null)tail = head;

current = temp;

}

queue->head = head;

queue->tail = tail;

}

If you have any doubt you can ask me.

Now, you are asked to do the followings: 1. Add void ReverseQueue(queueADT queue); prototype into queue.h interface: 2. Implement this function under both array
Now, you are asked to do the followings: 1. Add void ReverseQueue(queueADT queue); prototype into queue.h interface: 2. Implement this function under both array

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site