I have been honing my skills in data structures with JavaScript and recently tackled the task of converting an infix expression to postfix using a linked list stack. While I am confident in my logic, two functions - isLetter()
and precedence()
might be causing some issues.
Current result: Postfix Expression: +21-43--56/
Expected result: Postfix Expression: 12+34--65-/
Current result: Postfix Expression: *-^^12242
Expected result: Postfix Expression: 24*221^^-
class Node {
/* Creates a new node with given element and next node */
constructor(e, n) {
this.data = e;
this.next = n;
}
}
class LinkedStack {
/* Initializes an empty stack */
constructor() {
this.top = null;
this.size = 0;
}
// Rest of the code has not been altered to keep it as close to original instructive content
let pToIn = new PostfixToInfix();
let sample1 = "(((1+2)-(3-4))/(6-5))";
console.log("Infix Expression: " + sample1);
/* Current result: Postfix Expression: +21-43--56/
/* Expected result: Postfix Expression: 12+34--65-/
*/
console.log("Postfix Expression: " + pToIn.intoPost(sample1));
/* Current result: Postfix Expression: *-^^12242
/* Expected result: Postfix Expression: 24*221^^-
*/
let sample2 = "2*4-2^2^1";
console.log("Infix Expression: " + sample2);
console.log("Postfix Expression : " + pToIn.intoPost(sample2));
/*
let stack = new LinkedStack();
stack.push(9);
console.log(stack.pop() + " was popped"); // 9 was popped
stack.push(12);
stack.push(15);
stack.push(7);
console.log("Is Stack Empty? " + stack.isEmpty()); // Is Stack Empty? false
console.log("Stack Length: " + stack.length()); // Stack Length: 2
console.log("Top value: " + stack.peek()); // Top value: 15
console.log("Stack Content: " + stack.toString()); // Stack content [15, 12] */