JAVA實(shí)現(xiàn)雙向鏈表的增刪功能的方法
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ù)說(shuō)明:
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í)有所幫助,也希望大家多多支持腳本之家。
- java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例
- Java中雙向鏈表詳解及實(shí)例
- Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)
- java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解
- Java語(yǔ)言中鏈表和雙向鏈表
- java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享
- java實(shí)現(xiàn)單鏈表、雙向鏈表
- Java雙向鏈表按照順序添加節(jié)點(diǎn)的方法實(shí)例
- java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單鏈表與雙向鏈表
- 基于Java實(shí)現(xiàn)雙向鏈表
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)排序算法之樹(shù)形選擇排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)排序算法之樹(shù)形選擇排序,結(jié)合具體實(shí)例形式分析了java樹(shù)形選擇排序的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
Mybatis-Plus根據(jù)時(shí)間段去查詢(xún)數(shù)據(jù)的實(shí)現(xiàn)示例
這篇文章主要介紹了Mybatis-Plus根據(jù)時(shí)間段去查詢(xún)數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
基于SpringBoot生成二維碼的幾種實(shí)現(xiàn)方式
本文將基于Spring Boot介紹兩種生成二維碼的實(shí)現(xiàn)方式,一種是基于Google開(kāi)發(fā)工具包,另一種是基于Hutool來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03

