Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A) Add the following operation to myStack void reverseStack (stackType<Type> &ot

ID: 3559796 • Letter: A

Question

A) Add the following operation to myStack

void reverseStack (stackType<Type> &otherStack);

This operation copies the elements of a stack into reverse order onto another stack.

Consider the following statements;
stackType<int> stack1;
stackType<int> stack2;

The statement
stack1.reverseStack(stack2);
copies the elements of stack1 onto stack2 in reverse order. That is the top element of stack1 is the bottom element of stack2, and so on. The older contents of stack2 are destroyed and stack1 is unchanged.

B) Write the definition of the function template to implement the operation reverseStack

Listed first is my header, followed by my code

//Header file: myStack.h

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

#include "stackADT.h"

using namespace std;

template <class Type>
class stackType: public stackADT<Type>
{
public:
const stackType<Type>& operator=(const stackType<Type>&);

void initializeStack();

bool isEmptyStack() const;

bool isFullStack() const;

void push(const Type& newItem);

Type top() const;

void pop();

stackType(int stackSize = 100);

stackType(const stackType<Type>& otherStack);

~stackType();


private:
int maxStackSize;
int stackTop;   
Type *list;

void copyStack(const stackType<Type>& otherStack);

};

template <class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}//end initializeStack

template <class Type>
bool stackType<Type>::isEmptyStack() const
{
return(stackTop == 0);
}//end isEmptyStack

template <class Type>
bool stackType<Type>::isFullStack() const
{
return(stackTop == maxStackSize);
} //end isFullStack

template <class Type>
void stackType<Type>::push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem; //add newItem to the
//top of the stack
stackTop++; //increment stackTop
}
else
cout << "Cannot add to a full stack." << endl;
}//end push

template <class Type>
Type stackType<Type>::top() const
{
assert(stackTop != 0); //if stack is empty,
//terminate the program
return list[stackTop - 1]; //return the element of the
//stack indicated by
//stackTop - 1
}//end top

template <class Type>
void stackType<Type>::pop()
{
if (!isEmptyStack())
stackTop--; //decrement stackTop
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop

template <class Type>
stackType<Type>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must "
<< "be positive." << endl;
cout << "Creating an array of size 100." << endl;

maxStackSize = 100;
}
else
maxStackSize = stackSize; //set the stack size to
//the value specified by
//the parameter stackSize

stackTop = 0; //set stackTop to 0
list = new Type[maxStackSize]; //create the array to
//hold the stack elements
}//end constructor

template <class Type>
stackType<Type>::~stackType() //destructor
{
delete [] list; //deallocate the memory occupied
//by the array
}//end destructor

template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;                 
maxStackSize = otherStack.maxStackSize;         
stackTop = otherStack.stackTop;             
  
list = new Type[maxStackSize];                    

//copy otherStack into this stack
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
} //end copyStack


template <class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;

copyStack(otherStack);
}//end copy constructor

template <class Type>
const stackType<Type>& stackType<Type>::operator=
                    (const stackType<Type>& otherStack)
{
if (this != &otherStack) //avoid self-copy
copyStack(otherStack);

return *this;
} //end operator=   

#endif

#include <iostream>
#include "linkedStack.h"

using namespace std;

void testCopy(linkedStackType<int> OStack);

int main()
{
linkedStackType<int> stack;
linkedStackType<int> otherStack;
linkedStackType<int> newStack;

//Add elements into stack
stack.push(34);
stack.push(43);
stack.push(27);

//Use the assignment operator to copy the elements
//of stack into newStack
newStack = stack;

cout << "After the assignment operator, newStack: "
<< endl;

//Output the elements of newStack
while (!newStack.isEmptyStack())
{
cout << newStack.top() << endl;  
newStack.pop();
}

//Use the assignment operator to copy the elements
//of stack into otherStack
otherStack = stack;

cout << "Testing the copy constructor." << endl;

testCopy(otherStack);

cout << "After the copy constructor, otherStack: " << endl;

while (!otherStack.isEmptyStack())
{
cout << otherStack.top() << endl;  
otherStack.pop();
}

return 0;
}

//Function to test the copy constructor
void testCopy(linkedStackType<int> OStack)
{
cout << "Stack in the function testCopy:" << endl;

while (!OStack.isEmptyStack())
{
cout << OStack.top() << endl;  
OStack.pop();
}
}

Explanation / Answer


//reversing the stack;
template <class Type>
void linkedStackType<Type>::reverseStack
(const linkedStackType<Type> &otherStack)
{
  
   nodeType<Type> *newNode, *current, *last;
Type data;
  
   if (otherStack.stackTop != NULL) //if stack is nonempty, make it empty
otherStack.initializeStack();

if (stackTop == NULL)
otherStack.stackTop = NULL;
else
{
  
current = stackTop; //set current to point
//to the stack to be reversed;

//copy the stackTop element of the stack
otherStack.stackTop = new nodeType<Type>; //create the node

otherStack.stackTop->info = current->info; //copy the info
otherStack.stackTop->link = NULL; //set the link field of the
//node to NULL
last = otherStack.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 != NULL)
{
newNode = new nodeType<Type>;

newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}//end while
}//end else  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote