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

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

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

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

/**
 * 單向鏈表
 *
 */
public class NodeList<E> {
 private static class Node<E> { // 節(jié)點(diǎn)類
  E data; // 節(jié)點(diǎn)上的數(shù)據(jù)
  Node<E> next; // 指向下一個(gè)節(jié)點(diǎn)

  Node(E e) {
   this.data = e;
   this.next = null;
  }
 }

 private Node<E> head; // 鏈表的頭節(jié)點(diǎn)
 private Node<E> last; // 鏈表的尾節(jié)點(diǎn)
 private Node<E> other = null;
 private int length = 0; // 節(jié)點(diǎn)數(shù)量

 /**
  * 無參構(gòu)造方法
  */
 public NodeList() {
  // 默認(rèn)節(jié)點(diǎn)為空
  this.head = new Node<E>(null);
 }

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

 /**
  * 添加一個(gè)節(jié)點(diǎn)(尾插法)
  *
  * @param data
  *            數(shù)據(jù)
  */
 public void add(E data) {
  if (isEmpty()) {
   head = new Node<E>(data);
   last = head;
   length++;
  } else {
   Node<E> newNode = new Node<E>(data);
   last.next = newNode;
   last = newNode;
  }
 }

 /**
  * 獲得索引處的數(shù)據(jù)(索引輸入錯(cuò)誤拋出越界異常)
  * @param index 索引
  * @return 索引處數(shù)據(jù)
  */
 public E get(int index){
  if(index<0 || index>length){
   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(E oldValue,E newValue){
  other = head;
  while(other!=null){
   if(other.data.equals(oldValue)){
    other.data = newValue;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 在指定元素后插入一個(gè)元素
  *
  * @param data
  *            指定的元素
  * @param insertData
  *            需要插入的元素
  * @return false為未找到元素,true為插入成功
  */
 public boolean add(E data, E insertData) {
  other = head;
  while (other != null) {
   if (other.data.equals(data)) {
    Node<E> newNode = new Node<E>(insertData);
    Node<E> temp = other.next;
    newNode.next = temp;
    other.next = newNode;
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

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

 /**
  * 移除指定的元素
  * @param data 需要移除的元素
  * @return 不存在為false,成功為true
  */
 public boolean remove(E data){
  other = head;
  Node<E> temp = head;  //臨時(shí)變量,用于保存前一個(gè)節(jié)點(diǎn)
  while(other!=null){
   if(other.data.equals(data)){
    temp.next = other.next;
    length--;
    return true;
   }
   temp = other;
   other = other.next;
  }
  return false;
 }

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

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

 /**
  * 輸出所有節(jié)點(diǎn)
  */
 public void printLink() {
  if(isEmpty()){
   System.out.println("空鏈表");
  }else{
   other = head;
   while (other != null) {
    System.out.print(other.data);
    other = other.next;
   }
   System.out.println();
  }
 }
}

相關(guān)文章

  • SpringCloud-Config分布式配置代碼示例

    SpringCloud-Config分布式配置代碼示例

    這篇文章主要介紹了SpringCloud-Config分布式配置代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式)

    Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式)

    這篇文章主要介紹了Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • idea設(shè)置@Author文件頭注釋的實(shí)現(xiàn)步驟

    idea設(shè)置@Author文件頭注釋的實(shí)現(xiàn)步驟

    本文主要介紹了idea設(shè)置@Author文件頭注釋的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • struts2框架的登錄制作圖文教程

    struts2框架的登錄制作圖文教程

    下面小編就為大家?guī)硪黄猻truts2框架的登錄制作圖文教程。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • IDEA GIT 忽略文件的最佳方式推薦

    IDEA GIT 忽略文件的最佳方式推薦

    這篇文章主要介紹了IDEA GIT 忽略文件的最佳方式推薦,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java實(shí)現(xiàn)赫夫曼樹(哈夫曼樹)的創(chuàng)建

    Java實(shí)現(xiàn)赫夫曼樹(哈夫曼樹)的創(chuàng)建

    給定N個(gè)權(quán)值作為N個(gè)葉子結(jié)點(diǎn),構(gòu)造一棵二叉樹,若該樹的帶權(quán)路徑長(zhǎng)度(WPL)達(dá)到最小,稱這樣的二叉樹為最優(yōu)二叉樹,也稱為哈夫曼樹(Huffman Tree)。這篇文章主要就是為大家介紹如何通過Java實(shí)現(xiàn)赫夫曼樹,需要的朋友可以參考一下
    2021-12-12
  • spring的13個(gè)經(jīng)典面試題

    spring的13個(gè)經(jīng)典面試題

    Spring框架是一個(gè)開放源代碼的J2EE應(yīng)用程序框架,是針對(duì)bean的生命周期進(jìn)行管理的輕量級(jí)容Spring解決了開發(fā)者在J2EE開發(fā)中遇到的許多常見的問題,我們這篇文章就來了解一下spring的面試題
    2021-06-06
  • Java設(shè)計(jì)模式之工廠方法和抽象工廠

    Java設(shè)計(jì)模式之工廠方法和抽象工廠

    本文詳細(xì)講解了Java設(shè)計(jì)模式之工廠方法和抽象工廠,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • 詳解Java如何利用位操作符創(chuàng)建位掩碼

    詳解Java如何利用位操作符創(chuàng)建位掩碼

    在本文中,我們來看看如何使用位操作符實(shí)現(xiàn)低級(jí)別的位掩碼。我們將看到我們?nèi)绾螌⒁粋€(gè)單一的int變量作為一個(gè)單獨(dú)的數(shù)據(jù)容器,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-10-10
  • Java的MyBatis框架項(xiàng)目搭建與hellow world示例

    Java的MyBatis框架項(xiàng)目搭建與hellow world示例

    MyBatis框架為Java程序的數(shù)據(jù)庫操作帶來了很大的便利,這里我們就從最基礎(chǔ)的入手,來看一下Java的MyBatis框架項(xiàng)目搭建與hellow world示例,需要的朋友可以參考下
    2016-06-06

最新評(píng)論

长治县| 屏边| 沙湾县| 灌云县| 东海县| 肥城市| 新巴尔虎左旗| 汉沽区| 北宁市| 浪卡子县| 明星| 岳池县| 星子县| 视频| 济南市| 双鸭山市| 陇川县| 桐柏县| 新营市| 宜川县| 基隆市| 黄梅县| 兴仁县| 彰化县| 高雄县| 固阳县| 永福县| 儋州市| 盐津县| 文成县| 泸州市| 临夏县| 新建县| 河池市| 南川市| 大关县| 洛南县| 无极县| 朝阳区| 榕江县| 花垣县|