三、链表

1.链表分析

1587631164918

1587632097840

2.链表头部head指向实体结点

在链表头部添加元素

1587692297663

在链表中间添加元素

1587693990726

作图分析:

1587700774657

package dataStructure.linkedlsit;

public class LinkedList<E> {
    /**
     * 私有成员内部类
     * 构造“节点”数据类型
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        public String toString() {
            return e.toString();
        }
    }

    private Node head;
    private int size;

    public LinkedList() {
        head = null;
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addFirst(E e) {
        Node node = new Node(e);
        node.next = head;
        head = node;
//        以上三条语句可用以下一句代替
//        head = new Node(e, head);

        size++;
    }

    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("越界");
        }
        if (index == 0) {
            addFirst(e);
        } else {
            Node prev = head;
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }

            prev.next = new Node(e, prev.next);
            size++;
        }
    }

    public void addLast(E e){
        add(size,e);
    }
}

3.为链表设立虚拟头结点

1587701019676

删除索引为2位置的元素

1587776830053

实现类:

package dataStructure.linkedlsit;

public class LinkedList<E> {
    /**
     * 私有成员内部类
     * 构造“节点”数据类型
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        public String toString() {
            return e.toString();
        }
    }

    /*声明一个虚拟头结点*/
    private Node dummyHead;
    private int size;

    public LinkedList() {
//        初始化时虚拟头结点值为空,下级结点为空
        dummyHead = new Node(null, null);
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }


    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("越界");
        }

        /*找到index之前的结点,prev指向该结点*/
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }

        /*插入新的结点*/
        prev.next = new Node(e, prev.next);
        size++;

    }

    public void addFirst(E e) {
        add(0, e);
    }

    public void addLast(E e) {
        add(size, e);
    }

    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("越界");
        }
        Node cur = dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("越界");
        }
        Node cur = dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public boolean contains(E e) {
        Node cur = dummyHead;
        // 从虚拟结点遍历到null前一个结点
        while (cur != null) {
            if (cur.e.equals(e)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("越界");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;

        return retNode.e;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size - 1);
    }

    public String toString() {
        StringBuilder res = new StringBuilder();
        // 注意这种循环方式
        for (Node cur = dummyHead.next; cur != null; cur = cur.next) {
            res.append(cur + "->");
        }
        res.append("null");
        return res.toString();
    }
}
package dataStructure.linkedlsit;

public class Main {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 5; i++) {
            linkedList.addFirst(i);
            linkedList.addLast(i);
            System.out.println(linkedList);
        }

        linkedList.add(3, 888);
        System.out.println(linkedList);

        linkedList.remove(3);
        System.out.println("remove 3: " + linkedList);
        linkedList.removeFirst();
        System.out.println("removeFirst: " + linkedList);
        linkedList.removeLast();
        System.out.println("removeLast: " + linkedList);
    }
}

结果:

0->0->null
1->0->0->1->null
2->1->0->0->1->2->null
3->2->1->0->0->1->2->3->null
4->3->2->1->0->0->1->2->3->4->null
4->3->2->888->1->0->0->1->2->3->4->null
remove 3: 4->3->2->1->0->0->1->2->3->4->null
removeFirst: 3->2->1->0->0->1->2->3->4->null
removeLast: 3->2->1->0->0->1->2->3->null

4.链表时间复杂度分析

1587777901488

5.链表实现栈

1587779802332

package dataStructure.linkedlsit;

import dataStructure.stack.Stack;

public class LinkedListStack<E> implements Stack<E> {
    private LinkedList<E> list;

    public LinkedListStack() {
        list = new LinkedList<>();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack: top ");
        res.append(list);
        return res.toString();
    }

    public static void main(String[] args) {
        LinkedListStack<Integer> stack = new LinkedListStack<>();
        for (int i = 0; i < 6; i++) {
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();
        System.out.println(stack);
    }
}

6.链表实现队列

1587781639892

代码实现

package dataStructure.linkedlsit;

import dataStructure.queue.Queue;

public class LinkedListQueue<E> implements Queue<E> {

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node head;
    private Node tail;
    private int size;

    public LinkedListQueue() {
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void enqueue(E e) {
        // 队列为空时
        if (tail == null) {
            tail = new Node(e);
            head = tail;
        } else {  // 队列不为空
            tail.next = new Node(e);
            tail = tail.next;
        }
        size++;
    }


    @Override
    public E dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("空");
        }

        Node retNode = head;
        head = head.next;
        retNode.next = null;
        // 原队列只有一个元素的情况
        if (head == null) {
            tail = null;
        }
        size--;
        return retNode.e;
    }

    @Override
    public E getFront() {
        if (isEmpty()) {
            throw new IllegalArgumentException("空");
        }
        return head.e;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue: front [ ");
        Node cur = head;
        while (cur != null) {
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("null ] tail");
        return res.toString();
    }

    public static void main(String[] args){
        LinkedListQueue<Integer> queue = new LinkedListQueue<>();
        for(int i = 0;i < 10; i++){
            queue.enqueue(i);
            System.out.println(queue);
            if(i % 3 == 1){
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}

结果

Queue: front [ 0->null ] tail
Queue: front [ 0->1->null ] tail
Queue: front [ 1->null ] tail
Queue: front [ 1->2->null ] tail
Queue: front [ 1->2->3->null ] tail
Queue: front [ 1->2->3->4->null ] tail
Queue: front [ 2->3->4->null ] tail
Queue: front [ 2->3->4->5->null ] tail
Queue: front [ 2->3->4->5->6->null ] tail
Queue: front [ 2->3->4->5->6->7->null ] tail
Queue: front [ 3->4->5->6->7->null ] tail
Queue: front [ 3->4->5->6->7->8->null ] tail
Queue: front [ 3->4->5->6->7->8->9->null ] tail

7.链表应用习题(来自leetcode)

移除链表元素

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
package dataStructure.linkedlsit;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int x) {
        val = x;
    }

    /**
     * 传入一个数组,构造一个ListNode链表,this表示链表的头结点
     * @param arr:int[]
     */
    public ListNode(int[] arr){
        if(arr == null || arr.length == 0){
            throw new IllegalArgumentException("空");
        }
        val = arr[0];
        ListNode cur = this;
        for(int i = 1; i < arr.length; i++){
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }

    public String toString(){
        StringBuilder res = new StringBuilder();
        ListNode cur = this;
        while(cur != null){
            res.append(cur.val + "->");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }
}
package dataStructure.linkedlsit;

public class Solution {
    public static ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode prev = dummyHead;
        while (prev.next != null) {
            if (prev.next.val == val) {
                // 把已删除的元素引用置空,利于垃圾收集
//                ListNode delNode = prev.next;
//                prev.next = delNode.next;
//                delNode.next = null;

                // 下标向后移动即可,不用管已删除数据
                prev.next = prev.next.next;
            } else {
                prev = prev.next;
            }
        }

        return dummyHead.next;

    }

    public static void main(String[] args) {
        int[] array = {1, 2, 6, 8, 9, 11, 324, 5, 77, 6, 1};
        ListNode head = new ListNode(array);
        System.out.println(head.toString());
        System.out.println(removeElements(head,1));
    }
}

结果

1->2->6->8->9->11->324->5->77->6->1->null
2->6->8->9->11->324->5->77->6->null
posted @ 2020-04-26 09:35  jacob_code  阅读(35)  评论(0)    收藏  举报