99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

Java中怎么定義一個簡單鏈表

這篇文章給大家介紹Java中怎么定義一個簡單鏈表,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創新互聯專注于信陽企業網站建設,響應式網站,商城建設。信陽網站建設公司,為信陽等地區提供建站服務。全流程定制網站建設,專業設計,全程項目跟蹤,創新互聯專業和態度為您提供的服務

一、概述:

1、原理:

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

2、插入數據說明:

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

3、插入數據圖:

Java中怎么定義一個簡單鏈表

4、特點:先進后出

5、實現功能:

數據插入,指定位置插入,顯示,查詢,刪除等

6、刪除原理

Java中怎么定義一個簡單鏈表

7、插入頭節點原理

Java中怎么定義一個簡單鏈表

二、實現:

1、創建節點

/**
 * @描述     節點
 * @項目名稱   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、鏈表實現

/**
 * @描述     鏈表
 * @項目名稱   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;
  }
  /**
   * 插入數據
   * @param value
   */
  public void insertFirst(long value){
    Node newNode = new Node(value);
    if (first == null) {
      first = newNode;
    }else {
      //把first節點往下移動
      newNode.next = first;
      //把插入的節點作為新的節點
      first = newNode;
    }
  }
  /**
   * 刪除頭節點
   * @param value
   * @return
   */
  public Node deleteFirst(){
    if (first == null) {
      throw new RuntimeException("鏈表數據不存在");
    }
    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("沒找到節點");
        return null;
      }
      last = current;
      current = current.next;
    }
    if (current == first) {
      //return deleteFirst();
      //指向下個就表示刪除第一個
      first = first.next;
    }else {
      last.next = current.next;
    }
    return current;
  }
  /**
   * 顯示所有的數據
   */
  public void display(){
    if (first == null) {
      //throw new RuntimeException("鏈表數據不存在");
      return;
    }
    Node current = first;
    while(current != null){
      current.display();
      current = current.next;
    }
    System.out.println("---------------");
  }
  /**
   * 查找節點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;
  }
  /**
   * 查找節點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;
  }
  /**
   * 根據索引查找對應的值
   * @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中怎么定義一個簡單鏈表就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

名稱欄目:Java中怎么定義一個簡單鏈表
當前路徑:http://www.yijiale78.com/article44/jjpdee.html

成都網站建設公司_創新互聯,為您提供外貿建站網站營銷建站公司微信小程序微信公眾號網站內鏈

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作