Question: please write pseudocode using Cormen chapter 2 class Stack { constructor ( ) { this.items = [ ] ; } push ( element ) {
please write pseudocode using Cormen chapter class Stack
constructor
this.items ;
pushelement
this.items.pushelement;
pop
if thisisEmpty return "Underflow";
return this.items.pop;
peek
return this.itemsthisitems.length ;
isEmpty
return this.items.length ;
class PostfixEvaluator
constructor
this.stack new Stack;
this.variables ;
evaluatePostfixexpression
const tokens expression.split;
for let i ; i tokens.length; i
const token tokensi;
if isNaNparseFloattoken
this.stack.pushparseFloattoken;
else if token in this.variables
this.stack.pushthisvariablestoken;
else if thisisOperatortoken
const operand this.stack.pop;
const operand this.stack.pop;
const result this.calculatetoken operand operand;
this.stack.pushresult;
else if token
throw new ErrorInvalid token: token;
return this.stack.pop;
isOperatortoken
return token token token token ;
calculateoperator operand operand
switch operator
case :
return operand operand;
case :
return operand operand;
case :
return operand operand;
case :
if operand throw new ErrorDivision by zero';
return operand operand;
default:
throw new ErrorInvalid operator';
setVariablename value
this.variablesname value;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
