双向链表
作者:互联网
java数据结构与算法学习记录----双向链表
双向查找(向前,向后)
可自我删除(单链表删除总是找前一个结点)
双向链表增删查改
package linkedlist;
/**
* 双向链表
* @author jk
*
*/
public class DoubleLinkedList {
public static void main(String[] args) {
HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
DoubleLinkedListDome doubleLinkedListDome=new DoubleLinkedListDome();
// doubleLinkedListDome.add(hero1);
// doubleLinkedListDome.add(hero4);
// doubleLinkedListDome.add(hero3);
// doubleLinkedListDome.add(hero2);
// doubleLinkedListDome.display_list();
doubleLinkedListDome.addByOrder(hero1);
doubleLinkedListDome.addByOrder(hero4);
doubleLinkedListDome.addByOrder(hero3);
doubleLinkedListDome.addByOrder(hero2);
doubleLinkedListDome.display_list();
HeroNode2 heroNode2=new HeroNode2(4, "Vecter","猜不透" );
doubleLinkedListDome.Update(heroNode2);
System.out.println("更新后链表");
doubleLinkedListDome.display_list();
doubleLinkedListDome.del(1);
System.out.println("删除后链表");
doubleLinkedListDome.display_list();
}
}
class DoubleLinkedListDome{
private HeroNode2 head =new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
//显示链表(遍历)
public void display_list() {
if (head.next==null) {
System.out.println("链表为空");
return;
}
HeroNode2 temp=head.next;
while(true) {
if(temp==null) {
break;
}
System.out.println(temp);
temp=temp.next;
}
}
//按排名添加
public void addByOrder(HeroNode2 heroNode2) {
HeroNode2 temp=head;
boolean flag=false;
while(true) {
if (temp.next==null) {
break;
}
if(temp.next.on>heroNode2.on) {
break;
}else if(temp.next.on==heroNode2.on){
flag=true;
break;
}
temp=temp.next;
}
if(flag) {
System.out.printf("编号%d节点已存在",heroNode2.on);
}else {
heroNode2.next=temp.next;
temp.next=heroNode2;
heroNode2.pre=temp;
}
}
//添加结点到链表最后
public void add(HeroNode2 heroNode2) {
HeroNode2 temp=head;
while(true) {
if(temp.next==null) {
break;
}
temp=temp.next;
}
temp.next=heroNode2;
heroNode2.pre=temp;
}
//修改节点内容
public void Update(HeroNode2 newHeroNode2) {
if(head.next==null) {
System.out.println("链表无数据");
return;
}
HeroNode2 temp=head.next;
boolean flag=false;
while(true) {
if(temp==null) {
break;
}
if(temp.on==newHeroNode2.on) {
flag=true;
break;
}
temp=temp.next;
}
if (flag) {
temp.name=newHeroNode2.name;
temp.nickname=newHeroNode2.nickname;
}else {
System.out.printf("无编号为%d节点\n",newHeroNode2.on);
}
}
//删除节点
public void del(int on) {
if (head.next==null) {
System.out.println("链表无数据");
return;
}
HeroNode2 temp=head.next;
boolean flag=false;
while(true) {
if (temp==null) {
break;
}
if(temp.on==on) {
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.printf("要删除的%d节点不存在\n",on);
}
}
}
class HeroNode2{
public int on;
public String name;
public String nickname;
public HeroNode2 next;
public HeroNode2 pre;
public HeroNode2(int on,String name,String nickname) {
this.name=name;
this.on=on;
this.nickname=nickname;
}
@Override
public String toString() {
return "HeroNode [on=" + on + ", name=" + name + ", nickname=" + nickname + "]";
}
}
标签:temp,双向,next,链表,public,HeroNode2,heroNode2,doubleLinkedListDome 来源: https://blog.csdn.net/weixin_44565065/article/details/114555012