国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java數(shù)據(jù)結構之實現(xiàn)雙向鏈表的示例

2019-11-26 15:35:50
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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

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

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

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

 /**
  * 添加一個節(jié)點
  * @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ù)后插入一個節(jié)點
  * @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;
    //判斷是否在最后一個節(jié)點后添加節(jié)點
    if(t.next==null){
     last = t;
    }
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 在指定數(shù)據(jù)前插入一個節(jié)點
  * @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;
 }

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

 /**
  * 獲得第一個節(jié)點的數(shù)據(jù)
  * @return 第一個節(jié)點的數(shù)據(jù)
  */
 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;
 }

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 惠安县| 孝义市| 高青县| 汽车| 全椒县| 云龙县| 德清县| 南开区| 崇阳县| 南川市| 灵石县| 抚远县| 瓮安县| 叶城县| 浦江县| 孝义市| 西藏| 开江县| 衡阳县| 沛县| 聂拉木县| 岑巩县| 陇西县| 揭东县| 武川县| 晋中市| 大悟县| 云阳县| 洛浦县| 浑源县| 罗源县| 民权县| 安乡县| 宝丰县| 盐城市| 宝应县| 衡阳县| 大渡口区| 侯马市| 广灵县| 安平县|