In c++
Section 1. Stack ADT – Overview
Data Items
The data items in a stack are of generic DataType.
This means use should use templating and your Node class.
Structure
The stack data items are linearly ordered from the
most recently added (the top) to the least recently added (the
bottom). This is a LIFO scheme. Data items are inserted onto
(pushed) and removed from (popped) the top of the
stack.
Operations
Constructor. Creates an empty stack.
Copy constructor. Initializes the stack to be equivalent to the other Stack object parameter.
Overloaded assignment operator. Sets the stack to be equivalent to the other Stack object parameter and returns a reference to the modified stack.
Destructor. Deallocates the memory used to store the stack.
push (const DataType& newDataItem)
Requirements: Stack is not full.
Results: Inserts newDataItem onto the top of the stack.
DataType peek()
Requirements: Stack is not empty
Results: returns a copy of the value of the most recently added (top) data item from the stack
void pop()
Requirements: Stack is not empty
Results: Removes the most recently added (top) data item from the stack.
void clear()
Requirements: None
Results: Removes all the data items in the stack.
bool isEmpty() const
Requirements: None
Results: Returns true if the stack is empty. Otherwise, returns false.
bool isFull () const
Requirements: None
Results: Returns true if the stack is full. Otherwise,
returns false.
Section 2. Implementation Approaches
As in the class, we consider both
Array-based implementations
Linked-List implementations
For this assignment, create the link-based
implementation!
Section 3.
Programming Exercise 1
We commonly write arithmetic expressions in the so-called infix form. That is, with each
operator placed between its operand, as below
(5+6)*(4/3)
Although we are comfortable writing expressions in this form, infix form has the
disadvantage that parentheses must be used to indicate the order in which operators
are to be evaluated. These parentheses, in turn, complicate the evaluation process.
Evaluation is much easier if we can simply evaluate operators from left to right.
Unfortunately, this evaluation will not work with the infix form of arithmetic expressions.
However, it will work if the expression is in postfix form. In the postfix form of an arithmetic
expression, each operator is placed immediately after its operands. The expression
above is written in postfix form as
56+43/*
Note that both forms place the numbers in the same order (reading from left to right).
The order of the operators is different, however, because the operators in the postfix form
are positioned in the order that they are evaluated. The resulting postfix expression is hard
to read at first, but it is easy to evaluate programmatically. We will do so with stacks.
Suppose you have an arithmetic expression in postfix form that consists of a sequence of
single digit, nonnegative integers and the four basic arithmetic operators (+,-,*,/). This
expression can be evaluated using the following algorithm in conjuction with a stack of
floating-point numbers.
Read the expression character-by-character. As each character is read in:
If the character corresponds to a single digit number
(characters ‘0’ to ‘9’), then
push the corresponding floating-point number onto the stack.
If the character corresponds to one of the arithmetic
operators (characters ‘+’, ‘-
‘, ‘*’, ‘/’), then
Pop a number off of the stack. Call it
operand1.
Pop a number off of the stack. Call it operand
2.
Combine these operands using the arithmetic operator,
as follows
Result = operand2 operator operand1
Push result onto the stack.
When the end of the expression is reached, pop the remaining number
off the stack. This
number is the value of the expression. Applying this algorithm to the arithmetic expression
34+52/*
Results 17.5 as expected.
Create a program that reads the infix form of an
arithmetic expression, evaluates it, outputs the postfix
expression, and outputs the result. Assume that the expression
consists of single-digit, nonnegative integers (‘0’ to ‘9’) and the
FIVE basic arithmetic operators (‘+’,’-‘,’*’,’/’,’^’) (note to
correctly handle ‘^’). Further assume that the arithmetic
expression is input from the keyboard with all the characters
separated by white space on one line. Save your program in a file
called postfix.cpp
#include<bits/stdc++.h>
using namespace std;
//Function to return precedence of operators
int pred(char c)
{
if(c=='^')
return 3;
else if(c=='*'||c=='/')
return 2;
else if(c=='+'||c=='-')
return 1;
else
return -1;
}
//Main function to convert infix to postfix
void infixToPostfix(string s)
{
std::stack<char>st;
st.push('N');
int l=s.length();
string ns;
for(int i=0;i<l;i++)
{
if((s[i]>='a' && s[i]<='z')||(s[i]>='A' && s[i]<='Z'))
ns+=s[i];
else if(s[i]== '(' )
st.push( '(' );
else if(s[i]== ')' )
{
while(st.top()!='N' && st.top()!= '(' )
{
char c = st.top();
st.pop();
ns +=c;
}
if(st.top() == '(' )
{
char c =st.top();
st.pop();
}
}
else{
while(st.top() !='N' && prec(s[i]) <= prec(st.top()))
{
char c = st.top();
st.pop();
ns +=c;
}
st.push(s[i]);
}
}
//pop all remaining elements from the stack
while(st.top() != 'N')
{
char c =st.top();
st.pop();
ns += c;
}
cout << ns << endl;
}
int main()
{
string exp = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
In c++ Section 1. Stack ADT – Overview Data Items The data items in a stack...
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is 62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...
Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...
- implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...
i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack { char s[SIZE]; int top; }; void push(Stack *st, char c) { st->top++; st->s[st->top] = c;...
Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...
JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...