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

Please, I need help with this program (I would like the program to be written in

ID: 3884310 • Letter: P

Question

Please, I need help with this program (I would like the program to be written in computer please). I need to make a main and implement the following exercise:

LinkedStack.h

#pragma once

#include <cassert>

#include "StackInterface.h"

#include "Node.h"

template<class ItemType>

class LinkedStack : public StackInterface<ItemType>

{

private:

Node<ItemType>* topPtr;

  

public:

LinkedStack();   

LinkedStack(const LinkedStack<ItemType>& aStack);

virtual ~LinkedStack();   

bool isEmpty() const;

bool push(const ItemType& newItem);

bool pop();

ItemType peek() const;

};

template<class ItemType>

LinkedStack<ItemType>::LinkedStack() : topPtr(nullptr)

{

}

template<class ItemType>

LinkedStack<ItemType>::LinkedStack(const LinkedStack<ItemType>& aStack)

{

Node<ItemType>* origChainPtr = aStack.topPtr;

if (origChainPtr == nullptr)

topPtr = nullptr;

else

{

topPtr = new Node<ItemType>();

topPtr->setItem(origChainPtr->getItem());

Node<ItemType>* newChainPtr = topPtr;

origChainPtr = origChainPtr->getNext();

while (origChainPtr != nullptr)

{

ItemType nextItem = origChainPtr->getItem();

Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);

newChainPtr->setNext(newNodePtr);

newChainPtr = newChainPtr->getNext();

origChainPtr = origChainPtr->getNext();

}

newChainPtr->setNext(nullptr);   

}

}

template<class ItemType>

LinkedStack<ItemType>::~LinkedStack()

{

while (!isEmpty())

pop();

}

template<class ItemType>

bool LinkedStack<ItemType>::isEmpty() const

{

return topPtr == nullptr;

}

template<class ItemType>

bool LinkedStack<ItemType>::push(const ItemType& newItem)

{

Node<ItemType>* newNodePtr = new Node<ItemType>(newItem, topPtr);

topPtr = newNodePtr;

newNodePtr = nullptr;

return true;

}

template<class ItemType>

bool LinkedStack<ItemType>::pop()

{

bool result = false;

if (!isEmpty())

{

Node<ItemType>* nodeToDeletePtr = topPtr;

topPtr = topPtr->getNext();

nodeToDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDeletePtr = nullptr;

result = true;

}

return result;

}

template<class ItemType>

ItemType LinkedStack<ItemType>::peek() const

{

assert(!isEmpty());

return topPtr->getItem();

}

StackInterface.h

#pragma once

template<class ItemType>

class StackInterface

{

public:

virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;

virtual bool pop() = 0;

virtual ItemType peek() const = 0;

};

Node.h

#pragma once

#include <cstddef>

template<class ItemType>

class Node

{

private:

ItemType item;

Node<ItemType>* next;

public:

Node();

Node(const ItemType& anItem);

Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);

void setItem(const ItemType& anItem);

void setNext(Node<ItemType>* nextNodePtr);

ItemType getItem() const ;

Node<ItemType>* getNext() const ;

};

template<class ItemType>

Node<ItemType>::Node() : next(nullptr)

{

}

template<class ItemType>

Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr)

{

}

template<class ItemType>

Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) :

item(anItem), next(nextNodePtr)

{

}

template<class ItemType>

void Node<ItemType>::setItem(const ItemType& anItem)

{

item = anItem;

}

template<class ItemType>

void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)

{

next = nextNodePtr;

}

template<class ItemType>

ItemType Node<ItemType>::getItem() const

{

return item;

}

template<class ItemType>

Node<ItemType>* Node<ItemType>::getNext() const

{

return next;

}

9. Write a pseudocode function that uses a stack to determine whether a string is in the language L, where a. L {s : s contains equal numbers ofA's and B's} b. L-is:s is of the form A" B" for some 0 or some n 2

Explanation / Answer

#pragma once

#include <cassert>

#include "StackInterface.h"

#include "Node.h"

template<class ItemType>

class LinkedStack : public StackInterface<ItemType>

{

private:

Node<ItemType>* topPtr;

                       

public:

LinkedStack();                        

   LinkedStack(const LinkedStack<ItemType>& aStack);

virtual ~LinkedStack();                        

bool isEmpty() const;

bool push(const ItemType& newItem);

bool pop();

ItemType peek() const;

};

template<class ItemType>

LinkedStack<ItemType>::LinkedStack() : topPtr(nullptr)

{

}

template<class ItemType>

LinkedStack<ItemType>::LinkedStack(const LinkedStack<ItemType>& aStack)

{

Node<ItemType>* origChainPtr = aStack.topPtr;

if (origChainPtr == nullptr)

topPtr = nullptr;

else

{

topPtr = new Node<ItemType>();

topPtr->setItem(origChainPtr->getItem());

Node<ItemType>* newChainPtr = topPtr;

origChainPtr = origChainPtr->getNext();

while (origChainPtr != nullptr)

{

ItemType nextItem = origChainPtr->getItem();

Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);

newChainPtr->setNext(newNodePtr);

newChainPtr = newChainPtr->getNext();

origChainPtr = origChainPtr->getNext();

}

newChainPtr->setNext(nullptr);          

}

}

template<class ItemType>

LinkedStack<ItemType>::~LinkedStack()

{

while (!isEmpty())

pop();

}

template<class ItemType>

bool LinkedStack<ItemType>::isEmpty() const

{

return topPtr == nullptr;

}

template<class ItemType>

bool LinkedStack<ItemType>::push(const ItemType& newItem)

{

Node<ItemType>* newNodePtr = new Node<ItemType>(newItem, topPtr);

topPtr = newNodePtr;

newNodePtr = nullptr;

return true;

}

template<class ItemType>

bool LinkedStack<ItemType>::pop()

{

bool result = false;

if (!isEmpty())

{

Node<ItemType>* nodeToDeletePtr = topPtr;

topPtr = topPtr->getNext();

nodeToDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDeletePtr = nullptr;

result = true;

}

return result;

}

template<class ItemType>

ItemType LinkedStack<ItemType>::peek() const

{

assert(!isEmpty());

return topPtr->getItem();

}

StackInterface.h

#pragma once

template<class ItemType>

class StackInterface

{

public:

   virtual bool isEmpty() const = 0;

   virtual bool push(const ItemType& newEntry) = 0;

   virtual bool pop() = 0;

   virtual ItemType peek() const = 0;

};

Node.h

#pragma once

#include <cstddef>

template<class ItemType>

class Node

private:

   ItemType item;

   Node<ItemType>* next;

  

public:

   Node();

   Node(const ItemType& anItem);

   Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);

   void setItem(const ItemType& anItem);

   void setNext(Node<ItemType>* nextNodePtr);

   ItemType getItem() const ;

   Node<ItemType>* getNext() const ;

};

template<class ItemType>

Node<ItemType>::Node() : next(nullptr)

{

}

template<class ItemType>

Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr)

{

}

template<class ItemType>

Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) :

item(anItem), next(nextNodePtr)

{

}

template<class ItemType>

void Node<ItemType>::setItem(const ItemType& anItem)

{

item = anItem;

}

template<class ItemType>

void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr

{

next = nextNodePtr;

}

template<class ItemType>

ItemType Node<ItemType>::getItem() const

{

return item;

}

template<class ItemType>

Node<ItemType>* Node<ItemType>::getNext() const

{

return next;

}