Question: C++ Data structure Implement the codes must use the LinkedStack implementation Use this program provided /**file LinkedStack.cpp*/ showing below to resolve the problem 1 Problem1.

C++ Data structure

Implement the codes must use the LinkedStack implementation

Use this program provided /**file LinkedStack.cpp*/ showing below to resolve the problem 1

Problem1. Write the client that removes all negative numbers from a stack of int objects. If the original stack contained the integers 30, -15, 20, -25 (top of stack), the new stack should contain the integers 30, 20.

C++ CODE

/**file LinkedStack.cpp*/

#include //For assert

#include "LinkedStack.h> //Header file

template

LinkedStack::LinkedStack() : topPtr(nullptr)

{

}//end default constructor

template class ItemType>

LinkedStack(const LinkedStack& aStack)

{

//point to nodes in original chain

Node* origChainPtr = astack->topPtr;

if (origChainPtr == nullptr)

topPtr == nullptr;//original bag is empty

else

{

//copy first node

topPtr = new Node();

topPtr->setItem(origChainPtr->getItem());

//point to first node in new chain

Node* newChainPtr = topPtr;

//copy remaining nodes

while (origChainPtr = origChainPtr->getNext();

//Advance original-chain pointer

origChainPtr = origChainPtr->getNext();

//Get next item from original chain

ItemType nextItem = origChainPtr->getItem();

//Create a new node containing the next item

Node* newNodePtr = new Node(nextItem);

//Link new node to end of new chain

newChainPtr->setNext(newNodePtr);

//Link new node to end of new chain

newChainPtr->setNext(newNodePtr);

//Advance pointer to new last node

newChainPtr = newChainPtr->getNext();

}//end while

newChainPtr->setNext(nullptr); //Flag end of chain

} //end if

} //end copy constructor

template

LinkedStack::~LinkedStack()

{

//pop until stack is empty

while (!is empty())

pop();

}//end destructor

template

bool LinkedStack::isEmpty() const

{

return topPtr == nullptr;

} //end is empty

template

bool Linkedstack::push(const ItemType& newItem)

{

Node* newNodePtr = new Node(newItem, topPtr);

topPtr = newNodePtr;

newNodePtr = nullptr;

return true;

} // end push

template

bool LinkedStack::pop()

{

bool result = false;

if (!isempty())

{

//Stack is not empty; delete top

Node* nodeTo DeletePtr = topPtr;

topPtr = topPtr->getNext();

//return deleted node to system

nodetoDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDelePtr = nullPtr;

result = true;

} //end if

return result;

}//end pop

template

ItemType LinledStack::peek() const

{

assert(!isEmpty());//Enforce precondition

//Stack is not empty: return top

return topPtr->getItem();

} //end getTop

//end of implementation file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!