【数据结构】链表的实现 之 不带头双向链表的实现

接口方法:

public interface IDoubleLinked {
    //头插法
    void addFirst(int data);
    //尾插法
    void addLast(int data);
    //任意位置插入,第一个数据节点为0号下标
    boolean addIndex(int index,int data);
    //查找是否包含关键字key是否在单链表当中
    boolean contains(int key);
    //删除第一次出现关键字为key的节点
    int remove(int key);
    //删除所有值为key的节点
    void removeAllKey(int key);
    //得到单链表的长度
    int getLength();
    void display();
    void clear();
}

实现接口及方法重写:

public class DoubleLinkedListImpl implements IDoubleLinked {
    class Node {
        private int data;

        public int getData() {
            return data;
        }

        public Node next;
        private Node prev;

        public Node getNext() {
            return next;
        }

        public Node(int data) {
            this.data = data;
            this.next = null;
            this.prev = null;
        }
    }

    private Node head;

    public Node getHead() {
        return head;
    }

    private Node last;
    public DoubleLinkedListImpl(){
        this.head = null;
        this.last = null;
    }

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

    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        if (this.last == null){
            this.head = node;
            this.last = node;
        }
        else{
            last.next = node;
            node.prev = last;
            last = node;
        }

    }
    private void checkIndex(int index){
        if (index < 0 || index > getLength()){
            throw new UnsupportedOperationException("index不合法");
        }

    }

    private Node searchIndex(int index){
        checkIndex(index);
        Node cur = this.head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public boolean addIndex(int index, int data) {
        if (index == 0){
            addFirst(data);
            return true;
        }
        if (index == getLength()){
            addLast(data);
            return true;
        }
        Node node = new Node(data);
        Node cur = searchIndex(index);
        node.next = cur;
        node.prev = cur.prev;
        node.prev.next = node;
        node.next.prev = node;
        return true;
    }

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

    @Override
    public int remove(int key) {
        Node cur = this.head;
        while (cur != last.next) {
            if (cur.data == key) {
                int oldData = cur.data;
                if (cur == this.head) {
                    this.head = head.next;
                    this.head.prev = null;
                } else {
                    cur.prev.next = cur.next;
                    //表示删除的不是尾节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //尾节点删除需要移动last
                        this.last = cur.prev;
                    }
                }
                return oldData;
            }
            cur = cur.next;
        }
        return -1;
    }

    @Override
    public void removeAllKey(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                if (cur == this.head) {
                    this.head = this.head.next;
                    this.head.prev = null;
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.last = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int getLength() {
        int length = 0;
        Node cur = this.head;
        while (cur != last.next){
            cur = cur.next;
            length++;
        }
        return length;
    }

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

    @Override
    public void clear() {
        while (head.next != null){
            Node cur = this.head.next;
            head.next = cur.next;
            cur.prev = null;
        }
        this.head = null;
        this.last = null;
    }
}

测试用例: 

public static void main(String[] args) throws InterruptedException {
        DoubleLinkedListImpl doubleLinkedList = new DoubleLinkedListImpl();
        doubleLinkedList.addfirst(1);
        doubleLinkedList.addfirst(2);
        doubleLinkedList.addfirst(3);
        doubleLinkedList.addfirst(4);
        doubleLinkedList.addfirst(5);
        doubleLinkedList.display();
        doubleLinkedList.addLast(50);
        doubleLinkedList.addLast(15);
        doubleLinkedList.addLast(78);
        doubleLinkedList.addLast(10);
        doubleLinkedList.display();
        System.out.println(doubleLinkedList.getLength());
        doubleLinkedList.addIndex(3,666);
        doubleLinkedList.display();
        System.out.println("================================");
        doubleLinkedList.remove(60);
        doubleLinkedList.display();
        System.out.println("================================");
        doubleLinkedList.removeAllKey(60);
        doubleLinkedList.display();
        System.out.println("================================");
        doubleLinkedList.clear();
        doubleLinkedList.display();
       }

猜你喜欢

转载自blog.csdn.net/LXL7868/article/details/89470149