I have LinkedStack class, what I simply want to do is make a delimiter match alogorithm, I also have the working algorithm for it but the problem is implementing the algorithm . I'm just stuck in my POP method and which will fix just one line of algorithm currently showing incompatible types.
I have a abstract DataElement class, A CharElement class extending DataElement and a LinkedStack class. My LinkedStacked class has all the push, pop and peek methods. I need help in making the pop method a char type.
Below is my LinkedStack Class, just before the class ends, I have the pop method, so you can scroll all the way down and see the method.
Here is what I had previously, this method below worked perfectly when I tried to create a stack, and then peek and pop and then push again; but not with the delimiter algorithm method in my tester.
public char pop() throws StackException { if (stackTop == null) throw new StackException(); stackTop= stackTop.link }
public class LinkedStack { protected class StackNode extends CharElement { public DataElement info; public StackNode link; public StackNode() { info = null; link = null; } public StackNode(DataElement item, StackNode pointer) { info = item; link = pointer; } public String toString() { return info.toString(); } } private StackNode stackTop; public LinkedStack() { stackTop = null; } public void initializeStack() { stackTop = null; } public boolean isEmptyStack() { return (stackTop == null); } public boolean isFullStack() { return false; } public void push(DataElement newItem) { StackNode newTop; newTop = new StackNode(newItem, stackTop ); stackTop = newTop; } public DataElement peek() throws StackException { if (stackTop == null) throw new StackException(); return stackTop.info; } public char pop() throws StackException <-----------------------------------------------------------------------HERE IS THE POP METHOD, HELP ME CODE It, i need a return!! { if (stackTop == null) throw new StackException(); } }
here is my tester,
there is still stuff to write but having an error on one line of the method 'i'sdelimitermatching', I have marked it with arrows!
public class Tester { public static void main(String[] args) { LinkedStack stack1 = new LinkedStack(); stack1.push(new CharElement('a')); stack1.push(new CharElement('b')); stack1.push(new CharElement('c')); stack1.push(new CharElement('d')); stack1.push(new CharElement('e')); System.out.println("Top of the Stack = " + stack1.peek()); while (!stack1.isEmptyStack()) { System.out.println(stack1.peek()); stack1.pop(); } } public boolean isDelimiterMatching(String inputExpr) { int stackSize = inputExpr.length(); LinkedStack theStack = new LinkedStack(); for (int j = 0; j < inputExpr.length(); j++) { char ch = inputExpr.charAt(j); switch (ch) { case '{': case '[': case '(': String temp = Character.toString(ch); theStack.pop(); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Here is it, gotta fix it by fixing the pop method! break; case '}': case ']': case ')': if (!theStack.isEmptyStack()) { char stackContent; stackContent = theStack.pop(); if ((ch == '}' && stackContent!= '{') || (ch == ']' && stackContent != '[') || (ch == ')' && stackContent != '(')){ System.out.println("Mismatch found: " + ch + " at " + j); return false; } } else { System.out.println("Mismatch found: " + ch + " at " + j); return false; } break; default: break; } } if (!theStack.isEmptyStack()){ System.out.println("Error: missing right delimiter"); return false; } return true; }
Well I have the major part of the code but the problem is the Pop method, I need it to cut the head of my stack, i mean remove the top and at the same time return me the top that it just took off so that I can store in the char variable in my "isDelimittermatching" method(Marked with arrows above).
Please help me out write the pop method! Have been trying for ours now!