数据结构中实现双链表

数据结构中实现双链表
需要实现的方法

package cn.dataStructure2.com;

public interface doubleLinked {

    //头插法
    void addFirst(Object data);

    //尾插法
    void addLast(Object data);

    //任意位置插入,第一个数据节点为0号下标
    boolean addIndex(int index,Object data);

    //查找是否包含关键字key是否在单链表当中
    boolean contains(Object key);

    //删除第一次出现关键字为key的节点
    Object remove(Object key);

    //删除所有值为key的节点
    void removeAllKey(Object key);

    //得到单链表的长度
    int getLength();

    //打印
    void display();

    //清空
    void clear();
}

实现的具体方法过程

package cn.dataStructure2.com;

public class MyDoubleLinkedList implements doubleLinked {
    class Node {
        private Object data;
        private Node next;
        private Node pre;

        public Node(Object data) {
            this.data = data;
        }
    }
    private Node head;
    private Node last;

    public MyDoubleLinkedList() {
        this.head = null;
        this.last = null;
    }

    @Override
    public void addFirst(Object data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.pre = node;
            head = node;
        }
    }

    @Override
    public void addLast(Object data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
        }
        last.next = node;
        node.pre= last;
        last = node;
    }
    private void checkIndex(int index) {
        if (index < 0 || index > getLength()) {
            throw new RuntimeException("下标越界异常:"+index);
        }
    }
    private Node pre(int index) {
        checkIndex(index);
        Node cur = head;
        int n = 0;
        while(n < index) {
            cur = cur.next;
            n++;
        }
        return cur;
    }
    @Override
    public boolean addIndex(int index, Object data) {
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == getLength()) {
            addLast(data);
            return true;
        }
        Node node = new Node(data);
        Node tmp = pre(index);
        node.next = tmp;
        node.pre = tmp.pre;
        node.pre.next = node;
        node.next.pre = node;
        return true;
    }

    @Override
    public boolean contains(Object key) {
        Node p = head;
        while(p != null) {
            if (p.data == key) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    @Override
    public Object remove(Object key) {
        Node cur = this.head;
        while(cur != null) {
            if(cur.data == key) {
                Object oldData = cur.data;
                //要删除的节点是头结点
                if(cur == this.head) {
                    this.head = this.head.next;
                    this.head.pre = null;
                }else {

                    //cur.next != null表示删除的不是
                    //尾节点
                    if(cur.next != null) {
                        cur.pre.next = cur.next;
                        cur.next.pre = cur.pre;
                    }else {
                        //尾节点删除,last需要移动
                        this.last = cur.pre;
                        last.next = null;
                    }
                }
                return oldData;
            }
            cur = cur.next;
        }

        return null;
    }

    @Override
    public void removeAllKey(Object key) {
        Node cur = this.head;
        while(cur != null) {
            if(cur.data == key) {
                //要删除的节点是头结点
                if(cur == this.head) {
                    this.head = this.head.next;
                    this.head.pre = null;
                }else {
                    //cur.next != null表示删除的不是
                    //尾节点
                    if(cur.next != null) {
                        cur.pre.next = cur.next;
                        cur.next.pre = cur.pre;
                    }else {
                        //尾节点删除,last需要移动
                        this.last = cur.pre;
                        last.next = null;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int getLength() {
        Node p = head;
        int count = 0;
        while(p != null) {
            count++;
            p = p.next;
        }
        return count;
    }

    @Override
    public void display() {
        Node p = head;
        while(p != null) {
            System.out.print(p.data+" ");
            p = p.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        while(head.next != null) {
            Node p = head.next;
            head.next = p.next;
            p.pre = null;
        }
        head = null;
    }
}

测试结果

package cn.dataStructure2.com;

public class TestMyDoubleLinkedList {
    public static void main(String[] args) {
        MyDoubleLinkedList list = new MyDoubleLinkedList();
        list.addFirst("1");
        list.addFirst("2");
        list.addFirst("3");
        list.addFirst("4");
        list.addFirst("5");
        list.addLast("6");
        list.addLast("7");
        list.addLast("8");
        list.addFirst("0");
        list.addFirst("0");
        list.addLast("0");
        list.addLast("0");
        list.addIndex(4,"0");
        list.display();
        System.out.println(list.getLength());
//        list.removeAllKey("0");
//        list.display();
        list.remove("0");
        list.display();
        list.remove("0");
        list.display();
        list.remove("0");
        list.display();
//        list.remove("0");
//        list.display();
//        list.clear();
//        list.display();
//        list.addFirst("0");
//        list.display();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值