Difference between linear list, linear list and linked list

1. The main difference

存储类别     顺序存储结构 单链表
存储分配方式  用一段连续的存储单元依次存储线性表的数据元素       采用链式存储结构,用一组任意的存储单元存放线性表的元素
时间性能        查找O(1)、插入和删除O(n)    查找O(n)、插入和删除O(1)
空间性能      需要预分配存储空间,分大了浪费,小了容易发生上溢  不需要分配存储空间,只要有就可以分配,元素个数不受限制

From the above comparison, some empirical conclusions can be drawn:

If the linear table needs to be searched frequently and insert and delete operations are rarely performed, the sequential storage structure should be used. If frequent insertion and deletion are required, a singly linked list structure should be used .

      When the number of elements in the linear table varies greatly or the size of the elements is not known at all, it is best to use a singly linked list structure, so that the size of the storage space does not need to be considered. If the approximate length of the linear table is known in advance, it is much more efficient to store the structure in order.

http://zhidao.baidu.com/question/237262292.html?si=10&qbpn=1_10&tx=&wtp=wk&word=%E7%BA%BF%E6%80%A7%E8%A1%A8%E5%92%8C%E9%93%BE%E8%A1%A8%E7%9A%84%E5%8C%BA%E5%88%AB&fr=solved&from=qb&ssid=&uid=bd_1424171539_897&pu=sz%40224_240%2Cos%40&step=18&bd_page_type=1&init=middle

Linear lists include sequential and linked lists:

顺序表里面元素的地址是连续的,
链表里面节点的地址不是连续的,是通过指针连起来的。

http://www.bkjia.com/Javabc/1125496.html

2. Introduction

       A logical structure, a finite sequence of n data elements of the same data type, each element except the first element has and only one immediate predecessor, and each element except the last element has and only one immediate predecessor. successor.

Features of Linear Tables:

(1) The number of elements is limited (2) Logically, the elements are in sequence

(3) The data types are the same (4) Only the logical relationship between elements is discussed

Note: Linear lists are logical structures, while sequential lists and linked lists are storage structures.
write picture description here

1. sequential storage

The sequence table is implemented using an array, a group of storage units with consecutive addresses, and the size of the array is specified in two ways, one is static allocation, and the other is dynamic expansion.

注:线性表从1开始,而数组从0开始。

优点:随机访问特性,查找O(1)时间,存储密度高;逻辑上相邻的元素,物理上也相邻;

缺点:插入删除需移动大量元素。

The operations related to the sequence table are related to the array, generally moving the elements of the array.

Let's talk about the boundary conditions when inserting and deleting. First, the linear table starts from 1, and the array starts from 0. The simple file description is not direct enough. Let's talk about the picture.
write picture description here

插入时:对于线性表来说最小能插入的位置是1,最大能插入的位置是8(=7+1),所以  1<= index <=(7+1);移动数组元素时要注意,for (int i = count; i >= index; i--) {  items[i] = items[i-1];}

删除时:只能在蓝色方块之间寻找节点删除,即1 <= index <= 7。移动元素,for (i = index; i < count; i++) { items[i-1] = items[i];}

2. chain storage

      The definition of a linked list is recursive, it is either empty null, or a reference to another node node that contains a reference to the next node or linked list.

      Compared with sequential storage, the storage space is allowed to be discontinuous. When inserting and deleting, there is no need to move a large number of elements, just modify the pointer, but to find an element, you can only traverse the entire linked list from the beginning.

2.1 Singly linked list

Use arbitrary storage units to store data elements in the linear table, node types as above.

A singly linked list is divided into two types: a headed node and a non-headed node. Regardless of whether there is a head node, the head pointer points to the first node of the linked list (the head node points to the head node).

Head node: The value field can be set without any information, and the pointer field of the head node points to the first element of the linked list.

The benefits of a lead node are:

(1)链表第一位置节点上的操作和其它位置上的操作一致

(2)无论链表是否为空,头指针都指向头结点(非空),空表和非空表处理一样

(这里我没有使用头结点)

注:链表麻烦的地方是插入和删除时指针的修改,保证不断链,一般先断后链。

write picture description here

3. Basic operation

1. Head plug method

      Insert the new node into the header of the current linked list, (after the header node), the order of insertion is opposite to that in the linked list, the key point is to remember the old header and generate a new one and put it in front of the old header, such as picture:
write picture description here

核心代码:
public void headInsert(T item) {
    Node old = first;
    first = new Node();
    first.item = item;
    first.next = old;
    count++;
}

2. Tail insertion

A tail pointer is added, and the new node is inserted at the end of the linked list. The order of insertion is the same as that of the linked list, as shown in the figure:
write picture description here

核心代码:
public void tailInsert(T item) {
    Node old = last;
    last = new Node();
    last.item = item;
    last.next = null;
    if (isEmpty()) {
        first = last;
    } else {
        old.next = last;
    }
    count++;
}

      The point of inserting and deleting nodes is to disconnect first and then connect. The key is not to disconnect the chain. Taking insertion as an example (insert s between p and q), first disconnect means that p->q is disconnected first and becomes s ->q, connect after, and finally connect p and s.

3. Insert Node

The node to be inserted is s, and the post-insertion method is generally adopted, that is, the predecessor node of the node at the insertion position is found first, and then inserted, and the time complexity is O(n).
write picture description here

核心代码为:
p=getNodeByIndex(i-1);
s.next = p.next;
p.next = s;

Another method is to insert directly behind the position (pre-interpolation method), and then exchange the values ​​of the two nodes, the inserted node reaches the specified position, and the time complexity is O(1):

核心代码:
s.next = p.next;
p.next = s;
temp = p.item;    // 交换内容
p.item = s.item;
s.item = temp;

4. Delete node

The node to be deleted is q, and the predecessor node is also found first, and the pointer field can be modified, and the time complexity is O(n).
write picture description here

核心代码:
P = getNodeByIndex(i-1);
q = p.next;
p.next = q.next;
q = null;

Deleting a node can also directly delete its successor node, and then assign the content of the successor node to itself. The time complexity is O(1):

核心代码:
q = p.next;
p.item = p.next.item;
p.next = q.next;
q = null;

2.2 Doubly linked list

      The disadvantage of a singly linked list node is that there is only one successor node, and access to the predecessor node can only be traversed from the beginning (such as insertion, deletion), and the time complexity is O(n). Doubly linked list, that is, adding a node pointing to the predecessor, the node type is as follows:
write picture description here

private class Node{
    // 链表节点的嵌套类
    T item; // 节点内容
    Node prior, next; // 前驱节点和后继节点
}

      The search of the double-linked list is the same as that of the singly-linked list, which is not repeated here. The structure of the double-linked list is also divided into head insertion and tail insertion. The only difference from the singly-linked list is to modify the predecessor pointer priority. Insertion and deletion are different because two pointers need to be modified, and the time complexity of insertion and deletion is O(1) if given the node to be operated on.

Note: Insert and delete operations are also first disconnected and then connected.

3.1, insert node

Insert the s node after the p node, first disconnect and then connect, first break the chain between p and the original successor node, so that the successor node is only related to the s node:
write picture description here
① s.next = p.next; // First disconnect the successor of p
②p.next.prior = s; // After breaking the predecessor of p's successor
③s.prior = p; // Let the predecessor of s point to p
④p.next = s; // The successor of p points to s, reconnect the chain, This step must be after ①②

3.2. Deleting a node

To delete the successor node q of node p, it is also disconnected first and then connected, and the relationship between q and its successor nodes can be transferred to p:

write picture description here
①p.next = q.next; // After breaking the successor of q first
②q.next.prior = p; // After breaking the predecessor of the successor of q

Delete the predecessor node p of node q, and transfer the relationship between p and the predecessor node to q:
①q = p.prior.next; // Change the successor of the predecessor node of p to q
②q.prior = p.prior; / / Change the predecessor node of q to the predecessor node of p

4. Circular linked list

4.1. Circular singly linked list

The difference from the singly linked list is that the pointer of the last node in the table is not null, but instead points to the head node (the first node), so that the entire linked list forms a ring. Determine whether the circular singly linked list is empty, and determine whether it is equal to the head pointer.

A circular singleton table with only one tail pointer can easily operate the head and tail of the table, because the successor of the tail pointer is the head pointer O(1).

4.2. Circular Doubly Linked List

The difference from the doubly linked list is that the prior pointer of the head node points to the tail node, and the next pointer of the tail node points to the head node.

4.3 Static linked list

A static linked list is a linked storage structure that uses an array to describe a linear list. The node also has a data field and a pointer field. The pointer here is the relative address (array subscript) of the node, and a contiguous memory space needs to be allocated in advance.

Features, insertion and deletion are the same as dynamic linked lists, with next==-1 as the end sign.

4.4 Comparison of sequential and linked lists

1. 顺序表可以顺序存取,也支持随机存取;链表只能顺序存取。

2. 顺序表逻辑上相邻的物理上也相邻;而链表不一定,它是用指针来描述元素之间的关系。

3. 顺序表插入和删除要移动大量元素;链表只需修改指针即可

Original link:

https://www.cnblogs.com/wincai/p/5893475.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854495&siteId=291194637