Question
*With the following code* Write a pseudocode function that uses a stack to determine whether a string is in the language L, where a. L
*With the following code*
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 of As and Bs} b. L = { s : s is of the form An Bn for some n => 0}
Node.h
#pragma once
#include
template < class ItemType>
class Node
{
private:
ItemType item; // A data item
Node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node
void setItem(const ItemType& anItem);
void setNext(Node
ItemType getItem() const;
Node
};
template < class ItemType>
Node
{
} // end default constructor
template < class ItemType>
Node
{
} // end constructor
template < class ItemType>
Node
item(anItem), next(nextNodePtr)
{
} // end constructor
template < class ItemType>
void Node
{
item = anItem;
} // end setItem
template < class ItemType>
void Node
{
next = nextNodePtr;
} // end setNext
template < class ItemType>
ItemType Node
{
return item;
} // end getItem
template < class ItemType>
Node
{
return next;
}
#pragma once
#include
template < class ItemType>
class Node
{
private:
ItemType item; // A data item
Node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node
void setItem(const ItemType& anItem);
void setNext(Node
ItemType getItem() const;
Node
};
template < class ItemType>
Node
{
} // end default constructor
template < class ItemType>
Node
{
} // end constructor
template < class ItemType>
Node
item(anItem), next(nextNodePtr)
{
} // end constructor
template < class ItemType>
void Node
{
item = anItem;
} // end setItem
template < class ItemType>
void Node
{
next = nextNodePtr;
} // end setNext
template < class ItemType>
ItemType Node
{
return item;
} // end getItem
template < class ItemType>
Node
{
return next;
}
StackInterface.h
#pragma once
template
LinkedStack.h
#pragma once
#include "StackInterface.h"
#include "Node.h"
#include
template < class ItemType>
class LinkedStack : public StackInterface
{
private:
Node
/*Node
public:
// Constructors and destructor:
LinkedStack(); // Default constructor
LinkedStack(const LinkedStack
virtual ~LinkedStack(); // Destructor
// Stack operations:
bool isEmpty() const;
bool push(const ItemType& newItem);
bool pop();
/*void validateAB(LinkedStack
ItemType peek() const;
/*bool contains(const ItemType& anEntry) const;*/
}; // end LinkedStack
template < class ItemType>
LinkedStack
{
} // end default constructor
template < class ItemType>
LinkedStack
LinkedStack(const LinkedStack
{
// Point to nodes in original chain
Node
if (origChainPtr == nullptr)
topPtr = nullptr; // Original bag is empty
else
{
// Copy first node
topPtr = new Node
topPtr->setItem(origChainPtr->getItem());
// Point to first node in new chain
Node
// Copy remaining nodes
while (origChainPtr != nullptr)
{
// Advance original-chain pointer
origChainPtr = origChainPtr->getNext();
// Get next item from original chain
ItemType nextItem = origChainPtr->getItem();
// Create a new node containing the next item
Node
// Link new node to end of new chain
newChainPtr->setNext(newNodePtr);
// Advance pointer to new last node
newChainPtr = newChainPtr->getNext();
} // end while
newChainPtr->setNext(nullptr); // Flag end of chain
} // end if
} // end copy constructor
template < class ItemType>
LinkedStack
{
// Pop until stack is empty
while (!isEmpty())
pop();
} // end destructor
template < class ItemType>
bool LinkedStack
{
return topPtr == nullptr;
} // end isEmpty
template < class ItemType>
bool LinkedStack
{
Node
topPtr = newNodePtr;
newNodePtr = nullptr;
return true;
} // end push
template < class ItemType>
bool LinkedStack
{
bool result = false;
if (!isEmpty())
{
// Stack is not empty; delete top
Node
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
{
assert(!isEmpty()); // Enforce precondition
// Stack is not empty; return top
return topPtr->getItem();
} // end getTop
LinkedStackMain.cpp
#include"LinkedStack.h"
#include
#include
using namespace std;
int main() {
StackInterface
string anItem = "0";
cout << "Enter a string: ";
getline(cin, anItem);
link->push(anItem);
link->peek();
cout << link->peek() << endl;
cout << "Enter a string: ";
getline(cin, anItem);
link->push(anItem);
link->peek();
cout << link->peek() << endl;
link->pop();
cout << link->peek() << endl;
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started