分析双向链表的遍历,添加,修改,删除的操作思路===》代码实现
1)遍历方和单链表-样,只是可以向前,也可以向后查找
2)添加(默认添加到双向链表的最后)
(1)先找到双向链表的最后这个节点
(2) temp.next =newHeroNode
(3) newHeroNode.pre =temp;
3)修改思路和原来的单向链表一样.
4)删除
(1)因为是双向链表,因此,我们可以实现自我删除某个节点
(2)直接找到要删除的这个节点,比如temp
(3) temp.pre.next = temp.next
(4) temp.next.pre=temp.pre;
package com.main.dataStructures.LinkedList;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 hero1 = new HeroNode2(1,"wp1","pp");
HeroNode2 hero2 = new HeroNode2(2,"lmx2","dg");
HeroNode2 hero3 = new HeroNode2(3,"wp3","ss");
HeroNode2 hero4 = new HeroNode2(4,"wp4","qq");
HeroNode2 hero5 = new HeroNode2(5,"wp5","ww");
HeroNode2 hero6 = new HeroNode2(6,"lmx6","asb");
HeroNode2 hero7 = new HeroNode2(6,"lmxcbb","a23");
DoubleLinkedList list = new DoubleLinkedList();
// list.addNode(hero5);
// list.addNode(hero1);
// list.addNode(hero6);
// list.addNode(hero2);
// list.addNode(hero4);
// list.addNode(hero3);
// list.showNode();
// System.out.println("==================");
// list.updateNode(hero7);
// list.showNode();
// System.out.println("==================");
// list.deleteNode(3);
// list.showNode();
list.addNode2(hero5);
list.addNode2(hero1);
list.addNode2(hero6);
list.addNode2(hero2);
list.addNode2(hero4);
list.addNode2(hero3);
list.showNode();
}
}
class DoubleLinkedList{
//初始化
private HeroNode2 head = new HeroNode2(0,"","");
public HeroNode2 getHead() {
return head;
}
public void showNode(){
if (head.next == null){
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
while (temp!=null){
System.out.println(temp);
temp = temp.next;
}
}
public void addNode(HeroNode2 heroNode2){
HeroNode2 temp = head;
while (true){
if (temp.next==null){
break;
}
temp = temp.next;
}
temp.next = heroNode2;
heroNode2.pre = temp;
}
public void addNode2(HeroNode2 node){
HeroNode2 temp = head;
boolean flag = false;
while (true){
//判满:temp已经是链表最后一个元素了,无法添加
if (temp.next == null){
break;
}
if (temp.next.no>node.no){
break;
}else if (temp.next.no==node.no){
flag = true;
break;
}
//后移
temp = temp.next;
}
//添加
if (flag){
System.out.println("编号已存在");
}else {
node.next = temp.next;
temp.next = node;
node.pre = temp;
}
}
public void updateNode(HeroNode2 newNode){
if (head.next == null){
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while (true){
if (temp == null){
break;//已经遍历到最后了
}
if (temp.no == newNode.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.name = newNode.name;
temp.nickName = newNode.nickName;
}else {
System.out.println("没有该用户");
}
}
public void deleteNode(int no){
if (head.next == null){
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
while (true){
if (temp == null){
break;//已经遍历到最后了
}
if (temp.no == no){
temp.pre.next = temp.next;
if (temp.next!=null){
temp.next.pre = temp.pre;
}
break;
}
temp = temp.next;//后移
}
}
}
class HeroNode2{
int no;
String name;
String nickName;
HeroNode2 next;//指向下一个节点
HeroNode2 pre;//指向前一个节点
public HeroNode2(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '}';
}
}