Question: Complete the following arrayimplementation of STACK in C++ to include two other member functions intpeek() and void flush(). Function intpeek() returns the top element in
Complete the following arrayimplementation of STACK in C++ to include two other member functionsintpeek() and void flush(). Function intpeek() returns the top element in the stack without popping this element off 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:
int *s; int N;
public:
STACK(intmaxN)
{ s = new int[maxN]; N = 0; }
int empty() const
{ return N == 0; }
void push(int item)
{ s[N++] = item; }
int peek()
{
/* implement this part */
}
int pop()
{ return s[--N]; }
void flush()
{
/* also 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 the stack top and display
E : outputs empty if stack is empty; otherwise not empty
K : outputs the stack top integer x usingfunction peek
F : outputs the entire content of the stack in last-in first-out order; empties the stack using function flush
See below for a sample dialogue.
In this assignment, both correctness and efficiency of your programs are important.
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; stack is empty
P 2
E
not empty
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
