[data structure and algorithm] - linked list

Keywords: Java data structure linked list

  1. home page
  2. special column
  3. java
  4. Article details
0

[data structure and algorithm] - linked list

benntty74 Published 13 minutes ago

1. What is a linked list

Linked list is a kind of linear list. Linear table means literally that the relationship between data is connected by a line.

Linear tables are divided into address space continuous and address space discontinuous.

For example, arrays are continuous linear tables in address space. They are a continuous data block in memory. Blocks are close to each other, just like the relationship of [- 1,0,1] on the X-axis.

The linked list is a linear list with discontinuous address space. They are not continuous blocks in memory, but scattered everywhere, and then connected through a curved line.

The following is a more vivid description.

  • Address space continuous linear table:

    |😀 |😂|😆|🙃|😉|

  • Address space discontinuous linear table

    😀---------->😂------->😆--->🙃------------>🙃>😉

2. Linked list features

Linked list is divided into single linked list and double linked list. A single linked list only knows the location of the next node (the above figure is a single linked list), while a double linked list can know the location of the previous and next nodes.

Each node in a single linked list contains two parts, one is the data on the linked list node, and the other is the line (pointer) pointing to the next node.

Each node on the double linked list contains three parts. One part is the data on the linked list node, one part is the line (pointer) pointing to the next node, and the other part is the line (pointer) pointing to the previous node.

Basic operation of linked list

  • New nodes: at the beginning, middle and end
  • Delete node: at beginning, middle and end
  • Query node: query the node data of the ith position

3. java implementation (double linked list)

public class LinkedList<E> implements List<E>, Queue<E>, Stack<E> {

    private int size = 0;

    private Node<E> head;
    private Node<E> tail;


    public LinkedList() {
        tail = head = null;
    }

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

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

    @Override
    public boolean add(E e) {
        return offer(e);
    }

    @Override
    public boolean remove(E e) {
        Node<E> tmp = head;
        int idx = 0;
        while (tmp != null) {
            if (tmp.e.equals(e)) {
                remove(idx);
                return true;
            }
            idx++;
            tmp = tmp.next;
        }
        return false;
    }

    @Override
    public boolean contains(E e) {
        Node<E> tmp = head;
        while (tmp != null) {
            if (tmp.e.equals(e)) {
                return true;
            }
            tmp = tmp.next;
        }
        return false;
    }

    @Override
    public E get(int i) {
        if (i > size) {
            throw new IndexOutOfBoundsException("Index i " + i + "out of range " + size);
        }
        if (i == 0) {
            return head.e;
        }
        if (i == size - 1) {
            return tail.e;
        }
        Node<E> tmp = head;
        for (int j = 0; j < i; j++) {
            tmp = tmp.next;
        }
        return tmp.e;
    }

    @Override
    public E remove(int i) {
        if (i >= size) {
            throw new IndexOutOfBoundsException("Index i " + i + "out of range " + size);
        }
        if (i == 0) {
            return poll();
        }
        if (i == size - 1) {
            return pop();
        }
        Node<E> tmp = head;
        for (int j = 0; j < i; j++) {
            tmp = tmp.next;
        }
        E e = tmp.next.e;
        tmp.next.next.prev = tmp;
        tmp.next = tmp.next.next;
        return e;
    }

    @Override
    public boolean add(E e, int i) {
        if (i > size) {
            throw new IndexOutOfBoundsException("Index i " + i + "out of range " + size);
        }
        Node<E> node = new Node<>(e);
        if (i == size) {
            return offer(e);
        }
        if (i == 0) {
            node.next = head;
            node.prev = null;
            head = node;
        } else {
            Node<E> tmp = head;
            for (int j = 0; j <= i; j++) {
                tmp = tmp.next;
            }
            tmp.next = node;
            node.prev = tmp;
        }
        size++;
        return false;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("LinkedList: { ");
        Node<E> node = head;
        while (node != null) {
            sb.append(node.e).append("<->");
            node = node.next;
        }
        sb.append("null }");
        return sb.toString();
    }

    private static class Node<E> {

        E e;

        Node<E> next;
        Node<E> prev;

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

}

4. Data structure series code address

Individuals have also implemented other data structures and algorithms. Interested partners can view the detailed and complete code and test on Github or blog.

Github: https://github.com/bennetty74...

Bugkit blog: http://bugkit.cn or IP

Reading 10 was published 13 minutes ago
Like collection

Small transparent

34 prestige
0 fans
Focus on the author
Submit comments
You know what?

Register login

Small transparent

34 prestige
0 fans
Focus on the author
Article catalog
follow
Billboard

Posted by stolzyboy on Thu, 28 Oct 2021 12:58:33 -0700