Talking about the data structure List (Java description)

Talking about the data structure List (java description)

List (linear table):
List linear table is the most basic, simplest, and most commonly used data structure. A linear table is a finite sequence with n data elements of the same type. The relationship between data elements They are all in one-to-one correspondence.
Its characteristics are:

  1. There must be a unique "first element" in the set
  2. There must be a unique "last element" in the set
  3. all but the last element have a unique successor
  4. All but the first element have a unique predecessor

The "linear" referred to here is only logically linear, so doubly linked lists and circular linked lists are also
logically subdivided into linear lists. Linear lists can be divided into general linear lists and restricted linear lists. General linear lists are such as order Tables and linked lists can freely add and delete nodes, while restricted linear tables include stacks, and operations on queue nodes are restricted

insert image description here

Methods implemented by the List interface in Java
The List interface is implemented from the Collection interface. In this interface, users can precisely control the insertion position of each element in the list. Users can access elements through integer indexes and search for elements in the list.
Unlike sets, lists generally allow elements to repeat (e1.equals(e2) == true).

Example of using the List interface:
List realizes Yang Hui's triangle

public List<List<Integer>> generate(int numRows) {
    
    
    List<Integer> row = new ArrayList<>();
    List<List<Integer>> col = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
    
    
        row = new ArrayList<>();
        for (int j = 0; j <= i; j++) {
    
    
            if (j == 0 || j == i) {
    
    
                row.add(1);
            } else {
    
    
                List<Integer> temp = col.get(i - 1);
                row.add(temp.get(j - 1) + temp.get(j));
            }
        }
        col.add(row);

    }
    return col;
}

ArrayList sequence table
sequence table is a linear table stored in the form of an array in computer memory. The
elements in the table are stored one by one in a group of continuous storage units. This storage structure is a sequential structure. The
essence of a sequence table is a array

The ArrayList interface in Java is implemented from the List interface, and the resizable array implements the List interface, implements all optional list operations, and allows all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array used internally to store the list.
Each ArrayList instance has a capacity. Capacity is the size of the array used to store the elements in the list, it is always at least as big as the list size, when elements are added to the ArrayList its capacity is full, its capacity will grow automatically.

Implementation of ArrayList——

  • Imitation of the Iterable interface
package List;
//仿写真实的(java.util.Iterable)接口

public interface Iterable {
    
    
    Iterator iterator();
}
  • Imitation of the List interface
public interface List<E> extends Iterable{
    
    
    boolean add(E e);
    void addIndex(int index,E e);
    void clean();
    boolean contains(Object o);
    boolean equals(Object e);
    E get(int index);
    int indexOf(Object o);
    boolean isEmpty();
    E remove(int index);
    boolean remove(Object o);
    E set(int index, E e);
    int size();
}
  • Imitation of the Iterator interface
package List;
//仿写真实的(java.util.Iterator)接口

public interface Iterator<E> {
    
    
    boolean hasNext();
    E next();
    void remove();
}
  • Implement an ArrayList iterator
package List;

public class ArrayListIterator implements Iterator {
    
    
    //对一个顺序表迭代,重要在于控制下标
    private MyArrayList arrayList;
    private int index;

    public ArrayListIterator(MyArrayList arrayList) {
    
    
        this.arrayList = arrayList;
        this.index = 0;
    }

    public ArrayListIterator() {
    
    

    }

    @Override
    public boolean hasNext() {
    
    
        return index < arrayList.size();
    }

    @Override
    public Object next() {
    
    
        return arrayList.get(index++);
    }

    @Override
    public void remove() {
    
    
        arrayList.clean();
    }
}
  • Concrete implementation of ArrayList
package List;

import java.util.Arrays;

//仿写真实的(java.util.ArrayList)实现类
public class MyArrayList<E> implements List{
    
    
    private int[] arr;
    private int size;

    public MyArrayList() {
    
    
        this.arr = new int[10];
        this.size = 0;
    }

    @Override
    public boolean add(Object o) {
    
    
        if (!(o instanceof Integer)) {
    
    
            return false;
        }
        if (size == arr.length) {
    
    
                capacity();
        }
        arr[size] = (int)o;
        size++;
        return true;
    }

    @Override
    public void addIndex(int index, Object o) {
    
    
        if(index < 0 || index > size+1){
    
    
            return;
        }
        if (size == arr.length) {
    
    
            capacity();
        }
        for (int i = size; i >= index; i--) {
    
    
            arr[i + 1] = arr[i];
            if (i == index) {
    
    
                arr[index] = (int)o;
                size++;
            }
        }
    }

    private void capacity() {
    
    
        int[] newArr =  new int[2 * arr.length];
        System.arraycopy(arr, 0, newArr, 0, arr.length);
        arr = newArr;
    }

    @Override
    public void clean() {
    
    
        size = 0;
    }

    @Override
    public boolean contains(Object o) {
    
    
        for (int e : arr){
    
    
            if(e == (int)o){
    
    
                return true;
            }
        }
        return false;
    }


    @Override
    public Object get(int index) {
    
    
        if(index < 0 && index > size-1){
    
    
            return null;
        }else{
    
    
            return arr[index];
        }
    }

    @Override
    public int indexOf(Object o) {
    
    
        for (int i = 0 ;i < size;i++) {
    
    
            if(arr[i] == (int)o){
    
    
                return i;
            }
        }
        return -1;
    }

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

    @Override
    public Object remove(int index) {
    
    
        if(index < 0 || index > size+1){
    
    
            return null;
        }
        int num = arr[index];
        if (size - 1 - index >= 0) System.arraycopy(arr, index + 1, arr, index, size - 1 - index);
        arr[size-1] = 0;
        size--;
        return num;
    }

    @Override
    public boolean remove(Object o) {
    
    
        int i;
        boolean isFind = false;
        for (i = 0; i < size; i++) {
    
    
            if(arr[i] == (int)o){
    
    
                isFind = true;
                break;
            }
        }
        if(isFind) {
    
    
            if (size - 1 - i >= 0) System.arraycopy(arr, i + 1, arr, i, size - 1 - i);
            arr[size - 1] = 0;
            size--;
        }
        return isFind;
    }

    @Override
    public Object set(int index, Object o) {
    
    
        if(index < 0 || index > size+1){
    
    
            return null;
        }
        arr[index] = (int)o;
        return arr[index];
    }

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

    public Iterator iterator() {
    
    
        return new ArrayListIterator(this);
    }

    @Override
    public String toString() {
    
    
        return "MyArrayList{" +
                "arr=" + Arrays.toString(arr) +
                '}';
    }
}

LinkedList (linked list)
Linked list is a non-sequential, non-sequential physical storage unit, logically sequential storage storage structure. Using the linked list structure can overcome the disadvantage that the sequence table needs to know the data size in advance, and the linked list structure can make full use of the computer memory space and realize flexible memory dynamic management. There are many different types of linked lists: one-way linked lists, doubly linked lists, and circular linked lists. Linked lists in Java are stored in the form of references

The LinkedList interface in Java is implemented from the List interface, Deque interface, Cloneable interface, Serializable interface, and the double-linked list implements the List and Deque interfaces.
Implements all optional list operations and allows all elements (including null). All operations are expected as expected on a two-way list. Indexing into a list will traverse the list from the beginning or end, whichever is closer to the specified index.

Simple implementation of linked list (one-way linked list)——

  • Imitation of the Iterable interface
package MyLinkedList;

public interface Iterable {
    
    
    Iterator iterator();
}
  • Imitation of the Iterator interface
package MyLinkedList;

public interface Iterator {
    
    
    boolean hasNext();
    Integer next();
}
  • Imitation of the List interface
package MyLinkedList;

public interface List extends Iterable {
    
    
    void add(Integer e);
    //将指定的元素追加到此列表的末尾
    boolean add(int index, Integer element);
    //在此列表中的指定位置插入指定的元素
    void addFirst(Integer e);
    //在该列表开头插入指定的元素
    void addLast(Integer e);
    //将指定的元素追加到此列表的末尾
    void clear();
    //从列表中删除所有元素
    boolean contains(Object o);
    //如果此列表包含指定的元素,则返回 true
    Integer getFirst();
    //返回此列表中的第一个元素
    Integer getLast();
    //返回此列表中的最后一个元素
    int indexOf(Object o);
    //返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1
    int lastIndexOf(Object o);
    //返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1
    Integer remove();
    //检索并删除此列表的头(第一个元素)
    Integer remove(int index);
    //删除该列表中指定位置的元素
    boolean remove(Object o);
    //从列表中删除指定元素的第一个出现(如果存在)
    Integer removeFirst();
    //从此列表中删除并返回第一个元素
    Integer set(int index, Integer element);
    //用指定的元素替换此列表中指定位置的元素
    int size();
    //返回此列表中的元素数
    Integer removeLast();
    //从此列表中删除并返回最后一个元素
}
  • Define the LinkedListIterator class and implement the method of the Iterator interface
package MyLinkedList;

public class LinkedListIterator implements Iterator {
    
    
    private SinglyLinkedList list;
    private Node cur;

    public LinkedListIterator(SinglyLinkedList list) {
    
    
        this.list = list;
        this.cur = list.head;
    }

    @Override
    public boolean hasNext() {
    
    
        return cur != null;
    }

    @Override
    public Integer next() {
    
    
        if(cur != null) {
    
    
            cur = cur.next;
            return cur.element;
        }else{
    
    
            return null;
        }
    }
}
  • Define the Node class to store node data
package MyLinkedList;

public class Node {
    
    
    Integer element;
    Node next;

    public Node(Integer val) {
    
    
        this.element = val;
    }

    @Override
    public String toString() {
    
    
        return " " + element + " ";
    }
}
  • Define the SinglyLinkedList class and implement the method of the list interface
package MyLinkedList;

public class SinglyLinkedList implements List{
    
    
    protected Node head;
    private int size;

    @Override
    public void add(Integer e) {
    
    
        Node node = new Node(e);
        if(head == null){
    
    
            head = node;
        }else{
    
    
            Node cur = head;
            while (cur.next != null){
    
    
                cur = cur.next;
            }
            cur.next = node;
        }
        size++;
    }

    @Override
    public boolean add(int index, Integer element) {
    
    
        if(index < 0 || index > size){
    
    
            return false;
        }
        if(index == 0){
    
    
            addFirst(element);
            return true;
        }
        int count = 0;
        Node node = new Node(element);
        Node cur = head;
        while (count < index-1){
    
    
            cur = cur.next;
            count++;
        }
        Node t = cur.next;
        cur.next = node;
        node.next = t;
        size++;
        return true;
    }

    @Override
    public void addFirst(Integer e) {
    
    
        Node node = new Node(e);
        node.next = head;
        head = node;
        size++;
    }

    @Override
    public void addLast(Integer e) {
    
    
        add(e);
    }

    @Override
    public void clear() {
    
    
        size = 0;
        head = null;
    }

    @Override
    public boolean contains(Object o) {
    
    
        Node cur = head;
        while (cur != null){
    
    
            if(o.equals(cur.element)){
    
    
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public Integer getFirst() {
    
    
        if(head == null) {
    
    
            return null;
        }else{
    
    
            return head.element;
        }
    }

    @Override
    public Integer getLast() {
    
    
        if(head == null){
    
    
            return null;
        }else{
    
    
            Node cur = head;
            while (cur.next != null){
    
    
                cur = cur.next;
            }
            return cur.element;
        }
    }

    @Override
    public int indexOf(Object o) {
    
    
        Node cur = head;
        int count = 0;
        while (cur != null) {
    
    
            if(o.equals(cur.element)){
    
    
                return count;
            }
            cur = cur.next;
            count++;
        }
            return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
    
    
        int out = -1;
        int count = 0;
        Node cur = head;
        while (cur != null){
    
    
            if(o.equals(cur.element)){
    
    
                out = count;
            }
            cur = cur.next;
            count++;
        }
        return out;
    }

    @Override
    public Integer remove() {
    
    
        if(head == null){
    
    
            return null;
        }
        int out = head.element;
        head = head.next;
        size--;
        return out;
    }

    @Override
    public Integer remove(int index) {
    
    
        if(index < 0 || index > size-1){
    
    
            return null;
        }
        if(index == 0){
    
    
            return remove();
        }
        Node cur = head;
        Node pre = null;
        int count = 0;
        while(count < index){
    
    
            pre = cur;
            cur = cur.next;
            count++;
        }
        int out = cur.next.element;
        pre.next = cur.next;
        size--;
        return out;
    }

    @Override
    public boolean remove(Object o) {
    
    
        Node cur = head;
        Node pre = null;
        int count = 0;
        while(cur != null){
    
    
            if(o.equals(cur.element)){
    
    
                if(pre == null){
    
    
                    remove();
                }else {
    
    
                    pre.next = cur.next;
                }
                return true;
            }
                pre = cur;
                cur = cur.next;
        }
        return false;
    }

    @Override
    public Integer removeFirst() {
    
    
        return remove();
    }

    @Override
    public Integer set(int index, Integer element) {
    
    
        int count = 0;
        Node cur = head;
        while (cur != null){
    
    
            if(count == index){
    
    
                int out = cur.element;
                cur.element = element;
                return out;
            }
            count++;
            cur = cur.next;
        }
        return null;
    }

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

    @Override
    public Integer removeLast() {
    
    
        Node pre = null;
        Node cur = head;
        while (cur.next != null){
    
    
            pre = cur;
            cur = cur.next;
        }if(pre == null){
    
    
            return null;
        }
        pre.next = null;
        return cur.element;
    }

    @Override
    public Iterator iterator() {
    
    
        return new LinkedListIterator(this);
    }

    public void print(){
    
    
        Node cur = head;
        System.out.print("[");
        while (cur != null){
    
    
            System.out.print(cur);
            cur = cur.next;
        }
        System.out.println("]");
    }
}

The above is the summary of the list of this blog. With the deepening of the follow-up study, the content will be supplemented and modified synchronously. It will be a great honor to help all bloggers. Please correct me

Guess you like

Origin blog.csdn.net/m0_46233999/article/details/109357415