一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java數據結構之實現雙向鏈表的示例

java數據結構之實現雙向鏈表的示例

2019-11-15 14:35java技術網 JAVA教程

這篇文章主要介紹了java數據結構實現雙向鏈表的示例,需要的朋友可以參考下

代碼如下:


/**
 * 雙向鏈表的實現
 * @author Skip
 * @version 1.0
 */
public class DoubleNodeList<T> {
 //節點類
 private static class Node<T>{
  Node<T> perv;  //前節點
  Node<T> next;  //后節點
  T data;    //數據

  public Node(T t){
   this.data = t;
  }
 }
 private Node<T> head;  //頭節點
 private Node<T> last;  //尾節點
 private Node<T> other;  //備用節點存放臨時操作
 private int length;  //鏈表長度

 /**
  * 無參構造
  */
 public DoubleNodeList(){
  head = new Node<T>(null);
  last = head;
  length = 0;
 }

 /**
  * 初始化時創建一個節點
  * @param data 數據
  */
 public DoubleNodeList(T data){
  head = new Node<T>(data);
  last = head;
  length = 1;
 }

 /**
  * 添加一個節點
  * @param data 添加的數據
  */
 public void add(T data){
  if(isEmpty()){
   head = new Node<T>(data);
   last = head;
   length++;
  }else{
   //尾插法
   other = new Node<T>(data);
   other.perv = last;
   last.next = other;
   last = other;
   length++;
  }
 }

 /**
  * 在指定數據后插入一個節點
  * @param data 指定的數據
  * @param insertData 插入的數據
  * @return 插入成功返回true,不成功返回false
  */
 public boolean addAfert(T data , T insertData){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    Node<T> t = new Node<T>(insertData);
    t.perv = other;
    t.next = other.next;
    other.next = t;
    //判斷是否在最后一個節點后添加節點
    if(t.next==null){
     last = t;
    }
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 在指定數據前插入一個節點
  * @param data 指定的數據
  * @param insertData 插入的數據
  * @return 插入成功返回true,不成功返回false
  */
 public boolean addBefore(T data, T insertData){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    Node<T> t = new Node<T>(insertData);
    t.perv = other.perv;
    t.next = other;
    other.perv.next = t;
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 獲得索引處的數據
  * @param index 索引
  * @return 數據
  */
 public T get(int index){
  if(index>length || index<0){
   throw new IndexOutOfBoundsException("索引越界:"+index);
  }
  other = head;
  for(int i=0;i<index;i++){
   other = other.next;
  }
  return other.data;
 }

 /**
  * 新值替換舊值
  * @return 成功為true,未找到為false
  */
 public boolean set(T oldValue,T newValue){
  other = head;
  while(other!=null){
   if(other.data.equals(oldValue)){
    other.data = newValue;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 

 /**
  * 移除指定的元素
  * @param data 需要移除的元素
  * @return 不存在為false,成功為true
  */
 public boolean remove(T data){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    other.perv.next = other.next;
    length--;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 鏈表中是否包含此元素
  * @return 包含為true,不包含為false
  */
 public boolean contains(T data){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 獲得最后一個節點的數據
  * @return 最后一個節點的數據
  */
 public T getLast(){
  return last.data;
 }

 /**
  * 獲得第一個節點的數據
  * @return 第一個節點的數據
  */
 public T getFirst(){
  return head.data;
 }

 /**
  * 獲得鏈表的長度
  * @return 長度
  */
 public int getSize(){
  return length;
 }

 /**
  * 是否為空鏈表
  * @return 空鏈表為true,非空鏈表為false
  */
 public boolean isEmpty(){
  return length==0;
 }

 /**
  * 清空鏈表
  */
 public void clear(){
  head = null;
  length = 0;
 }

 /**
  * 輸出鏈表內所有節點
  */
 public void printList(){
  if(isEmpty()){
   System.out.println("空鏈表");
  }else{
   other = head;
   for(int i=0;i<length;i++){
    System.out.print(other.data+" ");
    other = other.next;
   }
   System.out.println();
  }
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美brazzers| 日本68xxxxxxxxx59 日本 视频 在线 | 欧美人与禽交片在线播放 | 天堂资源wwww在线看 | 天堂漫画破解版 | 欧美在线视频7777kkkk | 四虎影视永久在线 | 欧美一级专区免费大片 | 波多野结衣之双方调教在线观看 | 日韩基地1024首页 | 亚洲乱码一区二区三区国产精品 | 成人免费视频一区 | 国色天香社区在线视频播放 | 双子母性本能在线观看 | 操弄哥哥的108种姿势 | 人妖欧美一区二区三区四区 | 亚洲精品二三区伊人久久 | 欧美综合精品一区二区三区 | 啪啪无尽3d动漫漫画免费网站 | av中文字幕在线 | 午夜五月天 | 久久黄色大片 | 动漫美女胸被狂揉扒开吃奶动态图 | 青青青在线视频播放 | 日日操美女 | 精品久久久久久久高清 | 免费观看二十女人一摸是水 | 欧美日韩亚洲国内综合网香蕉 | 四虎最新永久在线精品免费 | 欧美成人香蕉在线观看 | 手机看片国产免费久久网 | 国产精品久久久久久久午夜片 | 日本高清在线精品一区二区三区 | 日本aa大片在线播放免费看 | 乌克兰少妇大胆大BBW | 色老头综合网 | 国产盗摄wc厕所撒尿视频 | 91高跟丝袜 | 五月最新女厕所高跟嘘嘘 | 欧美se图| 午夜影院一区二区三区 |