亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java數(shù)據(jù)結構之簡單鏈表的定義與實現(xiàn)方法示例

 更新時間:2017年10月23日 11:04:16   作者:CharlinGod  
這篇文章主要介紹了Java數(shù)據(jù)結構之簡單鏈表的定義與實現(xiàn)方法,簡單描述了鏈接的概念、原理,并結合實例形式分析了java定義與使用鏈表的相關步驟與操作技巧,需要的朋友可以參考下

本文實例講述了Java數(shù)據(jù)結構之簡單鏈表的定義與實現(xiàn)方法。分享給大家供大家參考,具體如下:

一、概述:

1、原理:

只有一個數(shù)據(jù)項(鏈接點Link),每個數(shù)據(jù)插入時都是對第一個數(shù)據(jù)的引用。

2、插入數(shù)據(jù)說明:

當鏈表沒有數(shù)據(jù)時,插入的值就是第一個數(shù)據(jù),如果鏈表里有數(shù)據(jù),就把當前的數(shù)據(jù)的next指針指向第一個數(shù)據(jù)。

3、插入數(shù)據(jù)圖:

4、特點:先進后出

5、實現(xiàn)功能:

數(shù)據(jù)插入,指定位置插入,顯示,查詢,刪除等

6、刪除原理

7、插入頭節(jié)點原理

二、實現(xiàn):

1、創(chuàng)建節(jié)點

/**
 * @描述     節(jié)點
 * @項目名稱   Java_DataStruct
 * @包名     com.struct.linklist
 * @類名     Node
 * @author   chenlin
 * @date    2010年6月26日 上午7:58:59
 * @version   1.0 
 */
public class Node {
  public long data;
  public Node next;
  public long getData() {
    return data;
  }
  public void display(){
    System.out.print(data + " ");
  }
  public Node(long data) {
    this.data = data;
  }
  public void setData(long data) {
    this.data = data;
  }
  public Node getNext() {
    return next;
  }
  public void setNext(Node next) {
    this.next = next;
  }
}

2、鏈表實現(xiàn)

/**
 * @描述     鏈表
 * @項目名稱   Java_DataStruct
 * @包名     com.struct.linklist
 * @類名     LinkList
 * @author   chenlin
 * @date    2010年6月26日 上午8:00:28
 * @version   1.0 
 */
public class LinkList {
  private Node first;
  public LinkList(){
    first = null;
  }
  /**
   * 插入數(shù)據(jù)
   * @param value
   */
  public void insertFirst(long value){
    Node newNode = new Node(value);
    if (first == null) {
      first = newNode;
    }else {
      //把first節(jié)點往下移動
      newNode.next = first;
      //把插入的節(jié)點作為新的節(jié)點
      first = newNode;
    }
  }
  /**
   * 刪除頭節(jié)點
   * @param value
   * @return
   */
  public Node deleteFirst(){
    if (first == null) {
      throw new RuntimeException("鏈表數(shù)據(jù)不存在");
    }
    Node temp = first;
    first = temp.next;
    return temp;
  }
  public Node deleteByKey(long key){
    Node current = first;
    Node last = first;
    while(current.data != key){
      if (current.next == null) {
        System.out.println("沒找到節(jié)點");
        return null;
      }
      last = current;
      current = current.next;
    }
    if (current == first) {
      //return deleteFirst();
      //指向下個就表示刪除第一個
      first = first.next;
    }else {
      last.next = current.next;
    }
    return current;
  }
  /**
   * 顯示所有的數(shù)據(jù)
   */
  public void display(){
    if (first == null) {
      //throw new RuntimeException("鏈表數(shù)據(jù)不存在");
      return;
    }
    Node current = first;
    while(current != null){
      current.display();
      current = current.next;
    }
    System.out.println("---------------");
  }
  /**
   * 查找節(jié)點1
   * @param value
   * @return
   */
  public Node findByValue(long value){
    Node current = first;
    while(current != null){
      if (current.data != value) {
        current = current.next;
      }else {
        break;
      }
    }
    if (current == null) {
      System.out.println("沒找到");
      return null;
    }
    return current;
  }
  /**
   * 查找節(jié)點2
   * 
   * @param key
   * @return
   */
  public Node findByKey(long key) {
    Node current = first;
    while (current.data != key) {
      if (current.next == null) {
        System.out.println("沒找到");
        return null;
      }
      current = current.next;
    }
    return current;
  }
  /**
   * 根據(jù)索引查找對應的值
   * @param position
   * @return
   */
  public Node findByPosition(int position){
    Node current = first;
    //為什么是position - 1,因為要使用遍歷,讓current指向下一個, 所以position - 1的下個node就是要找的值
    for (int i = 0; i < position - 1 ; i++) {
      current = current.next;
    }
    return current;
  }
  public static void main(String[] args) {
    LinkList linkList = new LinkList();
    linkList.insertFirst(21);
    linkList.insertFirst(22);
    linkList.insertFirst(23);
    linkList.insertFirst(24);
    linkList.insertFirst(25);
    linkList.insertFirst(26);
    linkList.insertFirst(27);
    System.out.println("腳本之家測試結果:");
    linkList.display();
    System.out.println("---查找-------------------------------------");
    linkList.findByKey(25).display();
    System.out.println("--刪除first-------------------------------------");
    //linkList.deleteFirst().display();
    ///linkList.deleteFirst().display();
    //linkList.deleteFirst().display();
    //linkList.deleteFirst().display();
    System.out.println("-刪除指定值---------------------------------------");
    linkList.deleteByKey(27).display();
    linkList.deleteByKey(21).display();
    System.out.println("----------------------------------------");
    linkList.display();
  }
}

顯示結果:

更多關于java算法相關內容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設計有所幫助。

相關文章

  • IntelliJ IDEA中Project與Module的概念以及區(qū)別

    IntelliJ IDEA中Project與Module的概念以及區(qū)別

    這篇文章主要給大家介紹了關于IntelliJ IDEA中Project與Module的概念以及區(qū)別的相關資料,文中通過實例介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 詳解JVM 中的StringTable

    詳解JVM 中的StringTable

    這篇文章主要介紹了JVM 中的StringTable,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 一篇文章帶你入門java變量與類型

    一篇文章帶你入門java變量與類型

    這篇文章主要給大家介紹了關于Java基本知識點之變量和數(shù)據(jù)類型的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-08-08
  • 詳解SpringBoot 處理異常的幾種常見姿勢

    詳解SpringBoot 處理異常的幾種常見姿勢

    這篇文章主要介紹了詳解SpringBoot 處理異常的幾種常見姿勢,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Java中Flux類的使用方法和示例代碼

    Java中Flux類的使用方法和示例代碼

    在Java編程中Flux是一種處理響應式編程的庫,它提供了一種異步數(shù)據(jù)流處理的方式,這篇文章主要給大家介紹了關于Java中Flux類的使用方法和示例代碼,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • Java如何將二維數(shù)組轉化為一維數(shù)組

    Java如何將二維數(shù)組轉化為一維數(shù)組

    這篇文章主要介紹了Java如何將二維數(shù)組轉化為一維數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Spring4下validation數(shù)據(jù)校驗無效(maven)的解決

    Spring4下validation數(shù)據(jù)校驗無效(maven)的解決

    這篇文章主要介紹了Spring4下validation數(shù)據(jù)校驗無效(maven)的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java虛擬機JVM性能優(yōu)化(三):垃圾收集詳解

    Java虛擬機JVM性能優(yōu)化(三):垃圾收集詳解

    這篇文章主要介紹了Java虛擬機JVM性能優(yōu)化(三):垃圾收集詳解,本文講解了眾多的JVM垃圾收集器知識點,需要的朋友可以參考下
    2014-09-09
  • Java中的Lambda表達式詳解

    Java中的Lambda表達式詳解

    這篇文章主要介紹了Java中的Lambda表達式詳解,Lambda 表達式是 JDK8 的一個新特性,可以取代大部分的匿名內部類,寫出更優(yōu)雅的 Java 代碼,尤其在集合的遍歷和其他集合操作中,可以極大地優(yōu)化代碼結構,需要的朋友可以參考下
    2024-01-01
  • 如何基于回調實現(xiàn)Java的異步調用

    如何基于回調實現(xiàn)Java的異步調用

    這篇文章主要介紹了如何基于回調實現(xiàn)Java的異步調用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論