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 linkedStack void reverseStack (stackType<Type>

ID: 3559795 • Letter: A

Question

A) Add the following operation to linkedStack

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

First is my header file, followed by my program code.

//Header File: linkedStack.h

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

#include "stackADT.h"

using namespace std;


//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};

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


bool isFullStack() const;

void initializeStack();

void push(const Type& newItem);

Type top() const;

void pop();

linkedStackType();

linkedStackType(const linkedStackType<Type>& otherStack);

~linkedStackType();


private:
nodeType<Type> *stackTop; //pointer to the stack

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

};

//Default constructor
template <class Type>
linkedStackType<Type>::linkedStackType()
{
stackTop = NULL;
}

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

template <class Type>
bool linkedStackType<Type>:: isFullStack() const
{
return false;
} //end isFullStack

template <class Type>
void linkedStackType<Type>:: initializeStack()
{
nodeType<Type> *temp; //pointer to delete the node

while (stackTop != NULL) //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 Type>
void linkedStackType<Type>::push(const Type& newElement)
{
nodeType<Type> *newNode; //pointer to create the new node

newNode = new nodeType<Type>; //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 Type>
Type linkedStackType<Type>::top() const
{
assert(stackTop != NULL); //if stack is empty,
//terminate the program
return stackTop->info; //return the top element
}//end top

template <class Type>
void linkedStackType<Type>::pop()
{
nodeType<Type> *temp; //pointer to deallocate memory

if (stackTop != NULL)
{
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 Type>
void linkedStackType<Type>::copyStack
(const linkedStackType<Type>& otherStack)
{
nodeType<Type> *newNode, *current, *last;

if (stackTop != NULL) //if stack is nonempty, make it empty
initializeStack();

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

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

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

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

//copy constructor
template <class Type>   
linkedStackType<Type>::linkedStackType(
const linkedStackType<Type>& otherStack)
{
stackTop = NULL;
copyStack(otherStack);
}//end copy constructor

//destructor
template <class Type>
linkedStackType<Type>::~linkedStackType()
{
initializeStack();
}//end destructor

//overloading the assignment operator
template <class Type>   
const linkedStackType<Type>& linkedStackType<Type>::operator=
           (const linkedStackType<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

here your funtion goes:

//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 otherStack is nonempty, make it empty
initializeStack();

if (stackTop == NULL) // means first stack is empty so otherStack must be empty
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