Provide a complete implementation of class LinkedStack and t
Provide a complete implementation of class LinkedStack and test it on the ParenChecker applcation (listed below).
package linkedstack;
import java.util.EmptyStackException;
/** Class to check for balanced parentheses.
* @author Koffman & Wolfgang
**/
public class ParenChecker {
// Constants
/** Set of opening parenthesis characters. */
private static final String OPEN = \"([{\";
/** Set of closing parenthesis characters, matches OPEN. */
private static final String CLOSE = \")]}\";
public static boolean isBalanced(String expression) {
// Create an empty stack.
LinkedStack s = new LinkedStack();
boolean balanced = true;
try {
int index = 0;
while (balanced && index < expression.length()) {
char nextCh = expression.charAt(index);
if (isOpen(nextCh)) {
s.push(nextCh);
} else if (isClose(nextCh)) {
char topCh = (char) s.pop();
balanced =
OPEN.indexOf(topCh) == CLOSE.indexOf(nextCh);
}
index++;
}
} catch (EmptyStackException ex) {
balanced = false;
}
return (balanced && s.empty());
}
/**
* Method to determine whether a character is one of the
* opening parentheses.
* @param ch Character to be tested
* @return true if ch is one of the opening parentheses
*/
private static boolean isOpen(char ch) {
return OPEN.indexOf(ch) > -1;
}
/**
* Method to determine whether a character is one of the
* closing parentheses.
* @param ch Character to be tested
* @return true if ch is one of the closing parentheses
*/
private static boolean isClose(char ch) {
return CLOSE.indexOf(ch) > -1;
}
}
Solution
Plase find my implementation.
Please let me know
############## LinkedStack.java ################
import java.util.EmptyStackException;
public class LinkedStack{
private int n; // size of the stack
private Node first; // top of stack
// helper linked list class
private class Node {
private char item;
private Node next;
}
/**
* Initializes an empty stack.
*/
public LinkedStack() {
first = null;
n = 0;
}
/**
* Is this stack empty?
* @return true if this stack is empty; false otherwise
*/
public boolean empty() {
return first == null;
}
/**
* Returns the number of items in the stack.
* @return the number of items in the stack
*/
public int size() {
return n;
}
/**
* Adds the item to this stack.
* @param item the item to add
*/
public void push(char item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
n++;
}
/**
* Removes and returns the item most recently added to this stack.
* @return the item most recently added
* @throws java.util.EmptyStackException if this stack is empty
*/
public char pop() {
if (empty()) throw new EmptyStackException();
char item = first.item; // save item to return
first = first.next; // delete first node
n--;
return item; // return the saved item
}
/**
* Returns (but does not remove) the item most recently added to this stack.
* @return the item most recently added to this stack
* @throws java.util.EmptyStackException if this stack is empty
*/
public char peek() {
if (empty()) throw new EmptyStackException();
return first.item;
}
}
############################## ParenChecker.java #####################
import java.util.EmptyStackException;
/** Class to check for balanced parentheses.
* @author Koffman & Wolfgang
**/
public class ParenChecker {
// Constants
/** Set of opening parenthesis characters. */
private static final String OPEN = \"([{\";
/** Set of closing parenthesis characters, matches OPEN. */
private static final String CLOSE = \")]}\";
public static boolean isBalanced(String expression) {
// Create an empty stack.
LinkedStack s = new LinkedStack();
boolean balanced = true;
try {
int index = 0;
while (balanced && index < expression.length()) {
char nextCh = expression.charAt(index);
if (isOpen(nextCh)) {
s.push(nextCh);
} else if (isClose(nextCh)) {
char topCh = (char) s.pop();
balanced =
OPEN.indexOf(topCh) == CLOSE.indexOf(nextCh);
}
index++;
}
} catch (EmptyStackException ex) {
balanced = false;
}
return (balanced && s.empty());
}
/**
* Method to determine whether a character is one of the
* opening parentheses.
* @param ch Character to be tested
* @return true if ch is one of the opening parentheses
*/
private static boolean isOpen(char ch) {
return OPEN.indexOf(ch) > -1;
}
/**
* Method to determine whether a character is one of the
* closing parentheses.
* @param ch Character to be tested
* @return true if ch is one of the closing parentheses
*/
private static boolean isClose(char ch) {
return CLOSE.indexOf(ch) > -1;
}
public static void main(String[] args) {
String str = \"{[public()]exmpty}\";
System.out.println(isBalanced(str));
}
}




