Question: C + + Converting infix to postfix using Stack Download and unzip Stack.zip, understand Stack.h , then implement a C + + program to convert

C++Converting infix to postfix using Stack
Download and unzip Stack.zip, understand Stack.h, then implement a C++ program to convert infix to postfix. Operands must be alphanumeric, and only the operators +,-,*, and / are permitted.
//Stack.h
#pragma once
#include
using namespace std;
class FullStack
// Exception class thrown by Push when stack is full.
{};
class EmptyStack
// Exception class thrown by Pop and Top when stack is emtpy.
{};
struct NodeType {
char value;
NodeType* next;
};
class Stack
{
private:
NodeType* topPtr;
public:
Stack();
// ~Stack();
void push(char x);
void pop(char& x);
char top();
bool isFull();
bool isEmpty();
void displayStack();
};
char Stack::top()
// Returns a copy of the top item in the stack.
// Pre: Stack has been initialized.
// Post: If stack is empty, EmptyStack exception is thrown;
// else a copy of the top element is returned.
{
if (isEmpty())
throw EmptyStack();
else
return topPtr->value;
}
Stack::Stack()// Class constructor.
{
topPtr = NULL;
}
bool Stack::isEmpty()
{
return (topPtr == NULL);
}
void Stack::pop(char& x)
{
if (isEmpty())
throw EmptyStack();
x = topPtr->value;
NodeType* temp = topPtr;
topPtr = topPtr->next;
delete temp;
}
void Stack::push(char x)
{
if (isFull())
throw FullStack();
NodeType* nodePtr = new NodeType();
nodePtr->value = x;
nodePtr->next = topPtr;
topPtr = nodePtr;
}
//Returns true if there is no room for another ItemType on the free store; false otherwise.
bool Stack::isFull()
{
NodeType* nodePtr;
try {
nodePtr = new NodeType;
delete nodePtr;
return false;
}
catch (std::bad_alloc exception){
//return true;
throw FullStack();
}
}
void Stack::displayStack()
{
NodeType* nodePtr = topPtr;
while (nodePtr != NULL)
{
cout << nodePtr->value <<"";
nodePtr = nodePtr->next;
}
}
//StackTest.cpp
#include
#include "Stack.h"
using namespace std;
int main()
{
Stack s;
s.push('a');
s.push('g');
s.push('p');
char ch;
s.pop(ch);
cout << ch << endl;
s.displayStack();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!