Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following arrayimplementation of STACK in C++ to include two other

ID: 3672007 • Letter: C

Question

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 user’s 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 <ENTER>

empty

P 1

P 10

E <ENTER>

not empty

K

10

P 19

R

19

R

10

E <ENTER>

not empty

P 5

P 20

F

20 5 1

E <ENTER>

empty

R

nothing to pop off; stack is empty

P 2

E <ENTER>

not empty

Explanation / Answer

/**
   * Creates a Stack of the size initialCapacity
   */
   public ArrayStack(int initialCapacity)
   {
      if (initialCapacity <= 0)
         A = (AnyType[]) new Object[DEFAULT_CAPACITY];
      else
         A = (AnyType[]) new Object[initialCapacity];

      top = -1;   //stack is empty
   }

/**
   * Creates a Stack with the default capacity
   */
   public ArrayStack()
   {
      this(DEFAULT_CAPACITY);
   }

/**
   * Tests if the stack is empty.
   */
   public boolean isEmpty()
   {
      return top==-1;
   }

/**
   * Returns the top item without its removal.
   */
   public AnyType peek()
   {
      if (isEmpty()) throw new StackException("Stack is empty");
      return A[top];
   }

/**
   * Removes and returns the item at the top of this stack.
   */
   public AnyType pop()
   {
      AnyType x = peek();
      A[top] = null;    //make sure the object is destroyed
      top--;
      return x;
   }

/**
   * Inserts an item onto the top of the stack.
   */
   public void push(AnyType e)
   {
      if (top == A.length) throw new StackException("Stack has overflowed");
       top++;
      A[top] = e;
   }

/**
   * Removes all items from the Stack.
   */
   public void clear()
   {
      for(int i = 0; i <= top; i++)
         A[i] = null;

      top = -1;
   }

/**
   * Returns a string representation of the Stack.
   */
   public String toString()
   {
      if(isEmpty()) return "[ ]";

      StringBuffer out = new StringBuffer("[");
      for(int i = 0; i < top; i++)
         out.append(A[i] + ", ");

      out.append(A[top] + "]");
      return out.toString();
   }

   public static void main(String[] args)
   {
      ArrayStack<Integer> s = new ArrayStack<Integer>(6);

      try
      {

         for(int i = 0; i < 6; i++) s.push(i);

         //s.clear();
         System.out.println(s);

         for(int i = 0; i < 5; i++) s.pop();

         System.out.println(s);

      }
      catch (StackException e)
      {
         System.err.println(e);
      }
   }
}

   /**              StackInterface           **/

interface StackInterface<AnyType>
{
/**
   * Tests if the stack is empty.
   */
   public boolean isEmpty();

/**
   * Removes and returns the item at the top of this stack.
   */
   public AnyType pop() throws StackException;

/**
   * Returns the top item without its removal
   */
   public AnyType peek() throws StackException;

/**
   * Inserts an item onto the top of the stack.
   */
   public void push(AnyType e) throws StackException;

/**
   * Removes all items from the stack.
   */
   public void clear();
}


   /**              StackException           **/


class StackException extends RuntimeException
{
   public StackException(String name)
   {
      super(name);
   }

   public StackException()
   {
      super();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote