双向链表
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-718rijvz-1612705516002)(C:\Users\17745\AppData\Roaming\Typora\typora-user-images\image-20210207212109036.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GN0uunrp-1612705516004)(C:\Users\17745\AppData\Roaming\Typora\typora-user-images\image-20210207212038805.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cQoCqspX-1612705516008)(C:\Users\17745\AppData\Roaming\Typora\typora-user-images\image-20210207212152150.png)]
代码实现:
package JavaSeStudy;
/**双向链表
* @author 野望编程小菜鸟
* @computer smartisan
* @create 2021-02-07 19:45
*/
public class DoubleSingleLink {
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,"林冲","豹子头");
//创建双向链表
DoubleSingleList list=new DoubleSingleList();
list.add(hero1);
list.add(hero4);
list.add(hero2);
list.add(hero3);
list.List();
HeroNode2 nh=new HeroNode2(4,"林冲","豹子虎头");
list.update(nh);
System.out.println("修改后的链表情况---");
list.List();
list.del(1);
System.out.println("删除后的链表情况---");
list.List();
}
}
class DoubleSingleList{
//初始化一个节点,头结点不能动。不存放具体的数据和思路
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
//显示链表(输出链表)
public void List(){
//p判断链表是否为空
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 add(HeroNode2 heroNode){
HeroNode2 temp=head;
//遍历
while (true){
//找到链表的尾巴
if (temp.next==null){
break;
}
//如果没有找到最后,将temp后移
temp =temp.next;
}
//当退出while循环时候,temp指向链表的最后
//形成一个双向链表
temp.next=heroNode;
heroNode.pre=temp;
}
public void update(HeroNode2 nh) {
//判断是否为空
if (head.next == null) {
System.out.println("链表为空");
return;
}
//找到需要修改的节点,根据no编号
//定义一个辅助变量
HeroNode2 temp = head.next;
boolean flag = false;//表示是否找到该节点
while (true) {
if (temp == null) {
break;
}
if (temp.no == nh.no) {//表示找到
flag = true;
break;
}
temp = temp.next;
}
//根据flag判断是否找到要修改的节点
if (flag) {
temp.name = nh.name;
temp.nickname = nh.nickname;
} else {
System.out.println("没有找到该节点" + nh.no);
}
}
public void del(int no) {
if (head.next==null){
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;//标志是否能找到删除的节点
while (true) {
if (temp== null) {
break;
}
if (temp.no == no) {
flag = true;
break;
}
temp = temp.next;//将temp后移遍历
}
if (flag ) {
temp.pre.next = temp.next;
if (temp.next!=null){
temp.next.pre=temp.pre;
}
} else {
System.out.println("要删除的结点不存在" + no);
}
}
}
//双向链表节点
class HeroNode2{
public int no;
public String name;
public String nickname;
public HeroNode2 next;//指向下一个节点
public HeroNode2 pre;//指向上一个节点
//构造器
public HeroNode2(int n,String name,String nickname){
this.no=n;
this.name=name;
this.nickname=nickname;
}
@Override
public String toString() {
return "HeroNode2{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
e2{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}