Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Using the following templete class: #include StackP.h // header file #include // for cout using namespace std; template Stack :: Stack() : topPtr(nullptr) { }

Using the following templete class:

#include "StackP.h" // header file #include  // for cout using namespace std; template  Stack :: Stack() : topPtr(nullptr) { } template  Stack :: Stack(const Stack& aStack) { if (aStack.topPtr == nullptr) topPtr = nullptr; // original list is empty else { // copy first node topPtr = new StackNode; topPtr->item = aStack.topPtr->item; // copy rest of list StackNode *newPtr = topPtr; // new list pointer for (StackNode *origPtr = aStack.topPtr->next; origPtr != nullptr; origPtr = origPtr->next) { newPtr->next = new StackNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = nullptr; } } template  Stack ::~Stack() { // pop until stack is empty while (!isEmpty()) pop(); } template  bool Stack ::isEmpty() const { return topPtr == nullptr; } template  bool Stack ::push(const StackItem &newItem) { // create a new node StackNode *newPtr = new StackNode; newPtr->item = newItem; // insert the new node newPtr->next = topPtr; topPtr = newPtr; return true; } template  bool Stack ::pop(StackItem& stackTop) { if (isEmpty()) return false; else { // stack is not empty; retrieve and delete top stackTop = topPtr->item; StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = nullptr; // safeguard delete temp; return true; } } template  bool Stack ::pop() { if (isEmpty()) return false; else { // stack is not empty; delete top StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = nullptr; // safeguard delete temp; return true; } } template  bool Stack ::getTop(StackItem& stackTop) const { if (isEmpty()) return false; else { // stack is not empty; retrieve top stackTop = topPtr->item; return true; } } 

Write a method to remove the element at the bottom of the stack. Dont change the other elements. The method is a member of the class. Do not use other class methods in your solution.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions