Question
Can someone explain how the code below work pls? import java.util.*; import java.io.*; class Node { private char data; private Node next; public Node() {
Can someone explain how the code below work pls?
import java.util.*; import java.io.*;
class Node { private char data; private Node next; public Node() { this('#', null); }
public Node(char d) { data = d; }
public Node(char d, Node n) { data = d; next = n; }
public void setData(char newData) { data = newData; }
public void setNext(Node newNext) { next = newNext; }
public char getData() { return data; }
public Node getNext() { return next; }
public void displayNode() { System.out.print(data); } }
interface LLQueue { public void enqueue(char item);
public char dequeue();
public int size();
public boolean empty();
public int front(); }
class LLQueueADT implements LLQueue { private int size; private Node front; private Node rear;
public LLQueueADT() { size = 0; front = null; rear = null; }
public boolean empty() { return (size == 0); }
public void enqueue(char c) { Node newNode = new Node(); newNode.setData(c); newNode.setNext(null); if (this.empty()) { front = newNode; } else { rear.setNext(newNode); } rear = newNode; size++; }
public char dequeue() { char i; i = front.getData(); front = front.getNext(); size--; if (this.empty()) { rear = null; } return i; }
public int front() { return front.getData(); }
public int size() { return size; } }
interface LLStack { public void push(char item);
public char pop();
public int size();
public boolean empty();
public char ontop(); }
class LLStackADT implements LLStack { private Node top; private int size;
public LLStackADT() { top = null; size = 0; }
public boolean empty() { return (top == null); }
public void push(char c) { Node newNode = new Node(); newNode.setData(c); newNode.setNext(top); top = newNode; size++; }
public char pop() { char i; i = top.getData(); top = top.getNext(); size--; return i; }
public char ontop() { char i = pop(); push(i); return i; }
public int size() { return size; }
}
public class infixPostfix {
public static int stackPriority(char c) { //given a token returns integer associated with its priority switch (c) { case '+': case '-': return 1; case '/': case '*': return 2; } return -1; }
public static void main(String[] args) {
LLStackADT operator = new LLStackADT(); //contains +.-.*,/,(, or # LLQueueADT infixQueue = new LLQueueADT(); //contains infix expression LLQueueADT postfixQueue = new LLQueueADT(); //contains final postfix expression LLStackADT value = new LLStackADT(); //contains postifx values to be evaluated operator.push('#'); Scanner scan = new Scanner(System.in); System.out.println("Enter infix expression: "); String infix = scan.nextLine(); for (int i = 0; i < infix.length(); i++) { if(infix.charAt(i) != ' ') infixQueue.enqueue(infix.charAt(i)); //enqueue infix expression to infixQueue }
while(!infixQueue.empty()) { int i = infixQueue.dequeue(); char c = (char)i; if(stackPriority(c)>=0) { int p1 = stackPriority(c); int p2 = stackPriority(operator.ontop()); while(p1 <= p2) { postfixQueue.enqueue(operator.pop()); p2 = stackPriority(operator.ontop()); } operator.push(c); } else if(c == ')') { char x=(char)operator.pop(); while(x!='(') { postfixQueue.enqueue(x); x=(char)operator.pop(); } } else if(c =='(') { operator.push(c); } else { postfixQueue.enqueue(c); } } while(!operator.empty()) { postfixQueue.enqueue(operator.pop()); }
System.out.println ("Postfix expression: "); while(!postfixQueue.empty()) { System.out.print (postfixQueue.dequeue() + " "); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
This Java code converts an infix expression to a postfix expression using a stack and a queue data s...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