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

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.

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

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

//#include “StackInterface.h”

//#include “Node.h”

template<class PartType>

{

private:

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

// this node contains the stack’s culmination

public:

// Constructors and destructor:

// Stack actions:

bool isEmpty() const;

bool butt(const PartType& novellightlightItem);

bool explode();

ItemType peek() const;

//#endif

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

//#include <cassert> // For assert

template<class PartType>

{

} // object absence constructor

template<class PartType>

{

// 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;

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();

origChainPtr = origChainPtr->getNext();

} // object timeliness

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

} // object if

} // object portraiture constructor

template<class PartType>

{

// Explode until stack is vacuity

timeliness (!isEmpty())

pop();

} // object destructor

template<class PartType>

{

reappear culminationPtr == nullptr;

} // object isEmpty

template<class PartType>

{

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

topPtr = novellightlightNodePtr;

newNodePtr = nullptr;

reappear true;

} // object butt

template<class PartType>

{

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>

{

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

//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

//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

//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;

}

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: