Question & Answer: 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…..

Please, I need help with this program. I need to make a main and implement the following exercise:

Please write the code in computer.

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 ofAs and Bs} b. L-is:s is of the form A B for some 0 or some n 2

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

Expert Answer

 

for this you can use following code

#include <iostream>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

int count1 = 0;
int count2 = 0;

bool isInLanguageL (string w);

int main()

{
    string input;

    cout << "Input any string; ";
    getline(cin,input);

    if (input.length() % 2 != 0)
        cout <<"Pattern entered does not match the language ";
    else
        isInLanguageL(input);

    return 0;
}

bool isInLanguageL (string w)
{
    stack<string> word1, word2;
    string a, b;

        for (unsigned i = 0; i < w.length()/2; i++)
    {
           a = w.at(i);
           word1.push(a);

    }

    reverse(w.begin(), w.end());

    for (unsigned i = 0; i < w.length()/2; i++)
    {
           b = w.at(i);
           word2.push(b);

    }


while(!word1.empty() && !word2.empty())
{


    word1.pop();
    count1 = count1++;
    word2.pop();
    count2 = count2++;

}

if(count1 == count2)
    return true;
else
    return false;

}
Still stressed from student homework?
Get quality assistance from academic writers!