Question

In: Computer Science

DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...

DO THIS IN C++:

Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue.

In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.

NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to ask. So please don't waste my question from copying the answer from another question of chegg since I have already seen the answers and I am looking for the more specific one which is done in C++ as well.. Thanks in Advance.

CODE:

QueueType.h

#ifndef QueueTYPE_H_INCLUDED

#define QueueTYPE_H_INCLUDED

class FullQueue

{};

class EmptyQueue

{};

template

class QueueType

{

    struct NodeType

    {

        ItemType info;

        NodeType* next;

    };

public:

    QueueType();

    ~QueueType();

    void MakeEmpty();

    void EnQueue(ItemType);

    void DeQueue(ItemType&);

    bool IsEmpty();

    bool IsFull();

private:

    NodeType *front, *rear;

};

#include "QueueType.tpp"

#endif // QueueTYPE_H_INCLUDED

QueueType.tpp

#include "QueueType.h"

#include

using namespace std;

template

QueueType::QueueType()

{

    front = NULL;

    rear = NULL;

}

template

bool QueueType::IsEmpty()

{

    return (front == NULL);

}

template

bool QueueType::IsFull()

{

    NodeType* location;

    try

    {

        location = new NodeType;

        delete location;

        return false;

    }

    catch(bad_alloc& exception)

    {

        return true;

    }

}

template

void QueueType::EnQueue(ItemType newItem)

{

    if (IsFull())

        throw FullQueue();

    else

    {

        NodeType* newNode;

        newNode = new NodeType;

        newNode->info = newItem;

        newNode->next = NULL;

        if (IsEmpty()) // (front == NULL)

            front = newNode;

        else

            rear->next = newNode;

        rear = newNode;

    }

}

template

void QueueType::DeQueue(ItemType& item)

{

    if (IsEmpty())

        throw EmptyQueue();

    else

    {

        item = front->info;

        NodeType* tempPtr;

        tempPtr = front;

        front = front->next;

        if (front == NULL)

            rear = NULL;

        delete tempPtr;

    }

}

template

void QueueType::MakeEmpty()

{

    NodeType* tempPtr;

    while (front != NULL)

    {

        tempPtr = front;

        front = front->next;

        delete tempPtr;

    }

    rear = NULL;

//        ItemType a = 0;

//        while(front!=NULL){

//            DeQueue(a);

//        }

}

template

QueueType::~QueueType()

{

    MakeEmpty();

}

Solutions

Expert Solution

Reversing a queue is very simple. Consider the following logic:

Consider a queue: 2 4 5 6  8 9 10

FRONT REAR

2 4 6 8 9 10

In this queue, front points at the first element 2 and rear points at the last element 10. In order to reverse the queue, we can swap these node pointers. Swap front and rear, so rear will point to 2 and front will point to 10. So now, 10 will be the first element as front points to 10 and 2 will be the last element as rear points to 2.

After swapping front and rear, the queue will look like this : 10 9 8 6 4 2

REAR FRONT

2 4 6 8 9 10

We can swap front and rear by the following logic :

Declare a temporary pointer temp.

1) Let temp point to the first element in the queue temp = front ( now both temp and front point to the first element 2 )

FRONT, temp REAR

2 4 6 8 9 10

2) Make front pointer point to the last element in the queue front = rear ( now both rear and front point to the last element 10)

temp REAR, FRONT

2 4 6 8 9 10

3) Make rear pointer point to the first element in the queue rear=temp ( now rear and temp both point to the first element 2 )

REAR, temp    FRONT

2 4 6 8 9 10

4) Delete the temp pointer as we no longer need it. delete temp ( now rear points to 2 and front points to 10 )

REAR FRONT

2 4 6 8 9 10

This is how the pointers front and rear are swapped and the queue is reversed.

I have added the reverseQ function in your code as follows:( The function is at the end )

#ifndef QueueTYPE_H_INCLUDED

#define QueueTYPE_H_INCLUDED

class FullQueue

{};

class EmptyQueue

{};

template

class QueueType

{

    struct NodeType

    {

        ItemType info;

        NodeType* next;

    };

public:

    QueueType();

    ~QueueType();

    void MakeEmpty();

    void EnQueue(ItemType);

    void DeQueue(ItemType&);

    bool IsEmpty();

    bool IsFull();

void reverseQ();

private:

    NodeType *front, *rear;

};

#include "QueueType.tpp"

#endif // QueueTYPE_H_INCLUDED

QueueType.tpp

#include "QueueType.h"

#include

using namespace std;

template

QueueType::QueueType()

{

    front = NULL;

    rear = NULL;

}

template

bool QueueType::IsEmpty()

{

    return (front == NULL);

}

template

bool QueueType::IsFull()

{

    NodeType* location;

    try

    {

        location = new NodeType;

        delete location;

        return false;

    }

    catch(bad_alloc& exception)

    {

        return true;

    }

}

template

void QueueType::EnQueue(ItemType newItem)

{

    if (IsFull())

        throw FullQueue();

    else

    {

        NodeType* newNode;

        newNode = new NodeType;

        newNode->info = newItem;

        newNode->next = NULL;

        if (IsEmpty()) // (front == NULL)

            front = newNode;

        else

            rear->next = newNode;

        rear = newNode;

    }

}

template

void QueueType::DeQueue(ItemType& item)

{

    if (IsEmpty())

        throw EmptyQueue();

    else

    {

        item = front->info;

        NodeType* tempPtr;

        tempPtr = front;

        front = front->next;

        if (front == NULL)

            rear = NULL;

        delete tempPtr;

    }

}

template

void QueueType::MakeEmpty()

{

    NodeType* tempPtr;

    while (front != NULL)

    {

        tempPtr = front;

        front = front->next;

        delete tempPtr;

    }

    rear = NULL;

//        ItemType a = 0;

//        while(front!=NULL){

//            DeQueue(a);

//        }

}

template

QueueType::~QueueType()

{

    MakeEmpty();

}

void QueueType::reverseQ()

{

  NodeType* tempPtr; // create a temporary pointer

tempPtr=front; //store front pointer in tempPtr , now tempPtr will also point to the first element in the queue

front=rear; // now front will point to the last element of the queue

rear=tempPtr; // rear will now point to the first element of queue as tempPtr points to the first element

// we have swapped front and rear pointers, so,now the queue is reversed

delete tempPtr; // we delete it as we no longer need tempPtr

}


Related Solutions

Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
C++. How do I reverse this encryption? Here is the question: "A company that wants to...
C++. How do I reverse this encryption? Here is the question: "A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your program should read a four-digit integer in main() entered by the user and encrypt it as follows: 1. Replace each digit with the result of adding 7 to the digit and...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary....
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary. An itinerary has a title, a source position and a destination position. In addition, it may include intermediate positions. All positions have the same abstract type T. The real type can be decided later, for example it can be a cartesian point with x,y,z coordinates, or an airport code, or a city name, or a country name, etc .. The itinerary includes a vector...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT