双链表
一、添加
package com
.love
.xu
.doubleLinkedList
;
public class DoubleLinkedList {
private LinkNode head
= new LinkNode(0,"","");
public LinkNode
getHead(){
return head
;
}
public void add(LinkNode linkNode
){
LinkNode temp
= head
;
while (true){
if(temp
.next
== null
){
break;
}
temp
= temp
.next
;
}
temp
.next
= linkNode
;
linkNode
.pre
= temp
;
}
public void update(LinkNode linkNode
){
if(head
.next
== null
){
System
.out
.println("链表为空");
return;
}
LinkNode temp
= head
.next
;
boolean flag
= false;
while (true){
if(temp
== null
){
break;
}
if(temp
.no
== linkNode
.no
){
flag
= true;
break;
}
temp
= temp
.next
;
}
if(flag
){
temp
.name
= linkNode
.name
;
temp
.nickname
= linkNode
.nickname
;
}else {
System
.out
.printf("没有找到 编号 %d 的节点,不能修改",linkNode
.no
);
}
}
public void del(int no
){
if (head
.next
== null
){
System
.out
.println("链表为空,无法删除");
return;
}
LinkNode temp
= head
;
boolean flag
= false;
while (true){
if(temp
.no
== no
){
flag
= true;
break;
}
temp
= temp
.next
;
}
if(flag
){
temp
.pre
.next
= temp
.next
;
if(temp
.next
!= null
){
temp
.next
.pre
= temp
.pre
;
}
}else {
System
.out
.println("找不到"+no
+"节点");
}
}
public void list(){
if(head
.next
== null
){
System
.out
.println("链表为空");
return;
}
LinkNode temp
= head
.next
;
while (true){
if(temp
== null
){
break;
}
System
.out
.println(temp
);
temp
= temp
.next
;
}
}
}
class LinkNode {
int no
;
String name
;
String nickname
;
LinkNode next
;
LinkNode pre
;
public LinkNode(int no
, String name
, String nickname
) {
this.no
= no
;
this.name
= name
;
this.nickname
= nickname
;
}
@Override
public String
toString() {
return "LinkNode{" +
"no=" + no
+
", name='" + name
+ '\'' +
", nickname='" + nickname
+ '\'' +
'}';
}
}
转载请注明原文地址:https://tech.qufami.com/read-19339.html