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
#include "LinkedStack.h> //Header file
template
LinkedStack
{
}//end default constructor
template class ItemType>
LinkedStack(const LinkedStack
{
//point to nodes in original chain
Node
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
//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
//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
{
//pop until stack is empty
while (!is empty())
pop();
}//end destructor
template
bool LinkedStack
{
return topPtr == nullptr;
} //end is empty
template
bool Linkedstack
{
Node
topPtr = newNodePtr;
newNodePtr = nullptr;
return true;
} // end push
template
bool LinkedStack
{
bool result = false;
if (!isempty())
{
//Stack is not empty; delete top
Node
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
{
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
Get step-by-step solutions from verified subject matter experts
