//结点类 public class Node { Object element; //数据域 Node next; //指针域 //头结点的构造方法 public Node(Node nextval) { this.next = nextval; } //非头结点的构造方法 public Node(Object obj,Node nextval) { this.element = obj; this.next = nextval; } //获得当前结点的后继结点 public Node getNext() { return this.next; } //获得当前的数据域的值 public Object getElement() { return this.element; } //设置当前结点的指针域 public void setNext(Node nextval) { this.next = nextval; } //设置当前结点的数据域 public void setElement(Object obj) { this.element = obj; } public String toString() { return this.element.toString(); } }
//栈接口 public interface Stack { //入栈 public void push(Object obj) throws Exception; //出栈 public Object pop() throws Exception; //获得栈顶元素 public Object getTop() throws Exception; //判断栈是否为空 public boolean isEmpty(); } //顺序栈 public class SequenceStack implements Stack { Object[] stack; //对象数组 final int defaultSize =10; //默认最大长度 int top; //栈顶位置 int maxSize; //最大长度 public SequenceStack() { init(defaultSize); } public SequenceStack(int size) { init(size); } public void init(int size) { this.maxSize = size; top =0; stack = new Object[size]; } @Override public Object getTop() throws Exception { // TODO Auto-generated method stub if(isEmpty()) { throw new Exception("堆栈为空!"); } return stack[top-1]; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return top==0; } @Override public Object pop() throws Exception { // TODO Auto-generated method stub if(isEmpty()) { throw new Exception("堆栈为空!"); } top--; return stack[top]; } @Override public void push(Object obj) throws Exception { // TODO Auto-generated method stub //首先判断栈是否已满 if(top == maxSize) { throw new Exception("堆栈已满!"); } stack[top]=obj; top++; } } //链表堆栈 public class LinkStack implements Stack { Node head; //栈顶指针 int size; //结点的个数 public LinkStack() { head = null; size = 0; } @Override public Object getTop() throws Exception { // TODO Auto-generated method stub return head.getElement(); } @Override public boolean isEmpty() { // TODO Auto-generated method stub return head==null; } @Override public Object pop() throws Exception { // TODO Auto-generated method stub if(isEmpty()) { throw new Exception("栈为空!"); } Object obj = head.getElement(); head = head.getNext(); size--; return obj; } @Override public void push(Object obj) throws Exception { // TODO Auto-generated method stub head = new Node(obj,head); size++; } }
以上是节点,顺序栈和链表栈;
下面分别是表达式括号的匹配使用和表达式计算;
public class Test { public static String[] expToStringArray(String exp) { int n = exp.length(); String[] arr = new String[n]; for(int i=0;i<arr.length;i++) { arr[i]= exp.substring(i, i+1); } return arr; } //判断表达式的括号是否正确 public static void signCheck(String exp)throws Exception { SequenceStack stack = new SequenceStack(); String[] arr = Test.expToStringArray(exp); for(int i=0;i<arr.length;i++) { if(arr[i].equals("(")||arr[i].equals("[")||arr[i].equals("{")) { stack.push(arr[i]); } else if(arr[i].equals(")")&& !stack.isEmpty()&& stack.getTop().equals("(")) { stack.pop(); } else if(arr[i].equals(")")&& !stack.isEmpty()&& !stack.getTop().equals("(")) { System.out.println("左右括号匹配次序不正确!"); return ; } else if(arr[i].equals("]")&& !stack.isEmpty()&& stack.getTop().equals("[")) { stack.pop(); } else if(arr[i].equals("]")&& !stack.isEmpty()&& !stack.getTop().equals("[")) { System.out.println("左右括号匹配次序不正确!"); return ; } else if(arr[i].equals("}")&& !stack.isEmpty()&& stack.getTop().equals("{")) { stack.pop(); } else if(arr[i].equals("}")&& !stack.isEmpty()&& !stack.getTop().equals("{")) { System.out.println("左右括号匹配次序不正确!"); return ; } else if(arr[i].equals(")")||arr[i].equals("]")||arr[i].equals("}")&& stack.isEmpty()) { System.out.println("右括号多于左括号!"); return ; } } if(!stack.isEmpty()) { System.out.println("左括号多于右括号!"); } else { System.out.println("括号匹配正确!"); } } //计算后缀表达式,表达式一个一个根据规则入栈和出栈 public static void expCaculate(LinkStack stack) throws Exception { char ch; //扫描每次输入的字符。 int x1,x2,b=0; //x1,x2:两个操作数 ,b字符的ASCII码 System.out.println("输入后缀表达式并以#符号结束:"); while((ch =(char)(b=System.in.read()))!='#') { //如果是数字,说明是操作数则压入堆栈 if(Character.isDigit(ch)) { stack.push(new Integer(Character.toString(ch))); } //如果不是数字,说明是运算符 else { x2 = ((Integer)stack.pop()).intValue(); x1 = ((Integer)stack.pop()).intValue(); switch(ch) { case '+': x1+=x2; break; case '-': x1-=x2; break; case '*': x1*=x2; break; case '/': if(x2==0) { throw new Exception("分母不能为零!"); } else { x1/=x2; } break; } stack.push(new Integer(x1)); } } System.out.println("后缀表达式计算结果是:"+stack.getTop()); } /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //SequenceStack stack = new SequenceStack(10); /* LinkStack stack = new LinkStack(); Scanner in = new Scanner(System.in); int temp; for(int i=0;i<10;i++) { System.out.println("请输入第"+(i+1)+"个整数:"); temp = in.nextInt(); stack.push(temp); } while(!stack.isEmpty()) { System.out.println(stack.pop()); } */ //String str = "([(a+b)-(c*e)]+{a+b}"; //Test.signCheck(str); //(2+3)*(3-1)/2=5 23+31-*2/ LinkStack stack = new LinkStack(); Test.expCaculate(stack); } }