使用堆栈测试平衡分隔符

时间:2015-09-25 23:58:38

标签: java stack delimiter

我基于单链表创建了一个通用堆栈类,并尝试使用它来检查用户输入的均衡分隔符。我只是检查这些分隔符:(){} []

堆码

public class SLStack<T> {

//initializes the first node
private SLStackNode<T> head; //initializes the first node

//constructor initializes the first node as null to simulate an empty stack
public SLStack(){
    head = null;
}

//inner node class
public static class SLStackNode<T>{
    public T data;
    public SLStackNode<T> next;
}

//adds an element to the top of the stack
public void push(T value){
    SLStackNode<T> newNode = new SLStackNode<>();
    newNode.data = value;
    newNode.next = head;
    head = newNode;
}

//removes an element from the top of the stack
public T pop(){
    if (head == null){
        throw new IllegalStateException("The list is empty.");
    }
    T value = head.data;
    head = head.next;
    return value;
}

//checks the element at the top of the stack
public T top(){
    if (head == null){
        throw new IllegalStateException("The list is 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 the 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 one expression per line.");
    System.out.println("End the program with a period on a line by 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 to the stack
            delimStack.push(token);
        }

        if (token.getType() == ExpressionScanner.Token.Type.DELIM_CLOSE){
            //look for matching opening delimiter in the stack and pop it from the stack
            if (token == delimStack.top()){
                delimStack.pop();
            }else{
                throw new IllegalStateException("Imbalanced delimiter detected.");
            }
        }

        if (delimStack.isEmpty()){
            System.out.println("Delimiters are balanced.");
        }else{
            System.out.println("Imbalanced delimiter detected.");
        }

        //System.out.println(token);
    }
}
}

当我运行测试仪时,它总是说分隔符是不平衡的,无论它是什么。即使做一个单独的开放分隔符也会导致它说存在不平衡但它不会抛出异常。它会在单个结束分隔符上抛出异常,或者如果有多个结束分隔符。如果我有两个打开的分隔符,它也不会终止。

如果有人需要,我也可以发布ExpressionScanner的代码。

1 个答案:

答案 0 :(得分:0)

            if (token == delimStack.top()){
            delimStack.pop();
        }else{
            throw new IllegalStateException("Imbalanced delimiter detected.");
        }

如果我假设你的代码在这里抛出异常,因为它期望DELIM_CLOSE类型的标记但是接收DELIM_OPEN(假设你的输入是'()')。

你应该检查的是delimStack.pop()= token.type.DELIM_OPEN。