2015-09-25 42 views
0

我做了一個基於單個鏈表的通用堆棧類,並試圖用它來檢查用戶輸入的平衡分隔符。我只是檢查這些分隔符:(){} []使用堆棧測試平衡分隔符

棧代碼

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 
} 
} 

平衡測試儀典

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的代碼。

+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。