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

JAVA實(shí)現(xiàn)雙向鏈表的增刪功能的方法

 更新時(shí)間:2018年03月26日 10:10:58   作者:song.yan  
本篇文章主要介紹了JAVA實(shí)現(xiàn)雙向鏈表的增刪功能的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

JAVA實(shí)現(xiàn)雙向鏈表的增刪功能,完整代碼

package linked;
class LinkedTable{  
}
public class LinkedTableTest {
   //構(gòu)造單鏈表
  static Node node1 = new Node("name1");
  static Node node2 = new Node("name2");
  static Node node3 = new Node("name3");
  static Node node4 = new Node("name4");
  static Node node5 = new Node("name5");
  public static void main(String[] args)
  {
    //設(shè)置指針
    setPoint();
    
    //循環(huán)遍歷
    System.out.println("*******初始鏈表*******");
    out(node1,node5);
    System.out.println();
    
    //插入節(jié)點(diǎn)在node2的后面
    addNode(node2,node3);
    
    // 循環(huán)遍歷
    System.out.println("*******插入node2.5*******");
    out(node1, node5);
    System.out.println();
        
    //刪除節(jié)點(diǎn)
    node2.setNextNode(node3);
    node3.setNextNodeF(node2);
    
    // 循環(huán)遍歷
    System.out.println("*******刪除node2.5*******");
    out(node1, node5);
    System.out.println();
    
  }
  
  //設(shè)置指針
  public static void setPoint()
  {
    //設(shè)置正向指針
    node1.setNextNode(node2);
    node2.setNextNode(node3);
    node3.setNextNode(node4);
    node4.setNextNode(node5);
    //設(shè)置反向指針
    node5.setNextNodeF(node4);
    node4.setNextNodeF(node3);
    node3.setNextNodeF(node2);
    node2.setNextNodeF(node1);
  }
  
  //循環(huán)遍歷單鏈表
  public static void outLinked(Node startNode){
    Node node= new Node();
    node.setNextNode(startNode);
    do
    {
      node=node.getNextNode();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNode()!=null);
  }
  
  //反向循環(huán)遍歷單鏈表
  public static void outLinkedF(Node endNode){
    Node node= new Node();
    node.setNextNodeF(endNode);
    do
    {
      node=node.getNextNodeF();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNodeF()!=null);
  }
  
  //循環(huán)遍歷
  public static void out(Node startNode,Node endNode)
  {
    outLinked(startNode);
    System.out.println();
    outLinkedF(endNode);    
  }
  
  //插入節(jié)點(diǎn)
  public static void addNode(Node preNode,Node nextNode)
  {
    Node node_add = new Node("name2.5");
    node_add.setNextNode(preNode.getNextNode());
    preNode.setNextNode(node_add);    
    node_add.setNextNodeF(nextNode.getNextNodeF());
    nextNode.setNextNodeF(node_add);
  }  
}

class Node {
  private String name;
  private Node nextNode;
  private Node nextNodeF;
  public void setName(String name)
  {
    this.name=name;
  }
  public void setNextNode(Node nextNode)
  {
    this.nextNode=nextNode;
  }
  public void setNextNodeF(Node nextNodeF)
  {
    this.nextNodeF=nextNodeF;
  }
  public String getName()
  {
    return this.name;
  }
  public Node getNextNode()
  {
    return this.nextNode;
  }
  public Node getNextNodeF()
  {
    return this.nextNodeF;
  }
  public Node(String name)
  {
    this.name=name;
    this.nextNode=null;
  }
  public Node( )
  {    
  }  
}

1,構(gòu)造node節(jié)點(diǎn),需要兩個(gè)指針,一個(gè)正向存儲(chǔ)下一個(gè)元素的位置,一個(gè)反向存儲(chǔ)下一個(gè)元素的位置

參數(shù)說明:

  name:用于存儲(chǔ)node自身的信息

  nextNode:用于存儲(chǔ)正向指針

  nextNodeF:用于存儲(chǔ)反向指針

class Node {
  private String name;
  private Node nextNode;
  private Node nextNodeF;
  public void setName(String name)
  {
    this.name=name;
  }
  public void setNextNode(Node nextNode)
  {
    this.nextNode=nextNode;
  }
  public void setNextNodeF(Node nextNodeF)
  {
    this.nextNodeF=nextNodeF;
  }
  public String getName()
  {
    return this.name;
  }
  public Node getNextNode()
  {
    return this.nextNode;
  }
  public Node getNextNodeF()
  {
    return this.nextNodeF;
  }
  public Node(String name)
  {
    this.name=name;
    this.nextNode=null;
  }
  public Node( )
  {    
  }  
}

2,創(chuàng)建節(jié)點(diǎn),設(shè)置指針連接節(jié)點(diǎn)

正向指針:指向下一個(gè)節(jié)點(diǎn)

反向節(jié)點(diǎn):指向上一個(gè)節(jié)點(diǎn)

//構(gòu)造單鏈表
  static Node node1 = new Node("name1");
  static Node node2 = new Node("name2");
  static Node node3 = new Node("name3");
  static Node node4 = new Node("name4");
  static Node node5 = new Node("name5");
public static void setPoint()
  {
    //設(shè)置正向指針
    node1.setNextNode(node2);
    node2.setNextNode(node3);
    node3.setNextNode(node4);
    node4.setNextNode(node5);
    //設(shè)置反向指針
    node5.setNextNodeF(node4);
    node4.setNextNodeF(node3);
    node3.setNextNodeF(node2);
    node2.setNextNodeF(node1);
  }

3,將鏈表循環(huán)遍歷輸出

public static void outLinked(Node startNode){
    Node node= new Node();
    node.setNextNode(startNode);
    do
    {
      node=node.getNextNode();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNode()!=null);
  }

  public static void outLinkedF(Node endNode){
    Node node= new Node();
    node.setNextNodeF(endNode);
    do
    {
      node=node.getNextNodeF();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNodeF()!=null);
  }

4,添加節(jié)點(diǎn)

  public static void addNode(Node preNode,Node nextNode)
  {
    Node node_add = new Node("name2.5");
    node_add.setNextNode(preNode.getNextNode());
    preNode.setNextNode(node_add);
    
    node_add.setNextNodeF(nextNode.getNextNodeF());
    nextNode.setNextNodeF(node_add);
  }

5,刪除節(jié)點(diǎn)

node2.setNextNode(node3);
node3.setNextNodeF(node2);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java壓縮文件工具類ZipUtil使用方法代碼示例

    Java壓縮文件工具類ZipUtil使用方法代碼示例

    這篇文章主要介紹了Java壓縮文件工具類ZipUtil使用方法代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫分表方式

    SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫分表方式

    這篇文章主要介紹了SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫分表方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot啟動(dòng)失敗的原因及其解決方法

    SpringBoot啟動(dòng)失敗的原因及其解決方法

    對(duì)于springboot的啟動(dòng)失敗,相信大家都有經(jīng)歷,但是為什么會(huì)啟動(dòng)失敗,以及怎么解決都只能通過日志進(jìn)行查看,在這里,我會(huì)將常見的springboot啟動(dòng)失敗的報(bào)錯(cuò)一一展示,需要的朋友可以參考下
    2024-06-06
  • Java編程發(fā)展歷史(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)

    Java編程發(fā)展歷史(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)

    Java的歷史可以追溯到1991年4月,Sun公司的James Gosling領(lǐng)導(dǎo)的綠色計(jì)劃(Green Project)開始著力發(fā)展一種分布式系統(tǒng)結(jié)構(gòu),使其能夠在各種消費(fèi)性電子產(chǎn)品上運(yùn)行,他們使用了C/C++/Oak語言。由于多種原因,綠色計(jì)劃逐漸陷于停滯狀態(tài)
    2017-03-03
  • Java?C++題解leetcode1441用棧操作構(gòu)建數(shù)組示例

    Java?C++題解leetcode1441用棧操作構(gòu)建數(shù)組示例

    這篇文章主要為大家介紹了Java?C++題解leetcode1441用棧操作構(gòu)建數(shù)組示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • idea關(guān)聯(lián)maven的使用詳解

    idea關(guān)聯(lián)maven的使用詳解

    這篇文章主要介紹了idea關(guān)聯(lián)maven的使用詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Spring?Initializr只能創(chuàng)建為Java?17版本以上的問題解決

    Spring?Initializr只能創(chuàng)建為Java?17版本以上的問題解決

    這篇文章主要給大家介紹了關(guān)于Spring?Initializr只能創(chuàng)建為Java?17版本以上問題的解決辦法,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • 使用Assembly打包和部署SpringBoot工程方式

    使用Assembly打包和部署SpringBoot工程方式

    文章介紹了SpringBoot項(xiàng)目的兩種部署方式:Docker容器部署和FatJar直接部署,FatJar部署存在配置文件隱藏和啟動(dòng)腳本復(fù)雜的問題,而Assembly打包方式可以解決這些問題,使得SpringBoot能夠加載jar外的配置文件并提供服務(wù)化的啟動(dòng)腳本
    2024-12-12
  • java進(jìn)行遠(yuǎn)程部署與調(diào)試及原理詳解

    java進(jìn)行遠(yuǎn)程部署與調(diào)試及原理詳解

    這篇文章主要介紹了java進(jìn)行遠(yuǎn)程部署與調(diào)試及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 使用Java?Socket實(shí)現(xiàn)GPS定位數(shù)據(jù)處理

    使用Java?Socket實(shí)現(xiàn)GPS定位數(shù)據(jù)處理

    在許多應(yīng)用場(chǎng)景中,如車輛追蹤、移動(dòng)設(shè)備定位等,GPS定位數(shù)據(jù)的實(shí)時(shí)獲取和處理至關(guān)重要,本文將介紹如何使用Java?Socket編程來接收GPS設(shè)備發(fā)送的數(shù)據(jù)并進(jìn)行處理,需要的朋友可以參考下
    2024-07-07

最新評(píng)論

瑞丽市| 苍南县| 舟山市| 浠水县| 宁强县| 准格尔旗| 修文县| 徐水县| 探索| 霍林郭勒市| 沧州市| 漳州市| 延津县| 临沭县| 安丘市| 福清市| 永泰县| 陇南市| 株洲市| 松溪县| 聂拉木县| 兴安县| 文昌市| 准格尔旗| 金沙县| 防城港市| 伊春市| 沈阳市| 武鸣县| 将乐县| 汉源县| 太和县| 凤翔县| 元阳县| 宁安市| 修武县| 西青区| 锡林郭勒盟| 车致| 长葛市| 吉首市|