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

implement the Stack ADT using the array-based approach ( using c++ programming l

ID: 3902924 • Letter: I

Question

implement the Stack ADT using the array-based approach ( using c++ programming language ).

Attached below is the stackarray.cpp file & stackarray.h

#include "StackArray.h"

template <typename DataType>

StackArray<DataType>::StackArray(int maxNumber)

{

}

template <typename DataType>

StackArray<DataType>::StackArray(const StackArray& other)

{

}

template <typename DataType>

StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)

{

}

template <typename DataType>

StackArray<DataType>::~StackArray()

{

}

template <typename DataType>

void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error)

{

}

template <typename DataType>

DataType StackArray<DataType>::pop() throw (logic_error)

{

   DataType t=0;

   return t;

}

template <typename DataType>

void StackArray<DataType>::clear()

{

}

template <typename DataType>

bool StackArray<DataType>::isEmpty() const

{

   return false;

}

template <typename DataType>

bool StackArray<DataType>::isFull() const

{

   return false;

}

template <typename DataType>

void StackArray<DataType>::showStructure() const

// Array implementation. Outputs the data items in a stack. If the

// stack is empty, outputs "Empty stack". This operation is intended

// for testing and debugging purposes only.

{

if( isEmpty() ) {

   cout << "Empty stack." << endl;

}

else {

   int j;

   cout << "Top = " << top << endl;

   for ( j = 0 ; j < maxSize ; j++ )

      cout << j << " ";

   cout << endl;

   for ( j = 0 ; j <= top ; j++ )

   {

      if( j == top )

      {

      cout << '[' << dataItems[j] << ']'<< " "; // Identify top

      }

      else

      {

       cout << dataItems[j] << " ";

      }

   }

   cout << endl;

}

cout << endl;

}

—————————

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include <stdexcept>

#include <iostream>

using namespace std;

#include "Stack.h"

template <typename DataType>

class StackArray : public Stack<DataType> {

public:

StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);

StackArray(const StackArray& other);

StackArray& operator=(const StackArray& other);

~StackArray();

void push(const DataType& newDataItem) throw (logic_error);

DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;

bool isFull() const;

void showStructure() const;

private:

int maxSize;

int top;

DataType* dataItems;

};

#endif       //#ifndef STACKARRAY_H

Explanation / Answer

StackArray<DataType>::StackArray(int maxNumber)
{
     DataType *dataItems = new DataType[maxNumber];
     top = -1;
     maxSize = maxNumber;
}
template <typename DataType>
StackArray<DataType>::StackArray(const StackArray& other)
{
        maxNumber = other.maxNumber;
        top = other.top;
        dataItems = new DataType[maxNumber];
        for (int i = 0; i<=top; i++){
            dataItems[i] = other.dataItems[i];
        }
       
}
template <typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{
        maxNumber = other.maxNumber;
        top = other.top;
        delete [] dataItems;
        dataItems = new DataType[maxNumber];
        for (int i = 0; i<=top; i++){
            dataItems[i] = other.dataItems[i];
        }      
}
template <typename DataType>
StackArray<DataType>::~StackArray()
{
       if (dataItems != NULL)
          delete [] dataItems;
}
template <typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
      if (!isFull()){
         top++;
         dataItems[top] = newDataItem;
      }
      else {
          cout << "Stack is full ";
      }
}
template <typename DataType>
DataType StackArray<DataType>::pop() throw (logic_error)
{
   if (isEmpty())
      throw logic_error();
   else {
      int a = top;
      top--;
      return dataItems[a];
   }
}
template <typename DataType>
void StackArray<DataType>::clear()
{
    if (dataItems != NULL){
        delete [] dataItems;
    }
    top = -1;
    maxSize = 0;
    dataItems = NULL;
   
   
}
template <typename DataType>
bool StackArray<DataType>::isEmpty() const
{
   return dataItems == NULL;
}
template <typename DataType>
bool StackArray<DataType>::isFull() const
{
   return top == maxNumber;
}

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