//接口
public interface Stack { public void push(Object obj) throws Exception;
public Object pop() throws Exception;
public Object getTop() throws Exception;
public boolean isEmpty() throws Exception;
}//链表的结点类public class Node { Node head,next;
Object element;

public Node(Node next){
this.next = next;
}

public Node(Object obj, Node node){
element = obj;
head = node;
}


public Object getElement(){
return element;
}

public void setElement(Object obj){
element = obj;
}

public Node getNext(){
return next;
}

public void setNext(Node nextval){
next = nextval;
}

public String toString(){
return element.toString();
}
}//链表类package jgy.cqut;public class LinkStack implements Stack{

Node head;
int size;

public void LinkStack(){
head = null;
size = 0;
}
public Object getTop() throws Exception {

return head.element;
}
public boolean isEmpty() throws Exception {

return head == null;
}
public Object pop() throws Exception {
if(size == 0){
throw new Exception("堆栈已空!");
}

Node temp = head;
head = head.next;
size --;
return temp.getElement();

}
public void push(Object obj) throws Exception { head = new Node(obj,head);
size ++;

}

public int size(){
return size;
}}
//扫描类package jgy.cqut;import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class InExpression { String expression; public InExpression() {
System.out.println("请输入表达式:");
expression = new Scanner(System.in).nextLine();
} public void ReadExpression() {
Pattern p = Pattern.compile("(\\d+\\+|\\-|\\*|\\/?\\d+)+");
Matcher m = p.matcher(expression);
if (!m.matches()) {
System.out.println("你输入的表达式有误!");
return;
} else {
System.out.println("你输入的表达式正确!"); LinkStack linkNumber = new LinkStack();
LinkStack linkOperator = new LinkStack(); Pattern pp = Pattern.compile("\\d+");
Matcher mm = pp.matcher(expression); int count = 0; while (mm.find()) {
try {
linkNumber.push(expression.substring(mm
.start(), mm.end()));
System.out.print(linkNumber.pop() + " ");

count++; } catch (NumberFormatException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace();
} try {
if (mm.end() < expression.length()) { linkOperator.push(expression.charAt(mm.end())); } if (count >= 2
&& ((linkOperator.getTop().toString()).equals("*") || (linkOperator
.getTop().toString()).equals("/"))) {
System.out.print(linkOperator.pop() + " ");

}  //为什么报空指针异常啊???????????? } catch (Exception e) { e.printStackTrace();
}
}




System.out.println("count="+count);

try {
System.out.println("linkOperator.size ="+linkOperator.size);
if (!linkOperator.isEmpty()) { System.out.print(linkOperator.pop()+" ");

}
} catch (Exception e) {

e.printStackTrace();
}
}
}
}
//测试类
package jgy.cqut;public class Main { public static void main(String[] args) {
InExpression inexpression = new InExpression();
try {
inexpression.ReadExpression();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) { e.printStackTrace();
}
}
}/*
错误信息:
请输入表达式:
12+34-56*78/90
你输入的表达式正确!
12 34 56 * 78 / 90 count=5
linkOperator.size =2
java.lang.NullPointerException
at jgy.cqut.LinkStack.getTop(LinkStack.java:16)
at jgy.cqut.InExpression.ReadExpression(InExpression.java:58)
at jgy.cqut.Main.main(Main.java:8)
*/

解决方案 »

  1.   

    at jgy.cqut.LinkStack.getTop(LinkStack.java:16)
    public Object getTop() throws Exception {return head.element;  //  《==========如果head为空,就报空指针吧
    }ps:系统有现成的链表类,不用重复造轮子吧如果你是学习数据结构的话,当我没说
      

  2.   


    linkNumber.push(expression.substring(mm
    .start(), mm.end()));
    System.out.print(linkNumber.pop() + " ");
    这里,貌似你push进去又pop出来,那岂不是后面要读取时,栈里面是空的?怪不得报空指针了这里pop换成 getTop
      

  3.   

    try {
    if (mm.end() < expression.length()) { linkOperator.push(expression.charAt(mm.end())); ?????这里编译错误啊。 } if (count >= 2
    && ((linkOperator.getTop().toString()).equals("*") || (linkOperator
    .getTop().toString()).equals("/"))) {
    System.out.print(linkOperator.pop() + " "); } // 为什么报空指针异常啊???????????? } catch (Exception e) { e.printStackTrace();
    }