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

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)站制作
偷拍福利视频一区二区三区| 亚洲女同中文字幕在线| 日韩av在线黄色免费大全| 国产精品久久久久久久亚洲| 日韩欧美一区二区麻豆| 欧美亚洲国产青草久久| 男女裸体做爰一进一出视频| 麻豆午夜福利在线播放| 日本女优高清不卡一二三四区| 一区二区三区在线观看日韩| 少妇人妻系列中文在线| 午夜少妇诱惑一区二区三区| 亚洲黄色成人在线观看| 亚洲精品国产二区中文字幕| 最新国产av网址大全| 午夜少妇诱惑一区二区三区| 久久中文字幕一区二区三区| 福利一区福利二区视频| 亚洲精品午夜在线观看| 国产精品色呦呦一区二区| 日韩欧美国产亚洲在线| 亚洲精品老司机福利在线| 校花出白浆视频一区二区三区| 欧美午夜福利视频观看| av全欧国男人在线天堂| 国产乱码精品一区二区蜜臀| 东京男人的天堂国产av| 亚洲综合欧美日韩一区| 91熟女激情五月综合| 未满十八禁止下载软件| 偷拍视频一区二区三区| 人妻勾引中文字幕在线视频| 熟妇人妻精品一区二区| 国产探花猛操性感美女| 国产91高清免费视频| 国产精品一二三在线看| 久久精品一区二区婷婷| 欧美日韩精品偷拍一区二区| 日韩在线不卡视频一区 | 国产老熟女一区二区三区| 在线欧美日韩一区二区三区|