Question: import java.util.NoSuchElementException; public class Stack { private Node head; class Node { public char data; public Node next; } // Constructs an empty stack: public

 import java.util.NoSuchElementException; public class Stack { private Node head; class Node

{ public char data; public Node next; } // Constructs an empty

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; }

}

===========================================================================================

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.

}

}

Today you are going to make use of the Stack.java 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 0 at the end of the expression, your parentheses are balanced. Expression: (2(3 4)/(1(5 2)) 7) Counter 12 1 23 21 0 However, mathematical notation allows expressions to use more than one kind of parentheses: t, 1, [,, and) As an example, take a look at the following mathematical expression: Cb b - (4 ac)/(2a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!