数据结构与算法-1.4双向链表的增删改查操作

1.结点

package com.example;

import java.util.Objects;

public class DoubleNode {

    private Integer id;
    private String name;
    private DoubleNode preNode;
    private DoubleNode nextNode;

    public DoubleNode() {
    }

    public DoubleNode(Integer id, String name, DoubleNode preNode, DoubleNode nextNode) {
        this.id = id;
        this.name = name;
        this.preNode = preNode;
        this.nextNode = nextNode;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DoubleNode that = (DoubleNode) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(name, that.name) &&
                Objects.equals(preNode, that.preNode) &&
                Objects.equals(nextNode, that.nextNode);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, name, preNode, nextNode);
    }

    @Override
    public String toString() {
        return "DoubleNode{" +
                "id=" + id +
                ", name='" + name +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public DoubleNode getPreNode() {
        return preNode;
    }

    public void setPreNode(DoubleNode preNode) {
        this.preNode = preNode;
    }

    public DoubleNode getNextNode() {
        return nextNode;
    }

    public void setNextNode(DoubleNode nextNode) {
        this.nextNode = nextNode;
    }
}

2.链表

package com.example;

public class DoubleLinkList {

    private static DoubleNode headNode = new DoubleNode(1,"头结点",null,null);

    public void list(){
        if (headNode.getNextNode()==null){
            System.out.println("链表为空");
            return;
        }
        DoubleNode flag = headNode;
        while (true){
            if (flag.getNextNode()!=null){
                System.out.println(flag.getNextNode());
                flag = flag.getNextNode();
            }else{
                break;
            }
        }
    }


    public String addNodeToEnd(DoubleNode doubleNode){
        DoubleNode flag = headNode;
        while (true){
            if(flag.getNextNode()== null){
                flag.setNextNode(doubleNode);
                doubleNode.setPreNode(flag);
                break;
            }
            flag = flag.getNextNode();
        }
        return "添加成功";
    }

    public String deleteNode(DoubleNode doubleNode){
        DoubleNode flag = headNode;
        while (true){
            if (flag.getId() == doubleNode.getId()){
                if (flag.getNextNode()==null){
                    flag.getPreNode().setNextNode(null);
                    flag.setPreNode(null);
                    break;
                }else{
                    flag.getPreNode().setNextNode(flag.getNextNode());
                    flag.getNextNode().setPreNode(flag.getPreNode());
                    break;
                }
            }
            if (flag.getNextNode()==null){
                return "删除失败,没有数据";
            }
            flag = flag.getNextNode();
        }
        return "删除成功";
    }
}

3.测试类

package com.example;


public class Test {

    public static void main(String[] args){
        DoubleNode doubleNode1 = new DoubleNode(2,"张三",null,null);
        DoubleNode doubleNode2 = new DoubleNode(3,"李四",null,null);
        DoubleNode doubleNode3 = new DoubleNode(4,"王五",null,null);
        DoubleNode doubleNode4 = new DoubleNode(5,"赵六",null,null);
        DoubleNode doubleNode5 = new DoubleNode(6,"冯七",null,null);
        DoubleNode doubleNode7 = new DoubleNode(7,"冯8",null,null);

        DoubleLinkList doubleLinkList = new DoubleLinkList();

        doubleLinkList.addNodeToEnd(doubleNode1);
        doubleLinkList.addNodeToEnd(doubleNode2);
        doubleLinkList.addNodeToEnd(doubleNode3);
        doubleLinkList.addNodeToEnd(doubleNode4);
        doubleLinkList.addNodeToEnd(doubleNode5);
        doubleLinkList.list();
        System.out.println("------------------------------------");
        String s = doubleLinkList.deleteNode(doubleNode5);
        System.out.println(s);
        doubleLinkList.list();
        System.out.println("------------------------------------");
        String s1 = doubleLinkList.deleteNode(doubleNode2);
        System.out.println(s1);
        doubleLinkList.list();
        System.out.println("------------------------------------");
        String s2 = doubleLinkList.deleteNode(doubleNode7);
        System.out.println(s2);
        doubleLinkList.list();

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值