Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1790176
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-03-18 08:34:12

废话不多说,直接上代码, 栈是最简单的数据及结构, 可以使用数组实现也可以用链表实现. 并且有对泛型的支持.
接口

点击(此处)折叠或打开

  1. public interface Stack<E> {
  2.     boolean isEmpty();
  3.     E peek();
  4.     E pop();
  5.     void push(E item);
  6.     int size();
  7. }
这是个用链表实现的stack.

点击(此处)折叠或打开

  1. public class GenericLinkedStack<E> implements Stack<E> {
  2.     
  3.     private class Node<T>{
  4.         private T data;
  5.         private Node<T> next;
  6.         
  7.     
  8.         
  9.         private Node(T data,Node<T> next){
  10.             this.data=data;
  11.             this.next=next;
  12.         }
  13.     }

  14.     private Node<E> top;
  15.     private int size;
  16.     
  17.     public void push(E item){
  18.         top=new Node(item, top);
  19.         size++;
  20.     }
  21.     
  22.     public E pop(){
  23.         E result=top.data;
  24.         top=top.next;
  25.         size--;
  26.         return result;
  27.     }
  28.     
  29.     public int size(){
  30.         return size;
  31.     }
  32.     
  33.     public E peek(){
  34.         return top.data;
  35.     }
  36.     
  37.     public boolean isEmpty(){
  38.         return size==0;
  39.     }
  40.     
  41.     public static void main(String[] args) {
  42.         // TODO Auto-generated method stub
  43.         Stack<Integer> s = new GenericLinkedStack<>();
  44.         System.out.println(s.isEmpty());
  45.         for(int i=0;i<5;i++){
  46.             s.push(i);
  47.         }
  48.         for(int i=0;i<10;i++){
  49.             s.push(i);
  50.         }
  51.         System.out.println(s.isEmpty());
  52.         System.out.println(s.peek());
  53.         System.out.println(s.size());
  54.         System.out.println(s.pop());
  55.         
  56.         Stack<Double> d= new GenericLinkedStack<>();
  57.         System.out.println(d.isEmpty());
  58.         Random gen = new Random();
  59.         
  60.         for(int i=0;i<10;i++){
  61.             d.push(gen.nextGaussian());
  62.         }
  63.         System.out.println(d.isEmpty());
  64.         System.out.println(d.peek());
  65.         System.out.println(d.size());
  66.         System.out.println(d.pop());
  67.     }
  68. }

下面的是数组的.

点击(此处)折叠或打开

  1. public class GenericArrayStack<E> implements Stack<E>{
  2.     private int top=-1;
  3.     private E[] data;
  4.     private static final int DEFAULT_CAPACITY=10;
  5.     
  6.     public boolean isEmpty(){
  7.         return top==-1;
  8.     }
  9.     
  10.     public int size(){
  11.         return data.length;
  12.     }
  13.     
  14.     //constructor
  15.     public GenericArrayStack(){
  16.         data=(E[])new Object[DEFAULT_CAPACITY];
  17.     }
  18.     
  19.     public E peek(){
  20.         return data[top];
  21.     }
  22.     
  23.     public void push(E elem){
  24.         if(top==data.length-1)
  25.             resize(2*data.length);
  26.         data[++top]=elem;
  27.     }
  28.     
  29.     public E pop(){
  30.         if(isEmpty()) throw new EmptyStackException();
  31.         return data[top--];
  32.     }
  33.     
  34.     public void resize(int newCapacity){
  35.         E[] newdata = (E[])new Object[newCapacity];
  36.         for(int i=0;i<=top;i++){
  37.             newdata[i]=data[i];
  38.         }
  39.         data=newdata;
  40.     }
  41.     
  42.     
  43.     public static void main(String[] args) {
  44.         // TODO Auto-generated method stub
  45.         Stack<Integer> s = new GenericArrayStack<>();
  46.         System.out.println(s.isEmpty());
  47.         
  48.         for(int i=0;i<10;i++){
  49.             s.push(i);
  50.         }
  51.         System.out.println(s.isEmpty());
  52.         System.out.println(s.peek());
  53.         System.out.println(s.size());
  54.         System.out.println(s.pop());
  55.     }
  56. }
下面是解析算术表达式

点击(此处)折叠或打开

  1. package tree;

  2. public class Expression {
  3.     private static int rank(String op){
  4.         switch(op){
  5.         case "*":
  6.         case "/":
  7.             return 2;
  8.         case "+":
  9.         case "-":
  10.             return 1;
  11.         default :
  12.             return -1;
  13.         }
  14.     }
  15.     
  16.     private static final String SPACE=" ";
  17.     
  18.     public static String toPostfix(String expr){
  19.         StringBuilder result = new StringBuilder();
  20.         Stack<String> operators = new GenericArrayStack<>();
  21.         for(String token: expr.split("\\s+")){
  22.             if(rank(token) > 0){
  23.                 while(!operators.isEmpty() &&
  24.                     rank(operators.peek()) >= rank(token)){
  25.                     result.append(operators.pop()+SPACE);
  26.                     }
  27.                 operators.push(token);
  28.             }else{
  29.                 result.append(token+SPACE);
  30.             }
  31.         }
  32.         while(!operators.isEmpty()){
  33.             result.append(operators.pop()+SPACE);
  34.         }
  35.         return result.toString();    
  36.     }
  37.     
  38.     public static int compute(int i1, int i2, String op){
  39.         int result=0;
  40.         switch(op){
  41.             case "*":
  42.                 result = i1 * i2;
  43.                 break;
  44.             case "/":
  45.                 result = i1 / i2;
  46.                 break;
  47.             case "+":
  48.                 result = i1 + i2;
  49.                 break;
  50.             case "-":
  51.                 result = i1 - i2;
  52.                 break;
  53.         }
  54.         return result;
  55.     }
  56.     
  57.     public static int evalPostfix(String exp){
  58.         //Stack results = new GenericArrayStack<>();
  59.         Stack<String> operands = new GenericArrayStack<>();
  60.         int result =0;
  61.         for(String token: exp.split("\\s+")){
  62.             if(rank(token)> 0){
  63.                 int oper2=Integer.parseInt(operands.pop());
  64.                 int oper1=Integer.parseInt(operands.pop());
  65.                 result = compute(oper1, oper2, token);
  66.                 //System.out.println(result);
  67.                 operands.push(Integer.toString(result,10));
  68.             }else{
  69.                 operands.push(token);
  70.             }
  71.         }
  72.         return Integer.parseInt(operands.pop());
  73.     }
  74.     
  75.     public static void main(String[] args) {
  76.         // TODO Auto-generated method stub
  77.         int answer =0;
  78.         String exp = "3 5 1 - *";
  79.         answer = evalPostfix("3 5 1 - *");
  80.         System.out.println(answer);
  81.         
  82.         String exp2= "5 - 1 + 3 * 2";
  83.         System.out.println(evalPostfix(toPostfix(exp2)));
  84.     }

  85. }





阅读(2249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~