I'm having an issue with getting this code reversed using a linkedStack. I tried looking into it from multiple sources and found very little information that can be used to find an answer to my problem. I can understand that they are similar to one another but maybe due to me being new to coding in general, I found it very difficult to use a linkedStack with my current files.
I hope to know how I could use my current files to create a reversedQueue using a linkedStack within the arrayQueue.h and arrayQueue.cpp files.
please let me know whether I'm missing something from what I provided down below.
queueADT.h file
//Header file: queueADT.h
#ifndef _QUEUEADT_
#define _QUEUEADT_
#include "stackADT.h"
template <class ItemType>
class queueADT
{
public:
virtual void initializeQueue() = 0;
//Function to initialize the queue to an empty state.
//Postcondition: The queue is empty.
virtual bool isEmptyQueue() const = 0;
//Function to determine whether the queue is empty.
//Postcondition: Returns true if the queue is empty,
// otherwise returns false.
virtual bool isFullQueue() const = 0;
//Function to determine whether the queue is full.
//Postcondition: Returns true if the queue is full,
// otherwise returns false.
virtual ItemType front() const = 0;
//Function to return the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the first
// element of the queue is returned.
virtual ItemType back() const = 0;
//Function to return the last element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the last
// element of the queue is returned.
virtual void reverseQueue(const ItemType& queue) = 0;
virtual void addQueue(const ItemType& queueElement) = 0;
//Function to add queueElement to the queue.
//Precondition: The queue exists and is not full.
//Postcondition: The queue is changed and queueElement
// is added to the queue.
virtual void deleteQueue() = 0;
//Function to remove the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: The queue is changed and the first
// element is removed from the queue.
};
#endif
arrayQueue.h file
//Header file arrayQueue
#ifndef _ARRAYQUEUE_
#define _ARRAYQUEUE_
#include <iostream>
#include <cassert>
#include "queueADT.h"
using namespace std;
template <class ItemType>
class arrayQueue: public queueADT<ItemType>
{
public:
const arrayQueue<ItemType>& operator=(const arrayQueue<ItemType>&);
//Overload the assignment operator.
bool isEmptyQueue() const;
//Function to determine whether the queue is empty.
//Postcondition: Returns true if the queue is empty,
// otherwise returns false.
bool isFullQueue() const;
//Function to determine whether the queue is full.
//Postcondition: Returns true if the queue is full,
// otherwise returns false.
void initializeQueue();
//Function to initialize the queue to an empty state.
//Postcondition: The queue is empty.
ItemType front() const;
//Function to return the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the first
// element of the queue is returned.
ItemType back() const;
//Function to return the last element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the last
// element of the queue is returned.
void addQueue(const ItemType& queueElement);
//Function to add queueElement to the queue.
//Precondition: The queue exists and is not full.
//Postcondition: The queue is changed and queueElement
// is added to the queue.
void reverseQueue(const ItemType& queue);
void deleteQueue();
//Function to remove the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: The queue is changed and the first
// element is removed from the queue.
arrayQueue(int queueSize = 100);
//Constructor
arrayQueue(const arrayQueue<ItemType>& otherQueue);
//Copy constructor
~arrayQueue();
//Destructor
private:
int maxQueueSize; //variable to store the maximum queue size
int count; //variable to store the number of
//elements in the queue
int queueFront; //variable to point to the first
//element of the queue
int queueRear; //variable to point to the last
//element of the queue
ItemType *list; //pointer to the array that holds
//the queue elements
};
#include "arrayQueue.cpp"
#endif
arrayQueue.cpp file
template <class ItemType>
bool arrayQueue<ItemType>::isEmptyQueue() const
{
return (count == 0);
} //end isEmptyQueue
template <class ItemType>
bool arrayQueue<ItemType>::isFullQueue() const
{
return (count == maxQueueSize);
} //end isFullQueue
template <class ItemType>
void arrayQueue<ItemType>::initializeQueue()
{
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
} //end initializeQueue
template <class ItemType>
ItemType arrayQueue<ItemType>::front() const
{
assert(!isEmptyQueue());
return list[queueFront];
} //end front
template <class ItemType>
ItemType arrayQueue<ItemType>::back() const
{
assert(!isEmptyQueue());
return list[queueRear];
} //end back
template <class ItemType>
void arrayQueue<ItemType>::addQueue(const ItemType& newElement)
{
if (!isFullQueue())
{
queueRear = (queueRear + 1) % maxQueueSize; //use mod
//operator to advance queueRear
//because the array is circular
count++;
list[queueRear] = newElement;
}
else
cout << "Cannot add to a full queue." << endl;
} //end addQueue
// Test for reversing a queue using a linkedStack
template <class ItemType>
void arrayQueue<ItemType>::reverseQueue(const ItemType& queue) {
int n = queue.front();
linkedStack<int> st;
// Remove all the elements from queue and push them to stack
for (int i = 0; i < n; i++) {
int curr = queue.front();
queue.pop();
st.push(curr);
}
// Pop out elements from the stack and push them back to queue
for (int i = 0; i < n; i++) {
int curr = st.top();
st.pop();
queue.push(curr);
}
// Print the reversed queue
for (int i = 0; i < n; i++) {
int curr = queue.front();
queue.pop();
cout<<curr<<" ";
queue.push(curr);
}
cout<<endl;
}
template <class ItemType>
void arrayQueue<ItemType>::deleteQueue()
{
if (!isEmptyQueue())
{
count--;
queueFront = (queueFront + 1) % maxQueueSize; //use the
//mod operator to advance queueFront
//because the array is circular
}
else
cout << "Cannot remove from an empty queue." << endl;
} //end deleteQueue
//Constructor
template <class ItemType>
arrayQueue<ItemType>::arrayQueue(int queueSize)
{
if (queueSize <= 0)
{
cout << "Size of the array to hold the queue must "
<< "be positive." << endl;
cout << "Creating an array of size 100." << endl;
maxQueueSize = 100;
}
else
maxQueueSize = queueSize; //set maxQueueSize to
//queueSize
queueFront = 0; //initialize queueFront
queueRear = maxQueueSize - 1; //initialize queueRear
count = 0;
list = new ItemType[maxQueueSize]; //create the array to
//hold the queue elements
} //end constructor
//Destructor
template <class ItemType>
arrayQueue<ItemType>::~arrayQueue()
{
delete [] list;
} //end destructor
template <class ItemType>
const arrayQueue<ItemType>& arrayQueue<ItemType>::operator=
(const arrayQueue<ItemType>& otherQueue)
{
cout << "Write the definition of the function "
<< "to overload the assignment operator." << endl;
} //end assignment operator
template <class ItemType>
arrayQueue<ItemType>::arrayQueue(const arrayQueue<ItemType>& otherQueue)
{
cout << "Write the definition of the copy constructor."
<< endl;
} //end copy constructor
stackADT.h file
#ifndef _STACKADT_
#define _STACKADT_
template <class ItemType>
class stackADT
{
public:
virtual void initializeStack() = 0;
//Method to initialize the stack to an empty state.
//Postcondition: Stack is empty
virtual bool isEmptyStack() const = 0;
//Method to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
// otherwise returns false.
virtual bool isFullStack() const = 0;
//Method to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
// otherwise returns false.
virtual void push(const ItemType& newItem) = 0;
//Method to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of the stack.
virtual ItemType top() const = 0;
//Method to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top element
// of the stack is returned.
virtual void pop() = 0;
//Method to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
};
#endif
linkedStack.h file
//Header File: linkedStack.h
#ifndef _LINKEDSTACK_
#define _LINKEDSTACK_
#include <iostream>
#include <cassert>
#include "stackADT.h"
using namespace std;
//Definition of the node
template <class ItemType>
struct nodeType
{
ItemType info;
nodeType<ItemType> *link;
};
template <class ItemType>
class linkedStack: public stackADT<ItemType>
{
public:
const linkedStack<ItemType>& operator=
(const linkedStack<ItemType>&);
//Overload the assignment operator.
bool isEmptyStack() const;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty;
// otherwise returns false.
bool isFullStack() const;
//Function to determine whether the stack is full.
//Postcondition: Returns false.
void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: The stack elements are removed;
// stackTop = nullptr;
void push(const ItemType& newItem);
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of the stack.
ItemType top() const;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top
// element of the stack is returned.
void pop();
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
linkedStack();
//Default constructor
//Postcondition: stackTop = nullptr;
linkedStack(const linkedStack<ItemType>& otherStack);
//Copy constructor
~linkedStack();
//Destructor
//Postcondition: All the elements of the stack are
// removed from the stack.
private:
nodeType<ItemType> *stackTop; //pointer to the stack
void copyStack(const linkedStack<ItemType>& otherStack);
//Function to make a copy of otherStack.
//Postcondition: A copy of otherStack is created and
// assigned to this stack.
};
#include "linkedStack.cpp"
#endif
linkedStack.cpp file
//Default constructor
template <class ItemType>
linkedStack<ItemType>::linkedStack()
{
stackTop = nullptr;
}
template <class ItemType>
bool linkedStack<ItemType>::isEmptyStack() const
{
return(stackTop == nullptr);
} //end isEmptyStack
template <class ItemType>
bool linkedStack<ItemType>:: isFullStack() const
{
return false;
} //end isFullStack
template <class ItemType>
void linkedStack<ItemType>:: initializeStack()
{
nodeType<ItemType> *temp; //pointer to delete the node
while (stackTop != nullptr) //while there are elements in
//the stack
{
temp = stackTop; //set temp to point to the
//current node
stackTop = stackTop->link; //advance stackTop to the
//next node
delete temp; //deallocate memory occupied by temp
}
} //end initializeStack
template <class ItemType>
void linkedStack<ItemType>::push(const ItemType& newElement)
{
nodeType<ItemType> *newNode; //pointer to create the new node
newNode = new nodeType<ItemType>; //create the node
newNode->info = newElement; //store newElement in the node
newNode->link = stackTop; //insert newNode before stackTop
stackTop = newNode; //set stackTop to point to the
//top node
} //end push
template <class ItemType>
ItemType linkedStack<ItemType>::top() const
{
assert(stackTop != nullptr); //if stack is empty,
//terminate the program
return stackTop->info; //return the top element
}//end top
template <class ItemType>
void linkedStack<ItemType>::pop()
{
nodeType<ItemType> *temp; //pointer to deallocate memory
if (stackTop != nullptr)
{
temp = stackTop; //set temp to point to the top node
stackTop = stackTop->link; //advance stackTop to the
//next node
delete temp; //delete the top node
}
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop
template <class ItemType>
void linkedStack<ItemType>::copyStack
(const linkedStack<ItemType>& otherStack)
{
nodeType<ItemType> *newNode, *current, *last;
if (stackTop != nullptr) //if stack is nonempty, make it empty
initializeStack();
if (otherStack.stackTop == nullptr)
stackTop = nullptr;
else
{
current = otherStack.stackTop; //set current to point
//to the stack to be copied
//copy the stackTop element of the stack
stackTop = new nodeType<ItemType>; //create the node
stackTop->info = current->info; //copy the info
stackTop->link = nullptr; //set the link field of the
//node to nullptr
last = stackTop; //set last to point to the node
current = current->link; //set current to point to
//the next node
//copy the remaining stack
while (current != nullptr)
{
newNode = new nodeType<ItemType>;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current = current->link;
}//end while
}//end else
} //end copyStack
//copy constructor
template <class ItemType>
linkedStack<ItemType>::linkedStack(
const linkedStack<ItemType>& otherStack)
{
stackTop = nullptr;
copyStack(otherStack);
}//end copy constructor
//destructor
template <class ItemType>
linkedStack<ItemType>::~linkedStack()
{
initializeStack();
}//end destructor
//overloading the assignment operator
template <class ItemType>
const linkedStack<ItemType>& linkedStack<ItemType>::operator=
(const linkedStack<ItemType>& otherStack)
{
if (this != &otherStack) //avoid self-copy
copyStack(otherStack);
return *this;
}//end operator=