Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi there, I'm attempting to write a program which checks that all bracket characters are balanced correctly. If the program encounters an opening bracket char,

Hi there, I'm attempting to write a program which checks that all bracket characters are balanced correctly. If the program encounters an opening bracket char, it should push the corresponding closing char onto the stack. If the character is a closing bracket, the stack should be popped and that result compared to the closing bracket. If they are not equal, the brackets are not paired correctly. After the entire string is read, if the stack is empty, then the parentheses were balanced, else, they are not matched correctly. I am having issues with comparing the popped stack character since it is of type node, with the char c. Any help is appreciated, thank you.

My code so far:

import java.io.*; import java.util.NoSuchElementException; import java.util.Scanner;

public class BalancedParensChecker { static Stack stack = new Stack();

/** * @param args the command line arguments */ public static void main(String[] args) { //Checks both strings to see if the parens are valid checkParens("if ( a[i] < a[i+1] )"); checkParens("if (a[i] < a[i + 1) ]"); } public static void checkParens(String s) { for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '(': stack.push(')'); break; case '[': stack.push(']'); break; case '{': stack.push('}'); break; default: break;

} //If it is a left parentheses, it will be pushed onto the stack //if(c == '(') //{ //stack.push(c); //} //If right, it will be popped off of the stack if(c == ')' || c == ']' || c == '}') { if(stack.isEmpty()) { System.out.println("Error: The stack is empty and cannot" + " be popped"); } else { stack.pop(); if( != c) { System.out.println("The brackets are not paired " + "correctly."); } } } } if(stack.isEmpty()) { System.out.println("The parentheses are balanced."); } else { System.out.println("Stack is not empty: A right parentheses " + "is missing."); } } }

public class Node { protected char data; protected Node next; public Node() { next = null; data = 0; } public Node(char d, Node n) { data = d; next = n; } public void setNext(Node n) { next = n; } public void setData(char d) { data = d; } public Node getNext() { return next; } public char getData() { return data; } }

import java.util.NoSuchElementException;

class Stack { protected Node top; protected int size; public Stack() { top = null; size = 0; } public boolean isEmpty() { return top == null; } public int getSize() { return size; } public void push(char data) { Node newNode = new Node(data, null); if(top == null) { top = newNode; } else { newNode.setNext(top); top = newNode; } size++; } public Node pop() { if(isEmpty()) { throw new NoSuchElementException("Error: Stack is empty."); } Node deleteNode = top; top = deleteNode.getNext(); size--; return deleteNode; } public Node peek() { if(isEmpty()) { throw new NoSuchElementException("Error: Stack is empty."); } return top; } public void displayStack() { System.out.println("Stack contents: "); if (size == 0) { System.out.println("Stack is empty."); return; } Node t = top; while(t != null) { System.out.println(t.getData() + " "); t = t.getNext(); } System.out.println(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions