zoukankan      html  css  js  c++  java
  • java实现双向循环链表

     
    1.双向循环链表的优缺点
    和单向循环链表类似,但是可以直接访问某个结点的直接前驱和直接后继,比较方便。
    2.实现思路
    采用内部类的方式实现,Link为外部链表类,Node为内部结点类。外部类定义宏观上的方法和调用内部类的方法,内部类定义对结点的操作方法。
    3.实现细节和注意要点
    1.还是始终要铭记链表没有下标,每次要使用下标都是从头开始遍历,这里定义的是index,每次需要使用的时候记得一定要初始化。
    2.这里将链表转化为数组输出,需要动态的申请数组,所以再调用转化成数组输出的方法的时候,一定一定一定要记得new.
     
     
      1 package Doublelink;
      2 /**
      3 * @类名  Link.java
      4 * @作者       修仙小华
      5 * @版本  V1.0
      6 * @日期  2019年7月22日-下午1:02:27
      7 * @描述  
      8 *
      9 */
     10 public class Link {
     11     int count=0;
     12     int index=0;
     13     String arrayList[];
     14     Node header=new Node();
     15     
     16     public void init() {
     17         header.data=null;
     18         header.root=header;
     19         header.next=header;
     20     }
     21     
     22     /**
     23      * 根据输入下标返回当前结点的直接前驱和直接后继
     24      */
     25     public String returnRootNext(int index) {
     26         this.index=0;
     27         if (header.next==header) {
     28             return null;
     29         }else {
     30             return header.next.returnRootNextNode(index);
     31         }
     32     }
     33     /**
     34      * 增加结点的方法
     35      */
     36     public void add(String data) {
     37         Node node=new Node(data);
     38         this.count++;
     39         if (header.next==header) {
     40             header.next=node;
     41             header.root=node;
     42             node.root=header;
     43             node.next=header;
     44         }else {
     45             header.next.addNode(node);
     46         }
     47     }
     48     /**
     49      * 通过下标查找数据
     50      */
     51     public String get(int index) {
     52         this.index=0;
     53         if (index<0||index>count-1) {
     54             return null;
     55         }else {
     56             return header.next.getNode(index);
     57         }
     58     }
     59     /**
     60      * 判断链表是否为空
     61      * @return
     62      */
     63     public boolean isEmparty() {
     64         if (header.next==header) {
     65             return true;
     66         }else {
     67             return false;
     68         }
     69     }
     70     /**
     71      * 将链表转化成数组输出
     72      */
     73     public void array() {
     74         this.index=0;
     75         arrayList=new String[count];
     76         if (this.isEmparty()) {
     77             return;
     78         }else {
     79             header.next.arrayNode();
     80         }
     81         for (int i = 0; i < arrayList.length; i++) {
     82             System.out.print(arrayList[i]+" ");
     83         }
     84         System.out.println();
     85         
     86     }
     87 //=================================================================
     88     class Node{
     89         String data;
     90         Node next;
     91         Node root;
     92         /**
     93          * 构造方法
     94          */
     95         public Node() {
     96             
     97         }
     98         /**
     99          * 根据输入下标返回当前结点的直接前驱和直接后继
    100          * @return 
    101          */
    102         public String returnRootNextNode(int index) {
    103             if (Link.this.index++ ==index) {
    104                 return this.root.data+" "+this.next.data;
    105             }else {
    106                 return this.next.returnRootNextNode(index);
    107             }
    108             
    109         }
    110         /**
    111          *通过下标查找数据
    112          */
    113         public String  getNode(int index) {
    114             if (Link.this.index++ ==index) {
    115                 return this.data;
    116             }else {
    117                 return this.next.getNode(index);
    118             }
    119             
    120         }
    121         public Node(String data) {
    122             this.data=data;
    123         }
    124         /**
    125          * 将链表转化为数组输出
    126          */
    127         public void arrayNode() {
    128             Link.this.arrayList[Link.this.index++]=this.data;
    129             if (this.next!=header) {
    130                 this.next.arrayNode();
    131             }
    132         }
    133         
    134         /**
    135          *  增加结点的方法
    136          */
    137         public void addNode(Node node) {
    138             if (this.next==header) {
    139                 this.next=node;
    140                 node.root=this;
    141                 node.next=header;
    142                 header.root=node;
    143                 
    144             }else {
    145                 this.next.addNode(node);
    146             }
    147             
    148         }
    149         
    150     }
    151 }

    测试类:

    public class LinkTest {
        public static void main(String[] args) {
            Link link=new Link();
            link.init();
            link.add("袁帅");
            link.add("袁丑");
            link.add("袁美");
            link.add("袁丧");
            link.add("袁泉");
    //        System.out.println(link.get(0));
    //        System.out.println(link.get(1));
    //        System.out.println(link.get(2));
    //        System.out.println(link.get(3));
    //        System.out.println(link.get(4));
    //        
    //        link.isEmparty();
            link.array();
            System.out.println(link.returnRootNext(1));
        }
    
    }
  • 相关阅读:
    java包装类的缓存机制(转)
    分布式事务的思考(转)
    分布式事务框架介绍与使用案例
    java 内部类、匿名内部类
    spring cloud应用
    Oralce学习笔记(六)
    部署CentOS虚拟机集群
    商品详情页系统架构
    Oracle学习笔记(五)
    hystrix完成对redis访问的资源隔离
  • 原文地址:https://www.cnblogs.com/had1314/p/11267937.html
Copyright © 2011-2022 走看看