Easily recognize the sequence list and linked list ------------ happy person's java beginner's diary [6]

Posted by brokenshadows on Sun, 28 Nov 2021 05:12:35 +0100

1, Linear table

A linear list is a finite sequence of n data elements with the same characteristics. Linear table is a data structure widely used in practice. Common linear tables: sequential table, linked list, stack, queue, string... Linear table is a linear structure logically, that is, a continuous straight line. However, the physical structure is not necessarily continuous. When the linear table is stored physically, it is usually stored in the form of array and chain structure.

2, Sequence table

2.1 what is a sequence table

Sequential table is a linear structure in which data elements are stored in sequence with a storage unit with continuous physical addresses. Generally, array storage is used. Complete the addition, deletion, query and modification of data on the array.

The sequence table can generally be divided into:
Static sequential table: use fixed length array storage.
Dynamic sequential table: use dynamic array storage.
The static sequence table is applicable to the scenario where you know how much data needs to be stored. The fixed length array of the static sequence table leads to large N, waste of space and insufficient space. In contrast, the dynamic sequence table is more flexible and dynamically allocates space according to needs

2.2 how to implement sequence table

We can create two classes under src in idea, one is Testdome, and the other is ArrList, as shown in the figure

In fact, the sequence table is like making an array with a class. Let's give a simple example

public class ArrList {
    public  int[] arr=new int[10];
    public int sum=10;
    public void add(int pos,int date){
        if(pos<0||pos>sum){
            return;
        }
        arr[pos]=date;
    }
    public void display(){
        for(int i=0;i<sum;i++){
            System.out.print(arr[i]);
        }

    }

}

public class Testdome {
    public static void main(String[] args) {
        ArrList arrList=new ArrList();
        arrList.add(0,2);
        arrList.add(1,2);
        arrList.add(2,2);
        arrList.add(3,2);
        arrList.display();

    }


    }

The result is 2222000000

2.3 conjecture on sequence table

  1. The insertion and deletion of the middle / head of the sequence table has a time complexity of O(N)
  2. To increase capacity, you need to apply for new space, copy data and release old space. There will be a lot of consumption.
  3. The capacity increase is generally double, which is bound to waste some space. For example, if the current capacity is 100 and the capacity is increased to 200 when it is full, we continue to insert 5 data, and there is no data to insert later, then 95 data spaces are wasted.

3, Linked list

3.1 concept of linked list

Linked list is a discontinuous storage structure in physical storage structure. The logical order of data elements is realized through the reference link order in the linked list. If the sequential list is an array to store numbers, the linked list is a table composed of nodes. (it consists of two parts, one is the value field, and the other is the location where the next node is stored).

There are eight kinds of linked lists, but we focus on headless one-way acyclic linked lists and headless two-way linked lists.
Headless one-way acyclic linked list: it has a simple structure and is generally not used to store data alone. In fact, it is more used as a substructure of other data structures, such as hash bucket, adjacency table of graph and so on. In addition, this structure appears a lot in the written interview.
For example:

This is the construction form of single linked list

class List {
    public  int val;
    public List next;
     public List(int val) {
         this.val=val;
     }
}
public class ArrList{
   public List head;
    public void FirstAdd(int date ){
        List pre =new List(date);
        if(head==null){
            head=pre;
            return;
        }
        pre.next=head;
        head=pre;
    }
public void display(){
        List cur =this.head;
        while (cur!=null){
            System.out.print(cur.val+" ");
             cur=cur.next;

        }
    System.out.println(" ");
    }

         }
public class Testdome {
    public static void main(String[] args) {
        ArrList arrList=new ArrList();
        arrList.FirstAdd(30);
        arrList.FirstAdd(15);
        arrList.display();
    }


    }

The result is 15 30
Because first 15 and then 30

In addition, there are some functions. Let's leave an assignment after class.
1. Implementation of headless one-way acyclic linked list
public class SingleLinkedList {
//Head insertion
public void addFirst(int data);
//Tail interpolation
public void addLast(int data);
//Insert at any position, and the first data node is subscript 0
public boolean addIndex(int index,int data);
//Find out whether the keyword is included and whether the key is in the single linked list
public boolean contains(int key);
//Delete the node whose keyword is key for the first time
public void remove(int key);
//Delete all nodes with the value of key
public void removeAllKey(int key);
//Get the length of the single linked list
public int size();

public void display();
public void clear(); }

Headless bidirectional linked list: in the collection framework library of Java, the underlying implementation of LinkedList is headless bidirectional circular linked list.

class DoubleList{
    public int val;
    public DoubleList prev;//Is to define Doublefangfa;
    public DoubleList next;
    public  DoubleList(int val){
        this.val=val;
    }
}
public class DoubleLink {
    public DoubleList head;//Define a head and list object with the DoubleList method;
    public DoubleList last;

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

    public int sizeof() {
        DoubleList cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean countain(int date) {
        DoubleList cur = this.head;
        while (cur != null) {
            if (cur.val == date) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void First(int date) {
        DoubleList node = new DoubleList(date);
        if (head == null) {
            this.head = node;
            this.last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    public void Last(int date) {
        DoubleList node = new DoubleList(date);
        if (last == null) {
            this.last = node;
            this.head = node;
        }
        DoubleList cur = this.last;
        last.next = node;
        node.prev = last;
        last = node;
    }

    public void remove(int date) {
        DoubleList node = new DoubleList(date);
        DoubleList cur = this.head;
        while (cur != null) {
            cur = cur.next;
            if (cur.val == date) {
                if (head == cur) {
                    head = head.next;
                    if (head!=null){
                    }else {
                        last=null;
                    }
                    head.prev = null;
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        last = last.prev;
                    }
                }
                return;
            }
        }
    }
}
public class Text {
    public static void main(String[] args) {
        DoubleLink doubleLink=new DoubleLink();
        doubleLink.First(1);

        doubleLink.display();
    }
}

4, The difference and relation between sequential list and linked list

Advantages of linked list: 1. The time complexity of insertion and deletion at any position is O (1). 2. There is no capacity increase problem. Insert one to open up a space.
Disadvantages of linked lists: they are stored in nodes and do not support random access. "
Advantages of sequential table: continuous space and support random access
Disadvantages of sequence table: 1. The insertion and deletion time complexity of the middle or front part is O (n). 2. The cost of capacity expansion is relatively large.

Topics: Java C Database