国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例

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

2019-11-15 14:35java技術(shù)網(wǎng) JAVA教程

這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表的示例,需要的朋友可以參考下

代碼如下:


/**
 * 雙向鏈表的實(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)度

 /**
  * 無(wú)參構(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();
  }
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 北条麻妃一区二区三区在线观看 | 亚洲精品成人天堂一二三 | 国产欧美综合一区二区三区 | 欧美成人a | 日韩电影在线 | 中文字幕av第一页 | 成人一区二区三区 | 亚洲精品国产精品国自产在线 | 国产成人无遮挡在线视频 | 中文字幕高清视频 | 超碰在线观看97 | 成人av影视 | 黄色av网 | 欧美日韩高清在线观看 | 在线观看亚洲区 | 一级黄片毛片免费看 | 久久综合av | 久久久久国产一区二区三区四区 | 97久久精品午夜一区二区 | 欧美一区二区三区成人 | 久草久| 中文字幕日产乱码六区小草 | 亚洲精品一区中文字幕乱码 | 人人99| 国产目拍亚洲精品99久久精品 | 一区二区在线视频 | 日韩精品 电影一区 亚洲 | 91视频.www| 日本精品久久 | 国产精品区二区三区日本 | 狠狠操av| 中文字幕高清在线播放 | 成人羞羞视频免费 | 毛片久久久 | 亚洲欧美综合 | 久久小草 | 国产精品爱久久久久久久 | 久久作爱视频 | 欧美国产视频 | 欧美激情亚洲 | 国产久 |