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 linked list approach Attached is the StackLi

ID: 3903036 • Letter: #

Question

- implement the Stack ADT using the linked list approach

Attached is the StackLinked.cpp and StackLinked.h **USE C++ PROGRAMMING LANGUAGE.

StackLinked.cpp

#include "StackLinked.h"

template <typename DataType>

StackLinked<DataType>::StackLinked (int maxNumber)

{

}

template <typename DataType>

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

{

}

template <typename DataType>

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

{

}

template <typename DataType>

StackLinked<DataType>::~StackLinked()

{

   clear();

}

template <typename DataType>

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

{

  

}

template <typename DataType>

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

{

   DataType t = 0;

   return t;

}

template <typename DataType>

void StackLinked<DataType>::clear()

{

  

}

template <typename DataType>

bool StackLinked<DataType>::isEmpty() const

{

return false;

}

template <typename DataType>

bool StackLinked<DataType>::isFull() const

{

   return false;

}

template <typename DataType>

void StackLinked<DataType>::showStructure() const

{

if( isEmpty() )

{

   cout << "Empty stack" << endl;

}

else

{

cout << "Top ";

   for (StackNode* temp = top; temp != 0; temp = temp->next) {

      if( temp == top ) {

       cout << "[" << temp->dataItem << "] ";

      }

      else {

       cout << temp->dataItem << " ";

      }

   }

cout << "Bottom" << endl;

}

}

------------

StackLinked.h

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include <stdexcept>

#include <iostream>

using namespace std;

#include "Stack.h"

template <typename DataType>

class StackLinked : public Stack<DataType> {

public:

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

StackLinked(const StackLinked& other);

StackLinked& operator=(const StackLinked& other);

~StackLinked();

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:

class StackNode {

public:

   StackNode(const DataType& nodeData, StackNode* nextPtr)

   {

       dataItem = nodeData;

       next = nextPtr;

   }

   DataType dataItem;

   StackNode* next;

};

StackNode* top;

};

#endif       //#ifndef STACKARRAY_H

Explanation / Answer

template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
     maxSize = maxNumber //StackLinked should have a data member maxSize to hold the maximum allowed elements
     top = NULL;
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
     maxSize = other.maxSize;
     StackNode *p = other.top;
     while (p != NULL){
          push(p->dataItem);
          p = p->next;
     }
}
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
     maxSize = other.maxSize;
     StackNode *p = other.top;
     while (p != NULL){
          push(p->dataItem);
          p = p->next;
     }
}
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
   StackNode *p;
   while (top != NULL){
       p = top;
       top = top->next;
       delete p;
      
   }
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
     int count = 0;
     StackNode *p = top;
     StackNode *q = new StackNode(newDataItem, NULL);
     while(p!=NULL){
        count++;
        p = p->next;
     }
     if (count < maxNumber){
         q->next = top;
         top = q;       
     }
     else {
         cout << "Stack is full ";
         throw new logic_error();
     }
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
      StackNode *p = top;
      DataType t = top->dataItem;
      top = top->next;
      delete p;
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
   StackNode *p;
   while (top != NULL){
       p = top;
       top = top->next;
       delete p;
      
   }
      
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
    return top == NULL;
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
     int count = 0;
     StackNode *p = top;
     while(p!=NULL){
        count++;
        p = p->next;
     }
     return count == maxNumber;
}