欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Java中怎么定義一個(gè)簡單鏈表

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

創(chuàng)新互聯(lián)專注于信陽企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城建設(shè)。信陽網(wǎng)站建設(shè)公司,為信陽等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站建設(shè),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

一、概述:

1、原理:

只有一個(gè)數(shù)據(jù)項(xiàng)(鏈接點(diǎn)Link),每個(gè)數(shù)據(jù)插入時(shí)都是對第一個(gè)數(shù)據(jù)的引用。

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

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

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

Java中怎么定義一個(gè)簡單鏈表

4、特點(diǎn):先進(jìn)后出

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

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

6、刪除原理

Java中怎么定義一個(gè)簡單鏈表

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

Java中怎么定義一個(gè)簡單鏈表

二、實(shí)現(xiàn):

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

/**
 * @描述     節(jié)點(diǎn)
 * @項(xiàng)目名稱   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、鏈表實(shí)現(xiàn)

/**
 * @描述     鏈表
 * @項(xiàng)目名稱   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é)點(diǎn)往下移動(dòng)
      newNode.next = first;
      //把插入的節(jié)點(diǎn)作為新的節(jié)點(diǎn)
      first = newNode;
    }
  }
  /**
   * 刪除頭節(jié)點(diǎn)
   * @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é)點(diǎn)");
        return null;
      }
      last = current;
      current = current.next;
    }
    if (current == first) {
      //return deleteFirst();
      //指向下個(gè)就表示刪除第一個(gè)
      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é)點(diǎn)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é)點(diǎn)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ù)索引查找對應(yīng)的值
   * @param position
   * @return
   */
  public Node findByPosition(int position){
    Node current = first;
    //為什么是position - 1,因?yàn)橐褂帽闅v,讓current指向下一個(gè), 所以position - 1的下個(gè)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("創(chuàng)新互聯(lián)測試結(jié)果:");
    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();
  }
}

顯示結(jié)果:

Java中怎么定義一個(gè)簡單鏈表

關(guān)于Java中怎么定義一個(gè)簡單鏈表就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

名稱欄目:Java中怎么定義一個(gè)簡單鏈表
當(dāng)前路徑:http://aaarwkj.com/article44/jjpdee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站營銷建站公司、微信小程序微信公眾號、網(wǎng)站內(nèi)鏈

廣告

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

外貿(mào)網(wǎng)站制作
两性色午夜视频在线观看| 国产成人公开免费视频| 伊人久久精品一区二区| 亚洲综合偷拍欧美一区日韩| 精品色欧美色国产一区国产| 精品欧美高清免费视频| 欧美精品亚洲精品国产| 日本中文有码在线观看| 国产传媒在线观看精品| 日韩三级黄片在线观看| 国内精品人妻中文字幕| 在线播放国产91精品| 高清一区二区三区不卡视频| 亚洲精品国产av一区二区三区| 亚洲蜜桃精品一区二区三区| 哪里可以看黄色片日韩| 国产一级二级三级在线电影| 国产精品超碰在线观看| 久久精品免成人费电影| 中文成人无字幕乱码精品| 国产成人免费公开视频| 亚洲欧美日韩1区2区| 日本新任女教师在线播放| 日韩欧美国产一区二区精品 | 日韩av天堂免费网站| 人妻少妇精品一区毛二区| 少妇二区三区精品视频| 欧洲亚洲精品免费二区| 亚洲精品欧美综合二区| 国产成人综合久久三区北岛玲| 亚洲丰满一区二区三区在线| 欧美日韩在线观看不卡视频| 日韩欧美二区三区在线| 欧美国产成人精品一区| 国产一区国产二区中文字幕| 婷婷中文字幕在线不卡视频| 日本精品亚洲一区二区三区| 国产av剧情同事肉体秘密| 欧美中文字幕精在线不卡| 日本黄色av一区二区| 日韩人妻高清精品专区|