模拟实现LinkedList

文章目录

  • 一、模拟实现无头单向非循环链表
  • 二、模拟实现无头双向非循环链表

一、模拟实现无头单向非循环链表

public class SingleLinkList {
    static class LinkNode {
        public int value;
        public LinkNode next;
        public LinkNode(int value) {
            this.value = value;
        }
    }
    public LinkNode head;
    public void creatSingleLinkList() {
        LinkNode node1 = new LinkNode(1);
        LinkNode node2 = new LinkNode(2);
        LinkNode node3 = new LinkNode(3);
        LinkNode node4 = new LinkNode(4);
        LinkNode node5 = new LinkNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }

    public void display() {
        LinkNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        LinkNode cur = this.head;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    //头插法
    public void addFirst(int data) {
        LinkNode cur = new LinkNode(data);
        cur.next = this.head;//如果head为null这样写也可以
        this.head = cur;
    }

    //尾插法
    public void addLast(int data) {
        LinkNode cur1 = new LinkNode(data);
        LinkNode cur = this.head;
        if(cur == null) {//要另外考虑head为null的情况
            this.head = cur1;
            return;
        }
        while(cur.next != null) {//注意这里是cur.next!=null而不是cur!=null,因为要找到最后一个节点
            cur = cur.next;
        }
        cur.next = cur1;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        LinkNode cur = this.head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    private LinkNode addIndexSubOne(int index) {
        LinkNode cur = this.head;
        while((index - 1) != 0) {//找到要插入位置下标的前一个节点,因为要通过这个节点找到下一个节点的引用
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        LinkNode cur1 = new LinkNode(data);
        if((index < 0) || (index > size())) {
            System.out.println(index + "坐标不合法");
            return false;
        }
        if(index == 0) {//在0下标处插入数据
            addFirst(data);
            return true;
        }
        LinkNode cur = addIndexSubOne(index);
        cur1.next = cur.next;//重点,对在size()下标处插入数据同样适用
        cur.next = cur1;//重点
        return true;
    }

    private LinkNode removeSubOne(int key) {
        LinkNode cur = this.head;
        while(cur.next != null) {//找到要删除数的前一个节点,因为要通过这个节点找到下一个节点的引用
            if(cur.next.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(this.head.value == key) {//删除第一个节点
            this.head = this.head.next;
            return;
        }
        LinkNode cur = removeSubOne(key);
        if(cur == null) {
            System.out.println("要删除的关键字不存在");
            return;
        }
        LinkNode del = cur.next;//重点,对删除最后一个节点同样适用
        cur.next = del.next;//重点
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
        LinkNode cur = head.next;//cur为要删除的节点
        LinkNode pre = head;//pre为要删除节点的前驱
        while(cur != null) {
            if(cur.value == key) {
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }
        if(this.head.value == key) {
            this.head = this.head.next;
        }
    }

    public void clear() {
        this.head = null;
    }
}

二、模拟实现无头双向非循环链表

public class LinkedList {
    static class LinkNode {
        public int val;
        public LinkNode prev;
        public LinkNode next;
        public LinkNode(int data) {
            this.val = data;
        }
    }
    public LinkNode head;
    public LinkNode last;

    //得到单链表的长度
    public int size() {
        int count = 0;
        LinkNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        LinkNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        LinkNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        LinkNode cur = new LinkNode(data);
        if(head == null) {
            head = cur;
            last = cur;
            return;//如果有else这里就不用加return
        }
        cur.next = head;
        head.prev = cur;
        head = cur;
    }

    //尾插法
    public void addLast(int data) {
        LinkNode cur = new LinkNode(data);
        if(last == null) {
            head = cur;
            last = cur;
            return;
        }
        last.next = cur;
        cur.prev = last;
        last = cur;//或last=last.next
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if((index < 0) || (index > size())) {
            throw new IndexOutOfException(index + "下标不合法");
        }
        if(index == 0) {
            addFirst(data);
            return true;
        }
        if(index == size()) {
            addLast(data);
            return true;
        }
        LinkNode node = new LinkNode(data);
        LinkNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;//index==size()时这样插入不行,所以index==size()时要单独用尾插法插入
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
        return true;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            System.out.println("链表为空");
            return;
        }
        LinkNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur.prev == null) {//删除头节点
                    if(cur.next != null) {
                        head = cur.next;
                        head.prev = null;
                    }else {//只有一个节点,且这个节点是要删除的节点
                        head = cur.next;
                        last = null;
                    }
                }else {
                    if(cur.next == null) {//删除尾节点
                        cur.prev.next = cur.next;
                        last = cur.prev;
                    }else {
                        cur.prev.next = cur.next;//删除中间节点
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
        System.out.println("没有要删除的数据");
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            System.out.println("链表为空");
            return;
        }
        LinkNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur.prev == null) {//删除头节点
                    if(cur.next != null) {
                        head = cur.next;
                        head.prev = null;
                    }else {//只有一个节点,且这个节点是要删除的节点
                        head = cur.next;
                        last = null;
                    }
                }else {
                    if(cur.next == null) {//删除尾节点
                        cur.prev.next = cur.next;
                        last = cur.prev;
                    }else {
                        cur.prev.next = cur.next;//删除中间节点
                        cur.next.prev = cur.prev;
                    }
                }
                cur = cur.next;
            }else{
                cur = cur.next;
            }
        }
    }

    public void clear() {
        LinkNode cur = head;
        while(cur != null) {//将每个节点的next和prev置为null
            LinkNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;//因为执行完上面的代码之后,链表断开了,但头结点和尾节点仍有head和last在引用
        last = null;
    }
}

你可能感兴趣的:(数据结构,java,学习,开发语言)