Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the following linked list implementation of STACK in C++ to include two other member functions int peek() and void flush(). Function int peek() returns

Complete the following linked list implementation of STACK in C++ to include two other member functions int peek() and void flush(). Function int peek() returns the top element of the stack without popping this element of the stack. Function void flush() displays the entire stack content, and empties the stack. Do not call empty() and pop() functions in flush(). Function flush() displays an empty line if the stack is empty. class STACK { private: struct node { int item; node* next; node(int x, node* t) { item = x; next = t; } }; typedef node *link; link head; public: STACK(int) { head = 0; } int empty() const { return head == 0; } void push(int x) { head = new node(x, head); } int peek() { /* implement this part */ } int pop() { int v = head->item; link t = head->next; delete head; head = t; return v; } void flush() { /* implement this part */ } }; Write a C++ program that creates a stack of integer numbers (for up to 100 numbers), and then it does the following on users input (your program must check for conditions: e.g. no pop when the stack is empty): P x : pushes integer x onto the stack R : pops off and displays the stack top E : outputs empty if stack is empty; otherwise not empty K : outputs the stack top integer x using function peek F : outputs the entire content of the stack in last-in-first-out order; empties the stack using function flush Example Dialogue (input can be read from a specific file, or can be directed to a file): E empty P 1 P 10 E not empty K 10 P 19 R 19 R 10 E not empty P 5 P 20 F 20 5 1 E empty R nothing to pop off; the stack is empty P 2 E not empty

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago