• JAVA双向链表


    1.链表是一种重要的数据结构,在程序设计中占有很重要的地位

    2.我们可以用类List来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。存储当前结点的指针时有一定的技 巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,当其值为null时表示当前结点是第一个结点。那么为什么要这样做呢? 这是因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。那么如何得到当前结点呢,我们定义了一 个方法cursor(),返回值是指向当前结点的指针。类List还定义了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种 操作。例如reset()方法使第一个结点成为当前结点。insert(Object d)方法在当前结点前插入一个结点,并使其成为当前结点。remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,如果删除的是最 后一个结点,则第一个结点变为当前结点。

      1 /**
      2  * 双向链表的实现
      3  * @author Skip
      4  * @version 1.0
      5  */
      6 public class DoubleNodeList<T> {
      7  //节点类
      8  private static class Node<T>{
      9   Node<T> perv;  //前节点
     10   Node<T> next;  //后节点
     11   T data;    //数据
     12 
     13   public Node(T t){
     14    this.data = t;
     15   }
     16  }
     17  private Node<T> head;  //头节点
     18  private Node<T> last;  //尾节点
     19  private Node<T> other;  //备用节点存放临时操作
     20  private int length;  //链表长度
     21 
     22  /**
     23   * 无参构造
     24   */
     25  public DoubleNodeList(){
     26   head = new Node<T>(null);
     27   last = head;
     28   length = 0;
     29  }
     30 
     31  /**
     32   * 初始化时创建一个节点
     33   * @param data 数据
     34   */
     35  public DoubleNodeList(T data){
     36   head = new Node<T>(data);
     37   last = head;
     38   length = 1;
     39  }
     40 
     41  /**
     42   * 添加一个节点
     43   * @param data 添加的数据
     44   */
     45  public void add(T data){
     46   if(isEmpty()){
     47    head = new Node<T>(data);
     48    last = head;
     49    length++;
     50   }else{
     51    //尾插法
     52    other = new Node<T>(data);
     53    other.perv = last;
     54    last.next = other;
     55    last = other;
     56    length++;
     57   }
     58  }
     59 
     60  /**
     61   * 在指定数据后插入一个节点
     62   * @param data 指定的数据
     63   * @param insertData 插入的数据
     64   * @return 插入成功返回true,不成功返回false
     65   */
     66  public boolean addAfert(T data , T insertData){
     67   other = head;
     68   while(other != null){
     69    if(other.data.equals(data)){
     70     Node<T> t = new Node<T>(insertData);
     71     t.perv = other;
     72     t.next = other.next;
     73     other.next = t;
     74     //判断是否在最后一个节点后添加节点
     75     if(t.next==null){
     76      last = t;
     77     }
     78     length++;
     79     return true;
     80    }
     81    other = other.next;
     82   }
     83   return false;
     84  }
     85 
     86  /**
     87   * 在指定数据前插入一个节点
     88   * @param data 指定的数据
     89   * @param insertData 插入的数据
     90   * @return 插入成功返回true,不成功返回false
     91   */
     92  public boolean addBefore(T data, T insertData){
     93   other = head;
     94   while(other != null){
     95    if(other.data.equals(data)){
     96     Node<T> t = new Node<T>(insertData);
     97     t.perv = other.perv;
     98     t.next = other;
     99     other.perv.next = t;
    100     length++;
    101     return true;
    102    }
    103    other = other.next;
    104   }
    105   return false;
    106  }
    107 
    108  /**
    109   * 获得索引处的数据
    110   * @param index 索引
    111   * @return 数据
    112   */
    113  public T get(int index){
    114   if(index>length || index<0){
    115    throw new IndexOutOfBoundsException("索引越界:"+index);
    116   }
    117   other = head;
    118   for(int i=0;i<index;i++){
    119    other = other.next;
    120   }
    121   return other.data;
    122  }
    123 
    124  /**
    125   * 新值替换旧值
    126   * @return 成功为true,未找到为false
    127   */
    128  public boolean set(T oldValue,T newValue){
    129   other = head;
    130   while(other!=null){
    131    if(other.data.equals(oldValue)){
    132     other.data = newValue;
    133     return true;
    134    }
    135    other = other.next;
    136   }
    137   return false;
    138  }
    139 
    140  /**
    141   * 移除指定的元素
    142   * @param data 需要移除的元素
    143   * @return 不存在为false,成功为true
    144   */
    145  public boolean remove(T data){
    146   other = head;
    147   while(other != null){
    148    if(other.data.equals(data)){
    149     other.perv.next = other.next;
    150     length--;
    151     return true;
    152    }
    153    other = other.next;
    154   }
    155   return false;
    156  }
    157 
    158  /**
    159   * 链表中是否包含此元素
    160   * @return 包含为true,不包含为false
    161   */
    162  public boolean contains(T data){
    163   other = head;
    164   while(other != null){
    165    if(other.data.equals(data)){
    166     return true;
    167    }
    168    other = other.next;
    169   }
    170   return false;
    171  }
    172 
    173  /**
    174   * 获得最后一个节点的数据
    175   * @return 最后一个节点的数据
    176   */
    177  public T getLast(){
    178   return last.data;
    179  }
    180 
    181  /**
    182   * 获得第一个节点的数据
    183   * @return 第一个节点的数据
    184   */
    185  public T getFirst(){
    186   return head.data;
    187  }
    188 
    189  /**
    190   * 获得链表的长度
    191   * @return 长度
    192   */
    193  public int getSize(){
    194   return length;
    195  }
    196 
    197  /**
    198   * 是否为空链表
    199   * @return 空链表为true,非空链表为false
    200   */
    201  public boolean isEmpty(){
    202   return length==0;
    203  }
    204 
    205  /**
    206   * 清空链表
    207   */
    208  public void clear(){
    209   head = null;
    210   length = 0;
    211  }
    212 
    213  /**
    214   * 输出链表内所有节点
    215   */
    216  public void printList(){
    217   if(isEmpty()){
    218    System.out.println("空链表");
    219   }else{
    220    other = head;
    221    for(int i=0;i<length;i++){
    222     System.out.print(other.data+" ");
    223     other = other.next;
    224    }
    225    System.out.println();
    226   }
    227  }
    228 }
  • 相关阅读:
    [CLYZ2017]day8
    [CLYZ2017]day12
    [bzoj1503][NOI2004]郁闷的出纳员
    [CLYZ2017]day18
    [CLYZ2017]day11
    [CLYZ2017]day17
    在DLL中获取服务器路径
    SPSecurity.RunWithElevatedPrivileges 拒绝访问
    prototype1.4.0(转载)
    删除多表数据
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5766740.html
Copyright © 2020-2023  润新知