Question: Write an array-based stack class that inherits the LeakyStackInterface to impliment a leaky stack. When such a stack is full, and push is invoked, rather
Write an array-based stack class that inherits the LeakyStackInterface to impliment a "leaky stack". When such a stack is full, and push is invoked, rather than throwing an exception, a more typical approach is to accept the pushed element at the top, while removing the oldest element from the bottom of the stack to make room. This is known as leaking. Note that this does not mean that the ADT exposes a method to allow removal from the bottom directly. This is only performed when the stack becomes full.
#ifndef LEAKY_STACK_INTERFACE
#define LEAKY_STACK_INTERFACE template
{
public: //returns whether stack is empty or not
virtual bool isEmpty() const = 0;
//adds a new entry to the top of the stack //if the stack is full, the bottom item is removed //or "leaked" first, and then the new item is set to the top //---> If the stack was full when the push was attempted, return false //---> If the stack was not full when the push was attempted, return true
virtual bool push(const ItemType& newEntry) = 0; //remove the top item //if the stack is empty, return false to indicate failure virtual bool pop() = 0; //return a copy of the top of the stack
virtual ItemType peek() const = 0; //destroys the stack and frees up memory //that was allocated
virtual ~StackInterface() {} }; #endif
Hello. I need help on creating a class that inherits this interface and makes a stack using an array. It must accept a push even when its full. When it is full, I think the bottom peice of data in the stack must get dropped in order for the pushed item to go into the stack. Any help with this is greatly appreciated!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
