Question: import java.util.Scanner; public class PoD { public static void main(String[] args) { Scanner in = new Scanner(System.in); Stack parentheses = new Stack(); String input =
![import java.util.Scanner; public class PoD { public static void main(String[] args)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f31bc3e9a72_38766f31bc389eb2.jpg)

import java.util.Scanner; public class PoD { public static void main(String[] args) { Scanner in = new Scanner(System.in); Stack parentheses = new Stack(); String input = in.nextLine(); //START WORK HERE ************************************************ //When you see an opening parenthesis, push it on the stack. //When you see a closing parenthesis: //If the stack is empty, unbalanced. //Otherwise, pop the stack. //If the opening and closing parentheses don't match: //The parentheses are unbalanced. Exit. //If at the end the stack is empty: //The parentheses are balanced. //Else: //The parentheses are not balanced. } } import java.util.NoSuchElementException; public class Stack { private Node head; class Node { public char data; public Node next; } // Constructs an empty stack: public Stack() { head = null; } /** * Adds an element to the top of the stack * @param c character to add */ public void push(char c) { Node newNode = new Node(); newNode.data = c; newNode.next = head; head = newNode; } /** * Removes the element from the top of the stack * @return the removed String */ public char pop() { if (head == null) {throw new NoSuchElementException();} char c = head.data; head = head.next; return c; } public boolean empty() { if (head == null) { return true; } else { return false; } } public String toString() { Node position = head; String output = ""; while (position != null) { output += position.data + " "; position = position.next; } return output; } }Today you are going to make use of the Stackjava file created yesterday! You are going to use the stack that you implemented to check for proper use of parentheses within a mathematical statement. In Java, where we use only rounded parentheses"(" and"", making sure that parentheses are balanced is as easy as incrementing, when you see"", and decrementing, when you encounter", a counter. If your counter is O at the end of the expression, your parentheses are balanced Expression: (2 (3 4) 1(5 / 2))7) Counter: 121 2 321 0 However, mathematical notation allows expressions to use more than one kind of parentheses , , L, ], ( and). As an example, take a look at the following mathematical expression: bb- 4ac)]/(2 a) On page 715 of your textbook, an algorithm for checking that these expressions are balanced is given. It makes use of a stack to check for balanced parentheses: When you see an opening parenthesis, push it on the stack. When you see a closing parenthesis, pop the stack If the opening and closing parentheses don't match: The parentheses are unbalanced. Exit The parentheses are balanced The parentheses are not balanced If at the end the stack is empty: se Today, we are going to use the implementation of a stack from yesterday (you will be given the file Stack.java) and implement this algorithm
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
