JAVA實(shí)現(xiàn)雙向鏈表的增刪功能,完整代碼
在巧家等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣,外貿(mào)網(wǎng)站制作,巧家網(wǎng)站建設(shè)費(fèi)用合理。
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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章標(biāo)題:JAVA實(shí)現(xiàn)雙向鏈表的增刪功能的方法
文章出自:http://chinadenli.net/article28/jhjhcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、軟件開(kāi)發(fā)、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)