最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例

 更新時(shí)間:2014年03月21日 09:14:14   作者:  
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表的示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

/**
 * 雙向鏈表的實(shí)現(xiàn)
 * @author Skip
 * @version 1.0
 */
public class DoubleNodeList<T> {
 //節(jié)點(diǎn)類
 private static class Node<T>{
  Node<T> perv;  //前節(jié)點(diǎn)
  Node<T> next;  //后節(jié)點(diǎn)
  T data;    //數(shù)據(jù)

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

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

 /**
  * 初始化時(shí)創(chuàng)建一個(gè)節(jié)點(diǎn)
  * @param data 數(shù)據(jù)
  */
 public DoubleNodeList(T data){
  head = new Node<T>(data);
  last = head;
  length = 1;
 }

 /**
  * 添加一個(gè)節(jié)點(diǎn)
  * @param data 添加的數(shù)據(jù)
  */
 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++;
  }
 }

 /**
  * 在指定數(shù)據(jù)后插入一個(gè)節(jié)點(diǎn)
  * @param data 指定的數(shù)據(jù)
  * @param insertData 插入的數(shù)據(jù)
  * @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;
    //判斷是否在最后一個(gè)節(jié)點(diǎn)后添加節(jié)點(diǎn)
    if(t.next==null){
     last = t;
    }
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 在指定數(shù)據(jù)前插入一個(gè)節(jié)點(diǎn)
  * @param data 指定的數(shù)據(jù)
  * @param insertData 插入的數(shù)據(jù)
  * @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;
 }

 /**
  * 獲得索引處的數(shù)據(jù)
  * @param index 索引
  * @return 數(shù)據(jù)
  */
 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;
 }

 /**
  * 獲得最后一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  * @return 最后一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  */
 public T getLast(){
  return last.data;
 }

 /**
  * 獲得第一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  * @return 第一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  */
 public T getFirst(){
  return head.data;
 }

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

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

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

 /**
  * 輸出鏈表內(nèi)所有節(jié)點(diǎn)
  */
 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();
  }
 }
}

相關(guān)文章

最新評(píng)論

邵阳县| 福海县| 株洲市| 得荣县| 盈江县| 内江市| 神农架林区| 金溪县| 莫力| 平谷区| 青神县| 樟树市| 澳门| 武冈市| 开化县| 郧西县| 昆明市| 谷城县| 二连浩特市| 绥阳县| 海林市| 阿拉善左旗| 浪卡子县| 泰和县| 平湖市| 磴口县| 徐水县| 青阳县| 寿宁县| 扎赉特旗| 抚松县| 澎湖县| 乌兰县| 体育| 临沧市| 宜都市| 宜兰县| 凤翔县| 樟树市| 泾源县| 泰兴市|