Homework Solution: Write a pseudocode function that uses LinkedStack to determine whether a string is in the language L, where…

    Write a pseudocode function that uses LinkedStack to determine whether a string is in the language L, where a. L = {s : s contains equal numbers of A’s and B’s} b. L = {s : s is of the form An Bn for some n 0} LinkedStack: template<class ItemType> class StackInterface { public: /** Sees whether this stack is empty. @return True if the stack is empty, or false if not. */ virtual bool isEmpty() const = 0;   /** Adds a new entry to the top of this stack. @post If the operation was successful, newEntry is at the top of the stack. @param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */ virtual bool push(const ItemType& newEntry) = 0;   /** Removes the top of this stack. @post If the operation was successful, the top of the stack has been removed. @return True if the removal is successful or false if not. */ virtual bool pop() = 0;   /** Returns the top of this stack. @pre The stack is not empty. @post The top of the stack has been returned, and the stack is unchanged. @return The top of the stack. */ virtual ItemType peek() const = 0; }; // end StackInterface //#endif ////////////////////////////////////////////////////////////// template<class ItemType> class Node { private: ItemType item; // A data item Node<ItemType>* next; // Pointer to next node   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 ; }; // end Node //#include "Node.cpp" //#endif // Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** @file Node.cpp Listing 4-2 */ //#include "Node.h" #include <cstddef> template<class ItemType> Node<ItemType>::Node() : next(nullptr) { } // end default constructor template<class ItemType> Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr) { } // end constructor template<class ItemType> Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) : item(anItem), next(nextNodePtr) { } // end constructor template<class ItemType> void Node<ItemType>::setItem(const ItemType& anItem) { item = anItem; } // end setItem template<class ItemType> void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr) { next = nextNodePtr; } // end setNext template<class ItemType> ItemType Node<ItemType>::getItem() const { return item; } // end getItem template<class ItemType> Node<ItemType>* Node<ItemType>::getNext() const { return next; } // end getNext ////////////////////////////////////////////////////////////// //#ifndef _LINKED_STACK //#define _LINKED_STACK //#include "StackInterface.h" //#include "Node.h" template<class ItemType> class LinkedStack : public StackInterface<ItemType> { private: Node<ItemType>* topPtr; // Pointer to first node in the chain; // this node contains the stack's top   public: // Constructors and destructor: LinkedStack(); // Default constructor LinkedStack(const LinkedStack<ItemType>& aStack);// Copy constructor virtual ~LinkedStack(); // Destructor // Stack operations: bool isEmpty() const; bool push(const ItemType& newItem); bool pop(); ItemType peek() const; }; // end LinkedStack //#include "LinkedStack.cpp" //#endif /////////////////////////////////////////////////////////////// //#include <cassert> // For assert //#include "LinkedStack.h" // Header file template<class ItemType> LinkedStack<ItemType>::LinkedStack() : topPtr(nullptr) { } // end default constructor template<class ItemType> LinkedStack<ItemType>::LinkedStack(const LinkedStack<ItemType>& aStack) { // Point to nodes in original chain Node<ItemType>* origChainPtr = aStack.topPtr;   if (origChainPtr == nullptr) topPtr = nullptr; // Original stack is empty else { // Copy first node topPtr = new Node<ItemType>(); topPtr->setItem(origChainPtr->getItem());   // Point to last node in new chain Node<ItemType>* newChainPtr = topPtr;   // Advance original-chain pointer origChainPtr = origChainPtr->getNext();   // Copy remaining nodes while (origChainPtr != nullptr) { // Get next item from original chain ItemType nextItem = origChainPtr->getItem();   // Create a new node containing the next item Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);   // Link new node to end of new chain newChainPtr->setNext(newNodePtr);   // Advance pointer to new last node newChainPtr = newChainPtr->getNext(); // Advance original-chain pointer origChainPtr = origChainPtr->getNext(); } // end while   newChainPtr->setNext(nullptr); // Flag end of chain } // end if } // end copy constructor template<class ItemType> LinkedStack<ItemType>::~LinkedStack() { // Pop until stack is empty while (!isEmpty()) pop(); } // end destructor template<class ItemType> bool LinkedStack<ItemType>::isEmpty() const { return topPtr == nullptr; } // end isEmpty template<class ItemType> bool LinkedStack<ItemType>::push(const ItemType& newItem) { Node<ItemType>* newNodePtr = new Node<ItemType>(newItem, topPtr); topPtr = newNodePtr; newNodePtr = nullptr; return true; } // end push template<class ItemType> bool LinkedStack<ItemType>::pop() { bool result = false; if (!isEmpty()) { // Stack is not empty; delete top Node<ItemType>* nodeToDeletePtr = topPtr; topPtr = topPtr->getNext(); // Return deleted node to system nodeToDeletePtr->setNext(nullptr); delete nodeToDeletePtr; nodeToDeletePtr = nullptr; result = true; } // end if return result; } // end pop template<class ItemType> ItemType LinkedStack<ItemType>::peek() const { assert(!isEmpty()); // Enforce precondition // Stack is not empty; return top return topPtr->getItem(); } // end getTop // End of implementation file.

    Expert Answer

    Write a pseudoenactment business that uses LinkedStack to particularize whether a string is in the language L, where

    a. L = {s : s contains correspondent gum of A’s and B’s}

    b. L = {s : s is of the fashion An Bn for some n 0}

    LinkedStack:

    template<class IndividualType>

    class StackInterface

    {

    public:

    /** Sees whether this stack is void.

    @yield True if the stack is void, or mock if referable. */

    virtual bool isEmpty() const = 0;

     

    /** Adds a upstartlightlight record to the summit of this stack.

    @post If the exercise was fortunate, upstartlightRecord is at the summit of the stack.

    @param upstartlightRecord The motive to be adventitious as a upstartlightlight record.

    @yield True if the enumeration is fortunate or mock if referable. */

    virtual bool impel(const IndividualType& upstartlightEntry) = 0;

     

    /** Removes the summit of this stack.

    @post If the exercise was fortunate, the summit of the stack

    has been removed.

    @yield True if the dispersion is fortunate or mock if referable. */

    virtual bool explode() = 0;

     

    /** Yields the summit of this stack.

    @pre The stack is referable void.

    @post The summit of the stack has been yielded, and

    the stack is illegal.

    @yield The summit of the stack. */

    virtual IndividualType peek() const = 0;

    }; // object StackInterface

    //#endif

    //////////////////////////////////////////////////////////////

    template<class IndividualType>

    class Node

    {

    private:

    ItemType individual; // A axioms individual

    Node<ItemType>* present; // Pointer to present node

     

    public:

    Node();

    Node(const IndividualType& anItem);

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

    void setItem(const IndividualType& anItem);

    void setNext(Node<ItemType>* presentNodePtr);

    ItemType getItem() const ;

    Node<ItemType>* getNext() const ;

    }; // object Node

    //#include “Node.cpp”

    //#endif

    // Created by Frank M. Carrano and Tim Henry.

    // Delineationright (c) 2013 __Pearson Education__. All hues sly.

    /** @refine Node.cpp

    Listing 4-2 */

    //#include “Node.h”

    #include <cstddef>

    template<class IndividualType>

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

    {

    } // object failure constructor

    template<class IndividualType>

    Node<ItemType>::Node(const IndividualType& anItem) : individual(anItem), present(nullptr)

    {

    }

    // object constructor

    template<class IndividualType>

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

    item(anItem), present(nextNodePtr)

    {

    } // object constructor

    template<class IndividualType>

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

    {

    individual = anItem;

    } // object setItem

    template<class IndividualType>

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

    {

    present = presentNodePtr;

    } // object setNext

    template<class IndividualType>

    ItemType Node<ItemType>::getItem() const

    {

    yield individual;

    } // object getItem

    template<class IndividualType>

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

    {

    yield present;

    } // object getNext

    //////////////////////////////////////////////////////////////

    //#ifndef _LINKED_STACK

    //#define _LINKED_STACK

    //#include “StackInterface.h”

    //#include “Node.h”

    template<class IndividualType>

    class LinkedStack : exoteric StackInterface<ItemType>

    {

    private:

    Node<ItemType>* summitPtr; // Pointer to chief node in the chain;

    // this node contains the stack’s summit

     

    public:

    // Constructors and destructor:

    LinkedStack(); // Failure constructor

    LinkedStack(const LinkedStack<ItemType>& aStack);// Delineation constructor

    virtual ~LinkedStack(); // Destructor

    // Stack exercises:

    bool isEmpty() const;

    bool impel(const IndividualType& upstartlightItem);

    bool explode();

    ItemType peek() const;

    }; // object LinkedStack

    //#include “LinkedStack.cpp”

    //#endif

    ///////////////////////////////////////////////////////////////

    //#include <cassert> // For assert

    //#include “LinkedStack.h” // Header refine

    template<class IndividualType>

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

    {

    } // object failure constructor

    template<class IndividualType>

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

    {

    // Point to nodes in initiatory chain

    Node<ItemType>* origChainPtr = aStack.topPtr;

     

    if (origChainPtr == nullptr)

    topPtr = nullptr; // Initiatory stack is void

    else

    {

    // Delineation chief node

    topPtr = upstartlightlight Node<ItemType>();

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

     

    // Point to definite node in upstartlightlight chain

    Node<ItemType>* upstartlightChainPtr = summitPtr;

     

    // Advance initiatory-chain pointer

    origChainPtr = origChainPtr->getNext();

     

    // Delineation retaining nodes

    occasion (origChainPtr != nullptr)

    {

    // Get present individual from initiatory chain

    ItemType presentIndividual = origChainPtr->getItem();

     

    // Create a upstartlightlight node containing the present individual

    Node<ItemType>* upstartlightNodePtr = upstartlightlight Node<ItemType>(nextItem);

     

    // Link upstartlightlight node to object of upstartlightlight chain

    newChainPtr->setNext(newNodePtr);

     

    // Advance pointer to upstartlightlight definite node

    newChainPtr = upstartlightChainPtr->getNext();

    // Advance initiatory-chain pointer

    origChainPtr = origChainPtr->getNext();

    } // object occasion

     

    newChainPtr->setNext(nullptr); // Flag object of chain

    } // object if

    }

    // object delineation constructor

    template<class IndividualType>

    LinkedStack<ItemType>::~LinkedStack()

    {

    // Explode until stack is void

    occasion (!isEmpty())

    pop();

    }

    // object destructor

    template<class IndividualType>

    bool LinkedStack<ItemType>::isEmpty() const

    {

    yield summitPtr == nullptr;

    } // object isEmpty

    template<class IndividualType>

    bool LinkedStack<ItemType>::push(const IndividualType& upstartlightItem)

    {

    Node<ItemType>* upstartlightNodePtr = upstartlightlight Node<ItemType>(newItem, summitPtr);

    topPtr = upstartlightNodePtr;

    newNodePtr = nullptr;

    yield true;

    } // object impel

    template<class IndividualType>

    bool LinkedStack<ItemType>::pop()

    {

    bool conclusion = mock;

    if (!isEmpty())

    {

    // Stack is referable void; delete summit

    Node<ItemType>* nodeToDeletePtr = summitPtr;

    topPtr = summitPtr->getNext();

    // Yield deleted node to system

    nodeToDeletePtr->setNext(nullptr);

    delete nodeToDeletePtr;

    nodeToDeletePtr = nullptr;

    conclusion = true;

    } // object if

    yield conclusion;

    } // object explode

    template<class IndividualType>

    ItemType LinkedStack<ItemType>::peek() const

    {

    assert(!isEmpty()); // Enforce precondition

    // Stack is referable void; yield summit

    yield summitPtr->getItem();

    } // object getTop

    // Object of implementation refine.

    Expert Defense

     

    Raw Cooyable pseudo enactment:

    Pseudo enactment businesss to impede whether the input string belongs to the attached languages:

    a. L = {s : s contains correspondent gum of A’s and B’s}

    boolean impedeString(string s)

    {

    //variables to hoard the calculate of A and B

    int calculateA=0, calculateB=0;

    //create the entreaty of linked stack to trust stamps

    LinkedStack <char> stackObj=newlight LinkedStack();

    //loop invariant

    int i=0;

    //impel the stamps of the string into the stack

    while(i<s.length())

    {

    //impel the stamp into the stack

    stackObj.push(s[i]);

    }

    //explode the stamps from the stack

    While(!stackObj.isEmpty())

    {

    //extract the summit part

    char summit=stackObj.peek();

     

    //if the stamp is A increment calculate of A

    If(top==’A’)

    countA++;

    //if the stamp is B increment calculate of B

    If(top==’B’)

    countB++;

    }

    //impede the calculate of A and B, whether they are same

    if(countA==countB)

    yield true

    else

    yield mock;

    }

    b. L = {s : s is of the fashion An Bn for some n >0}

    boolean impedeStringb(string s)

    {

    //variables to hoard the calculate of A and B

    int calculateA=0, calculateB=0;

    //create the entreatys of linked stack to trust stamps

    LinkedStack <char> stackObj1=newlight LinkedStack();

    LinkedStack <char> stackObj2=newlight LinkedStack();

    //loop invariant

    int i=0;

    //impel the stamps of the string into the stackObj1

    while(i<s.length())

    {

    //impel the stamp into the stack

    stackObj1.push(s[i]);

    }

    //Create the delineation of stack

    LinkedStack <char> stackObj3=newlight LinkedStack(stackObj1);

    //create the inversion string stack using stackObj3

    //explode the stamps from the stack

    While(!stackObj3.isEmpty())

    {

    //extract the summit part

    char summit=stackObj3.peek();

    //impel into the inversion stack

    stackObj2.push(top);

    //explode the summit

    stackObj2.pop();

    }

    int calculate=0;

    //The occasion loop to impede the stack contents

    While(!stackObj1.isEmpty() && !stackObj1.isEmpty())

    {

    //extract the summit part of stack 1

    char summit1=stackObj1.peek();

     

     

    //extract the summit part of stack 1

    char summit2=stackObj2.peek();

    //explode the stacks

    stackObj2.pop();

    stackObj1.pop();

    if (top2==’A’)

    if(top1!=’B’)

    retrun mock;

     

    count++;

    if (top2==’B’)

    break;

     

    }

    //impede the calculate of A is half the string

    if(count==(s.length()/2))

    yield true

    else

    yield mock;

    }