Homework Solution: (C++ Data Structure) Having problems with ths code, I'm supposed to write a pseudocode fu…

    (C++ Data Structure) Having problems with ths code, I'm supposed to 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} and creat a main. 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. Functions: #include <iostream> using namespace std; bool checkString(string s) { //variables to store the count of A and B   int countA=0, countB=0;   //create the instance of linked stack to hold characters   LinkedStack <char> stackObj= new LinkedStack(); //loop invariant int i=0; //push the characters of the string into the stack while(i<s.length()) { //push the character into the stack stackObj.push(s[i]); } //pop the characters from the stack while (!stackObj.isEmpty())   { //extract the top element char top=stackObj.peek();   //if the character is A increment count of A if (top =='A') countA++; //if the character is B increment count of B if(top=='B') countB++; } //check the count of A and B, whether they are same   if(countA==countB)   return true;   else   return false; } bool checkStringb(string s) {   //variables to store the count of A and B   int countA=0, countB=0;   //create the instances of linked stack to hold characters   LinkedStack <char> stackObj1= new LinkedStack();   LinkedStack <char> stackObj2= new LinkedStack();   //loop invariant   int i=0;   //push the characters of the string into the stackObj1   while(i<s.length())   {   //push the character into the stack   stackObj1.push(s[i]);   }   //Create the copy of stack   LinkedStack <char> stackObj3= new LinkedStack(stackObj1);   //create the reverse string stack using stackObj3   //pop the characters from the stack   while (!stackObj3.isEmpty()) { //extract the top element char top=stackObj3.peek(); //push into the reverse stack stackObj2.push(top); //pop the top   stackObj2.pop(); } int count=0; //The while loop to check the stack contents while (!stackObj1.isEmpty() && !stackObj1.isEmpty()) { //extract the top element of stack 1 char top1=stackObj1.peek(); //extract the top element of stack 1 char top2=stackObj2.peek(); //pop the stacks stackObj2.pop(); stackObj1.pop(); if (top2=='A') if(top1!='B') return false; count++; if (top2=='B') break; } //check the count of A is half the string if(count==(s.length()/2)) return true; else return false; } int main() { return 0; }

    Expert Answer

     
    a) Pseudo Code: function whether_string_is_in_L(str){

    (C++ Grounds Structure) Having problems with ths regulation, I’m reckoned to transcribe a pseudoregulation discharge that uses LinkedStack to detail whether a string is in the language L, where

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

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

    and creat a deep.

    LinkedStack:

    template<class PartType>

    class StackInterface

    {

    public:

    /** Sees whether this stack is vacuity.

    @reappear True if the stack is vacuity, or untrue if referable. */

    virtual bool isEmpty() const = 0;

     

    /** Adds a novellightlightlight register to the culmination of this stack.

    @post If the action was happy, novellightlightRegister is at the culmination of the stack.

    @param novellightlightRegister The sight to be ascititious as a novellightlightlight register.

    @reappear True if the restitution is happy or untrue if referable. */

    virtual bool butt(const PartType& novellightlightEntry) = 0;

     

    /** Removes the culmination of this stack.

    @post If the action was happy, the culmination of the stack

    has been removed.

    @reappear True if the non-location is happy or untrue if referable. */

    virtual bool explode() = 0;

     

    /** Reappears the culmination of this stack.

    @pre The stack is referable vacuity.

    @post The culmination of the stack has been reappeared, and

    the stack is unchanged.

    @reappear The culmination of the stack. */

    virtual PartType peek() const = 0;

    }; // object StackInterface

    //#endif

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

    template<class PartType>

    class Node

    {

    private:

    ItemType part; // A grounds part

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

     

    public:

    Node();

    Node(const PartType& anItem);

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

    void setItem(const PartType& anItem);

    void setNext(Node<ItemType>* contiguousNodePtr);

    ItemType getItem() const ;

    Node<ItemType>* getNext() const ;

    }; // object Node

    //#include “Node.cpp”

    //#endif

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

    // Portraitureright (c) 2013 __Pearson Education__. Perfect rights silent.

    /** @rasp Node.cpp

    Listing 4-2 */

    //#include “Node.h”

    #include <cstddef>

    template<class PartType>

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

    {

    } // object absence constructor

    template<class PartType>

    Node<ItemType>::Node(const PartType& anItem) : part(anItem), contiguous(nullptr)

    {

    } // object constructor

    template<class PartType>

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

    item(anItem), contiguous(nextNodePtr)

    {

    } // object constructor

    template<class PartType>

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

    {

    part = anItem;

    } // object setItem

    template<class PartType>

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

    {

    contiguous = contiguousNodePtr;

    } // object setNext

    template<class PartType>

    ItemType Node<ItemType>::getItem() const

    {

    reappear part;

    } // object getItem

    template<class PartType>

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

    {

    reappear contiguous;

    } // object getNext

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

    //#ifndef _LINKED_STACK

    //#define _LINKED_STACK

    //#include “StackInterface.h”

    //#include “Node.h”

    template<class PartType>

    class LinkedStack : general StackInterface<ItemType>

    {

    private:

    Node<ItemType>* culminationPtr; // Pointer to pristine node in the chain;

    // this node contains the stack’s culmination

     

    public:

    // Constructors and destructor:

    LinkedStack(); // Absence constructor

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

    virtual ~LinkedStack(); // Destructor

     

    // Stack actions:

    bool isEmpty() const;

    bool butt(const PartType& novellightlightItem);

    bool explode();

    ItemType peek() const;

    }; // object LinkedStack

    //#include “LinkedStack.cpp”

    //#endif

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

    //#include <cassert> // For assert

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

    template<class PartType>

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

    {

    } // object absence constructor

    template<class PartType>

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

    {

    // Point to nodes in primeval chain

    Node<ItemType>* origChainPtr = aStack.topPtr;

     

    if (origChainPtr == nullptr)

    topPtr = nullptr; // Primeval stack is vacuity

    else

    {

    // Portraiture pristine node

    topPtr = novellightlightlight Node<ItemType>();

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

     

    // Point to conclusive node in novellightlightlight chain

    Node<ItemType>* novellightlightChainPtr = culminationPtr;

     

    // Advance primeval-chain pointer

    origChainPtr = origChainPtr->getNext();

     

    // Portraiture fostering nodes

    timeliness (origChainPtr != nullptr)

    {

    // Get contiguous part from primeval chain

    ItemType contiguousPart = origChainPtr->getItem();

     

    // Create a novellightlightlight node containing the contiguous part

    Node<ItemType>* novellightlightNodePtr = novellightlightlight Node<ItemType>(nextItem);

     

    // Link novellightlightlight node to object of novellightlightlight chain

    newChainPtr->setNext(newNodePtr);

     

    // Advance pointer to novellightlightlight conclusive node

    newChainPtr = novellightlightChainPtr->getNext();

     

    // Advance primeval-chain pointer

    origChainPtr = origChainPtr->getNext();

    } // object timeliness

     

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

    } // object if

    } // object portraiture constructor

    template<class PartType>

    LinkedStack<ItemType>::~LinkedStack()

    {

    // Explode until stack is vacuity

    timeliness (!isEmpty())

    pop();

    } // object destructor

    template<class PartType>

    bool LinkedStack<ItemType>::isEmpty() const

    {

    reappear culminationPtr == nullptr;

    } // object isEmpty

    template<class PartType>

    bool LinkedStack<ItemType>::push(const PartType& novellightlightItem)

    {

    Node<ItemType>* novellightlightNodePtr = novellightlightlight Node<ItemType>(newItem, culminationPtr);

    topPtr = novellightlightNodePtr;

    newNodePtr = nullptr;

     

    reappear true;

    } // object butt

    template<class PartType>

    bool LinkedStack<ItemType>::pop()

    {

    bool upshot = untrue;

    if (!isEmpty())

    {

    // Stack is referable vacuity; delete culmination

    Node<ItemType>* nodeToDeletePtr = culminationPtr;

    topPtr = culminationPtr->getNext();

     

    // Reappear deleted node to system

    nodeToDeletePtr->setNext(nullptr);

    delete nodeToDeletePtr;

    nodeToDeletePtr = nullptr;

     

    upshot = true;

    } // object if

     

    reappear upshot;

    } // object explode

    template<class PartType>

    ItemType LinkedStack<ItemType>::peek() const

    {

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

     

    // Stack is referable vacuity; reappear culmination

    reappear culminationPtr->getItem();

    } // object getTop

    // Object of implementation rasp.

    Functions:

    #include <iostream>

    using namespace std;

    bool hinderString(string s)

    {

    //variables to abundance the reckon of A and B

     

    int reckonA=0, reckonB=0;

     

    //create the occurrence of linked stack to wait reputes

     

    LinkedStack <char> stackObj= novellightlightlight LinkedStack();

    //loop invariant

    int i=0;

    //butt the reputes of the string into the stack

    while(i<s.length())

    {

    //butt the repute into the stack

    stackObj.push(s[i]);

    }

    //explode the reputes from the stack

    timeliness (!stackObj.isEmpty())

     

    {

    //extract the culmination atom

    char culmination=stackObj.peek();

     

    //if the repute is A increment reckon of A

    if (culmination ==’A’)

    countA++;

    //if the repute is B increment reckon of B

    if(top==’B’)

    countB++;

    }

    //hinder the reckon of A and B, whether they are same

     

    if(countA==countB)

     

    reappear true;

     

    else

     

    reappear untrue;

    }

    bool hinderStringb(string s)

    {

     

    //variables to abundance the reckon of A and B

     

    int reckonA=0, reckonB=0;

     

    //create the occurrences of linked stack to wait reputes

     

    LinkedStack <char> stackObj1= novellightlightlight LinkedStack();

     

    LinkedStack <char> stackObj2= novellightlightlight LinkedStack();

     

    //loop invariant

     

    int i=0;

     

    //butt the reputes of the string into the stackObj1

     

    while(i<s.length())

     

    {

     

    //butt the repute into the stack

     

    stackObj1.push(s[i]);

     

    }

     

    //Create the portraiture of stack

     

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

     

    //create the derangement string stack using stackObj3

     

    //explode the reputes from the stack

     

    timeliness (!stackObj3.isEmpty())

    {

    //extract the culmination atom

    char culmination=stackObj3.peek();

    //butt into the derangement stack

    stackObj2.push(top);

    //explode the culmination

     

    stackObj2.pop();

    }

    int reckon=0;

    //The timeliness loop to hinder the stack contents

    timeliness (!stackObj1.isEmpty() && !stackObj1.isEmpty())

    {

    //extract the culmination atom of stack 1

    char culmination1=stackObj1.peek();

    //extract the culmination atom of stack 1

    char culmination2=stackObj2.peek();

    //explode the stacks

    stackObj2.pop();

    stackObj1.pop();

    if (top2==’A’)

    if(top1!=’B’)

    reappear untrue;

    count++;

    if (top2==’B’)

    break;

    }

    //hinder the reckon of A is half the string

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

    reappear true;

    else

    reappear untrue;

    }

    int deep()

    {

    reappear 0;

    }

    Expert Reply

     

    a)

    Pseudo Regulation:

    discharge whether_string_is_in_L(str){

    initialize a stack

    iterate aggravate perfect the char of the string
    if char is ‘a’ or ‘b’ {
    if the stack is vacuity and
    butt ‘a’ or ‘b’
    else
    conclusive = peek culmination of stack
    if conclusive == running char{
    butt running char in stack
    }else{
    explode from the stack
    }
    }

    Executable Regulation:

    #include <iostream>
    #include <stack>
    #include <vector>
    #include <deque>
    using namespace std;
    #include <string>

    int whether_string_is_in_L(string s){
    stack<char> mystack;

    for (int i = 0; i < s.length(); ++i)
    {
    if(s[i] == ‘a’ || s[i] == ‘b’){

    if( mystack.empty() ) mystack.push(s[i]);
    else{

    if(s[i] == mystack.top()){
    mystack.push(s[i]);
    }else{
    mystack.pop();
    }
    }
    }
    }

    if(mystack.empty()){
    cout << “string ” << s << ” belongs to rise L” << objectl;
    }else{
    cout << “string ” << s << ” does referable belongs to rise L” << objectl;
    }

    reappear 1;
    }

    int deep ()
    {

    string s = “aabssssaawss”;

    whether_string_is_in_L(s);

    s = “a”;

    whether_string_is_in_L(s);

    s = “b”;

    whether_string_is_in_L(s);

    s = “ab”;

    whether_string_is_in_L(s);

    s = “ababab”;

    whether_string_is_in_L(s);

    s = “ababaaaa”;

    whether_string_is_in_L(s);

    reappear 0;
    }

    Output: