Java 算法(背包,队列和栈)

Dijkstra的双栈算术表达式求值算法:

 1 import java.util.*;
 2 public class Main
 3 {
 4     public static double evaluate(String abs) {
 5         Stack<String> ops = new Stack<String>();
 6         Stack<Double> vals = new Stack<Double>();
 7         char arr[]=abs.toCharArray();
 8         for(char c:arr){
 9             String s=c+"";
10            if(s.equals(""));
11            else if(s.equals("("));
12            else if (s.equals("+")) ops.push(s);
13            else if (s.equals("-")) ops.push(s);
14            else if (s.equals("*")) ops.push(s);
15            else if (s.equals("/")) ops.push(s);
16            else if (s.equals(")"))
17            {
18                String op=ops.pop();
19                double v=vals.pop();
20                if(op.equals("*")) v=vals.pop()*v;
21                else if(op.equals("+")) v=vals.pop()+v;
22                else if(op.equals("-")) v=vals.pop()-v;
23                else if(op.equals("/")) v=vals.pop()/v;
24                vals.push(v);
25            }
26            else vals.push(Double.parseDouble(s));
27         }
28         return vals.pop();
29     }
30     public static void main(String args[])
31     {
32         Scanner s=new Scanner(System.in);
33         String s1=s.next();
34         System.out.print(evaluate(s1));
35     }
36 }

 下压栈(LIFO)能够动态调整数组大小的实现

 1 import java.util.*;
 2 import java.lang.*;
 3 public class Main<Item> implements Iterable<Item> {
 4     private Item[] a = (Item[])new Object[1];//栈元素
 5     private int N = 0;//元素数量
 6 
 7     public boolean isEmpty(){
 8         return N == 0;
 9     }
10 
11     public int size(){
12         return N;
13     }
14 
15     public void resize(int max){
16         Item[] temp = (Item[])new Object[max];
17         for(int i = 0;i<N;i++){
18             temp[i] = a[i];
19         }
20         a = temp;
21     }
22 
23     public void push(Item item){
24         if(N == a.length) resize( 2 * a.length);
25         a[N++] = item;
26     }
27 
28     public Item pop(){
29         Item item = a[--N];
30         a[N] = null;
31         if(N>0 && N == a.length/4) resize(a.length/2);
32         return item;
33     }
34 
35    // @Override
36     public Iterator<Item> iterator() {
37         return new ReverseArrayIterator();
38     }
39 
40     private class ReverseArrayIterator implements Iterator<Item>{
41         private int i = N;
42         public boolean hasNext() {
43             return i > 0;
44         }
45 
46         public Item next() {
47             return a[--i];
48         }
49 
50        // @Override
51         public void remove() {
52             throw new UnsupportedOperationException();
53         }
54 
55     }
56     public static void main(String args[])
57     {
58         Main<String> s=new Main<String>();
59         Scanner cin=new Scanner(System.in);
60         System.out.println("Input a String end with $:");
61         while(cin.hasNext()){
62             String item=cin.next();
63             if(item.equals("$")){
64                 break;
65             }
66             else {
67                 s.push(item);
68             }
69         }
70         // foreach语句是while语句的一种简写方式
71         System.out.println("display by for:");
72         for(String str : s){
73             System.out.print(str+" ");
74         }
75         System.out.println();
76         // which
77         System.out.println("display by which:");
78         Iterator<String> it=s.iterator();
79         while(it.hasNext()){
80             String i=it.next();
81             System.out.print(i+" ");
82         }
83     }
84 
85 }

 

posted @ 2017-04-23 19:54  女王公园的八神  阅读(213)  评论(0编辑  收藏  举报