Write an singly linked list class that inherits the LeakyStackInterface to imple
ID: 3821639 • Letter: W
Question
Write an singly linked list class that inherits the LeakyStackInterface to implement 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 <ItemType>
class LeakyStackInterface
{
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 with creating a class that inherits this interface and makes a stack using a single linked list. It must accept a push even when it's full. When it is full, 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!
Explanation / Answer
#include #include #include void main() { struct node { int num; struct node *ptr; }; typedef struct node NODE; NODE *head, *first, *temp = 0; int count = 0; int choice = 1; first = 0; while (choice) { head = (NODE *)malloc(sizeof(NODE)); printf("Enter the data item "); scanf("%d", &head-> num); if (first != 0) { temp->ptr = head; temp = head; } else { first = temp = head; } fflush(stdin); printf("Do you want to continue(Type 0 or 1)? "); scanf("%d", &choice); } temp->ptr = 0; /* reset temp to the beginning */ temp = first; printf(" status of the linked list is "); while (temp != 0) { printf("%d=>", temp->num); count++; temp = temp -> ptr; } printf("NULL "); printf("No. of nodes in the list = %d ", count); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.