• java算法:堆栈ADT及实例


    java算法:堆栈ADT及实例

    在支持插入和删除数据项集的数据类型中,最重要的数据类型是堆栈。

    堆栈:是由两个基本操作构成的ADT,插入(或压入)一个新项,以及删除(或弹出)最近插入的数据项。

    例1:堆栈ADT接口

    Java代码 复制代码
    1. public interface IIntStack {   
    2.     int intStack(int value);   
    3.     int empty();   
    4.     void push(int value);   
    5.     int pop();   
    6. }  

    例2:中缀法:5*(((9+8)*(4*6))+7),后缀法:5 9 8 + 4 6 * * 7 + *,前缀法:* + 7 * + 9 8 * 4 6 5

    后缀计法和相关的压入栈操作应用到计算机中。如:

    5     5

    9     5    9

    8     5    9   8

    +     5    17

    4     5    17  4

    6     5    17  4  6

    *     5    17  24

    *     5    408

    7     5    408 7

    +     5    415

    *     2075

    Java代码 复制代码
    1. public class Postfix {   
    2.        
    3.     public static void main(String[] args) {   
    4.         char [] a = "598+46**7+*".toCharArray();   
    5.         int N = a.length;   
    6.         IIntStack s = new IntStackImpl();   
    7.         for(int i = 0; i < N; i++){   
    8.             if(a[i] == '+'){   
    9.                 s.push(s.pop() + s.pop());   
    10.             }   
    11.             if(a[i] == '*'){   
    12.                 s.push(s.pop() * s.pop());   
    13.             }   
    14.             if((a[i] >= '0') && (a[i] <= '9')){   
    15.                 s.push(0);   
    16.             }   
    17.             while((a[i] >= '0') && (a[i] <= '9')){   
    18.                 s.push(10 * s.pop() + (a[i++]-'0'));   
    19.             }   
    20.         }   
    21.         System.out.println(s.pop());   
    22.   
    23.     }   
    24.   
    25. }  

    例3:中缀到后缀的转换

    Java代码 复制代码
    1. public class InfixToPostfix {   
    2.        
    3.     public static void main(String[] args) {   
    4.         char [] a = "598+46**7+*".toCharArray();   
    5.         int N = a.length;   
    6.         IIntStack s = new IntStackImpl();   
    7.         for(int i = 0; i < N; i++){   
    8.             if(a[i] == ')'){   
    9.                 System.out.println(s.pop() + " ");   
    10.             }    
    11.             if((a[i] == '*') || (a[i] == '+')){   
    12.                 s.push(a[i]);   
    13.             }   
    14.             if((a[i] >= '0') && (a[i] <= '9')){   
    15.                 System.out.println(a[i] + " ");   
    16.             }   
    17.         }   
    18.         System.out.println(" ");   
    19.   
    20.     }   
    21.   
    22. }  

    栈的ADT实现

    主要:数组和链表

    例3:堆栈的数组实现

    Java代码 复制代码
    1. class  IntStack{   
    2.     private int[] s;   
    3.     private int N;   
    4.     IntStack(int maxN){   
    5.         s = new int[maxN];   
    6.         N = 0;   
    7.     }   
    8.     boolean isEmpty(){   
    9.         return (N == 0);   
    10.     }   
    11.     void push(int item){   
    12.         s[N++] = item;   
    13.     }   
    14.     int pop(){   
    15.         return s[--N];   
    16.     }   
    17. }<SPAN></SPAN>  

    例4:堆栈的链表实现

    Java代码 复制代码
    1. class intStack{   
    2.     private Node head;   
    3.     private class Node{   
    4.         int item;   
    5.         Node next;   
    6.         Node(int item, Node next){   
    7.             this.item = item;   
    8.             this.next = next;   
    9.         }   
    10.     }   
    11.     intStack(int maxN){   
    12.         head = null;   
    13.     }   
    14.     boolean isEmpty(){   
    15.         return (head == null);   
    16.     }   
    17.     void push(int item){   
    18.         head = new Node(item, head);   
    19.     }   
    20.     int pop(){   
    21.         int v = head.item;   
    22.         Node t = head.next;   
    23.         head = t;   
    24.         return v;   
    25.     }   
    26. }  


    客户程序并不关心在数组实现和链表实现中栈的项是否按不同的顺序存放的。实现可自由地使用任何数据结构,只要它能保持抽象堆栈的假象。链表实现能产生栈可以无限增长的假象。而在实际中是不可能的:到达一定时候,当无法满足请求更多内存的要求时,就会抛出异常。

     

    一般实现:定义一个接口,并实现object类型的堆栈,

    Java代码 复制代码
    1. Stack s = new Stack(N);   
    2. s.push(a);   
    3. s.push(b);   
    4. a = (Item) s.pop();   
    5. b = (Item) s.pop();  

    例5:一般堆栈

    Java代码 复制代码
    1. class Stack{   
    2.     private Object[] s;   
    3.     private int N;   
    4.     Stack(int maxN){   
    5.         s = new Object[maxN];   
    6.         N = 0;   
    7.     }   
    8.     boolean isEmpty(){   
    9.         return (M == 0);   
    10.     }   
    11.     void push(Object item){   
    12.         s[N++] = item;   
    13.     }   
    14.     Object pop(){   
    15.         Object t = s[--N];   
    16.         s[N] = null;   
    17.         return t;   
    18.     }   
    19. }  

    例6:整数栈的适配类

    Java代码 复制代码
    1. class IntStack{   
    2.     private Stack s;   
    3.     intStack(int maxN){   
    4.         s = new Stack(maxN);   
    5.     }   
    6.   
    7.     boolean isEmpty(){   
    8.         return s.isEmpty();   
    9.     }   
    10.     void push(int item){   
    11.         s.push(new Integer(item));   
    12.     }   
    13.     int pop(){   
    14.         return ((Integer)s.pop()).intValue();   
    15.     }   
    16.        
    17. }  

    创建新的ADT

    设计一个新的ADT:开发解决应用问题的客户程序,定义接口,验证ADT是否更容易实现客户程序,再考虑是否合理、效率实现ADT中的操作,如果不能,找到原因进行更改。多次之后,改进的客户程序和ADT,采取策略冻结接口。

    例7:等价关系ADT接口,连通性问题:初始化一个抽象数据结构来跟踪给定节点数之间的联系,判断两个给定节点是否连通,并把两个节点连起来,以后就认为它们是连通的。

    Java代码 复制代码
    1. class UF{   
    2.     UF(int N);   
    3.     boolean find(int x; int y);   
    4.     void unite(int x; int y);   
    5. }  

    例8:等价关系ADT的客户

    Java代码 复制代码
    1. class Equivalence{   
    2.     public static void main(String [] args){   
    3.         int p,q, N = 100;   
    4.         UF info = new UF(N);   
    5.         for(In.init(); !In.empty();){   
    6.             p = In.getInt(); q = In.getInt();   
    7.             if(!info.find(p,q)){   
    8.                 info.unite(p,q);   
    9.                 System.out.println(p + "-" + q);   
    10.             }   
    11.         }   
    12.     }      
    13. }  

    例9:等价关系ADT实现:加权快速合并实现接口:

    Java代码 复制代码
    1. private int [] id, sz;   
    2. private int find(int x){   
    3.     while(x != id[x]){   
    4.         x = id[x];   
    5.     }   
    6.     return x;   
    7. }   
    8. UF(int N){   
    9.     id = new int[N];   
    10.     sz = new int[N];   
    11.     for(int i = 0; i < N; i++){   
    12.         id[i] = i;   
    13.         sz[i] = i;   
    14.     }   
    15. }   
    16. boolean find(int p, int q){   
    17.     return (find(p) == find(q));   
    18. }   
    19. void unite(int p, int q){   
    20.     int i = find(p), j = find(q);   
    21.     if(i == j){   
    22.         return;   
    23.     }   
    24.     if(sz[i] < sz[j]){   
    25.         id[i] = j; sz[j] += sz[i];   
    26.     }else{   
    27.         id[j] = i; sz[i] += sz[j];   
    28.     }   
    29. }  

    比较:把解决高层(连通性)问题的任务和解决低层(合并-查找)的问题分开了,从而使我们能独立解决这两个问题;为我们提供了把解决该问题的不同算法和数据结构进行比较的自然方式;通过该接口定义了一个方法来检查软件是否按所期望的操作;为我们提供了一种更新的表示(新数据结构或新算法)而根本不需要改变客户程序的方法;为我们提供了一个能用来构建其他算法的抽象结构。

    注意:使用抽象类或接口会产生运行时开销,每个抽象方法的调用需要跟踪表中对方法的一个引用。算法与数据结构经常用于系统中关键的性能部分,不希望以此为代价获得抽象类和接口的灵活性。

  • 相关阅读:
    2020-04-07 python一行代码 http服务器文件共享
    2020-04-06 linux命令之awk
    2020-04-05 ubuntu安装docker并使用国内加速
    2020-04-04 ssh免密登录
    尚学堂 JAVA DAY11 概念总结
    尚学堂 JAVA Day3 概念总结
    尚学堂 JAVA Day1 概念总结
    Android Studio 首次安装报错 Java.lang.RuntimeException:java.lang.NullPointerException...错
    Android 迷之Version管理
    Android Develop 之 Ddevelop WorkFlow Basics
  • 原文地址:https://www.cnblogs.com/wuyida/p/6301151.html
Copyright © 2020-2023  润新知