i made generic stack class based on singly linked list , trying use check user input balanced delimiters. checking these delimiters: (){}[]
stack code
public class slstack<t> { //initializes first node private slstacknode<t> head; //initializes first node //constructor initializes first node null simulate empty stack public slstack(){ head = null; } //inner node class public static class slstacknode<t>{ public t data; public slstacknode<t> next; } //adds element top of stack public void push(t value){ slstacknode<t> newnode = new slstacknode<>(); newnode.data = value; newnode.next = head; head = newnode; } //removes element top of stack public t pop(){ if (head == null){ throw new illegalstateexception("the list empty."); } t value = head.data; head = head.next; return value; } //checks element @ top of stack public t top(){ if (head == null){ throw new illegalstateexception("the list empty."); } t value = head.data; return value; } public boolean isempty(){ return head == null; } public void printstack(slstacknode<t> node, int depth) { if (node.next != null) { system.out.println(depth + " : " + node.data); //recurses through stack //printstack(node.next); } system.out.println(depth + " : " + node.data); //recursive base case } }
balance tester code
public class balanced { public static void main(string[] args) { slstack<expressionscanner.token> delimstack = new slstack<>(); system.out.println("enter 1 expression per line."); system.out.println("end program period on line itself."); expressionscanner escan = new expressionscanner(new scanner(system.in)); while (escan.hasnext()) { expressionscanner.token token = escan.next(); if (token.gettype() == expressionscanner.token.type.op || token.gettype() == expressionscanner.token.type.var){ //ignore these tokens continue; } if (token.gettype() == expressionscanner.token.type.delim_open){ //push opening delimiter stack delimstack.push(token); } if (token.gettype() == expressionscanner.token.type.delim_close){ //look matching opening delimiter in stack , pop stack if (token == delimstack.top()){ delimstack.pop(); }else{ throw new illegalstateexception("imbalanced delimiter detected."); } } if (delimstack.isempty()){ system.out.println("delimiters balanced."); }else{ system.out.println("imbalanced delimiter detected."); } //system.out.println(token); } } }
when run tester says delimiter imbalanced no matter is. doing single opening delimiter causes there imbalance doesn't throw exception. throws exception on single closing delimiter or if there more 1 closing delimiters. if have 2 opening delimiters doesn't terminate either.
i can post code expressionscanner if needs it.
if (token == delimstack.top()){ delimstack.pop(); }else{ throw new illegalstateexception("imbalanced delimiter detected."); }
if assumed correctly code throwing , exception here because it's expecting token of type delim_close receiving delim_open (provided input '()').
what should check whether delimstack.pop() = token.type.delim_open.
Comments
Post a Comment