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

Below is a project that defines a static based array stack. Please identify & co

ID: 3720237 • Letter: B

Question

Below is a project that defines a static based array stack. Please identify & correct the incorrect parts of the code.

/*-- Stack.h ---------------------------------------------------

This header file defines a Stack data type.

Basic operations:

    constructor: Constructs an empty stack

    empty:        Checks if a stack is empty

    push:         Modifies a stack by adding a value at the top

    top:          Retrieves the top stack value; leaves stack unchanged

    pop:          Modifies stack by removing the value at the top

    display:      Displays all the stack elements, starting from top

Class Invariant:

    1. The stack elements (if any) are stored in positions

       0, 1, . . ., myTop of myArray.

    2. -1 <= myTop < STACK_CAPACITY

--------------------------------------------------------------*/

#include <iostream>

#ifndef STACK

#define STACK

const int STACK_CAPACITY = 128;

typedef int StackElement;

class Stack

{

public:

Stack();

bool empty() const;

void push(const StackElement & value);

void display(ostream & out) const;

StackElement top() const;

void pop();

private:

StackElement myArray[STACK_CAPACITY];

int myTop;

};

}

Explanation / Answer

here is your modified code : -------->>>>>>>

#include <iostream>
using namespace std;


//--- Definition of Stack constructor
Stack::Stack():myTop(-1){}

bool Stack::empty() const
{
   return (myTop == -1);  
}


void Stack::push(const StackElement & value)
{
   if (myTop < STACK_CAPACITY-1)
   {
   ++myTop;
myArray[myTop] = value;   
   }
   else
   {
      cerr << "*** Stack full -- can't add new value *** "
              "Must increase value of STACK_CAPACITY in Stack.h ";
      exit(1);
   }
}


void Stack::display(ostream & out)const
{
   for(int i = myTop; i >= 0;i--)
      out << myArray[i] << endl;
}


StackElement Stack::top() const
{
if(empty()){
  cerr<<"***Stack is empty ***";
  exit(1);
}
return (myArray[myTop])
}

void Stack::pop()
{
if(empty()){
  cerr<<"***Stack is empty ***";
  exit(1);
}
myTop--;
}

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