Chapter 2: Linear List (Sequential List VS Linked List)
In the learning data structures - Chapter: linear table (sequential storage, insertion, deletion)
和
learning data structures - Chapter: linear table (chain stores, single chain, double chain, circular linked list) in these two articles are speaking After the storage characteristics and basic operations of sequence lists and linked lists, the following explains some of the differences between sequence lists and linked lists, what choices we should make when solving practical problems, and the common operations of linear lists.
1. The difference between sequence list and linked list
The storage of each data in a singly linked list is not necessarily continuous, so we cannot achieve random access, and can only connect various nodes through pointers, so we can only achieve sequential access.
In the sequence table, the storage of each data source is continuous, we only need to pass the formula LOC(A)+(n-1)*sizeof(ElemType), as long as we know the address of the first element, we will The address of each element can be obtained by calculation, so that random access and sequential access can be realized. Therefore:
Singly linked list : can only be achieved 顺序存取
.
Sequence table : 顺序存储
sum can be achieved 随机存取
.
2. Differences between logical structure and physical structure
The storage locations of data elements in a singly linked list are arbitrary, and may or may not be adjacent, while the storage locations of sequence table elements must be adjacent.
The linked list maintains the logical structure through a pointer to the next element. Each element not only stores the data element but also the address of the next data element. All elements are threaded into a chain through the pointer. The sequence table maintains a logical structure through the adjacency of data elements, and data elements that are logically adjacent are also physically adjacent.
Sequence table : logically adjacent and physically 也相邻
, pass 相邻
means logical shutdown.
Singly linked list : logically adjacent to each other physically 不一定相邻
, 指针
expressing logical relationships through .
3. Basic operation of linear table
3.1 Insert
In the sequence table: move the element backward -> assign the new element to the
singly linked list: the next pointer of the new node S points to the next node of the p node -> modify the next pointer of the p node to point to the new node S.
3.2 Delete
In the sequence table: start with the element to be deleted, and move the following elements forward one by one.
In the singly linked list: first a new pointer points to the node q to be deleted —> then modify the next pointer of the previous node of q to point to the node after q —> then release the space of the q node.
Insert & delete
- The singly linked list is
O(1)
(the node pointer is known);O(n)
(the node pointer is unknown), but you only need to modify the pointer during operation - The sequence table is
O(n)且需要大量移动元素
3.3 Find
Find by value
Sequence table: traverse the comparison value of the sequence table. O(n)
singly-linked list: traverse the comparison value of the entire singly-linked list. O(n)
Search by serial number
In the sequence table: According to the sequence number, we can directly find the position of the array element corresponding to the sequence number through the subscript of the array. O(1) In the
singly linked list: the entire linked list needs to be traversed. O(n)
Find
无序
Both singly linked list and sequence list ( ) in search by value are O(n);- The singly linked list is O(n) in the sequential search, and the sequence table is O(1);
3. Differences in memory space
Sequential storage Whether statically allocated or non-static, 预先分配
appropriate memory space is required . When statically allocated, the pre-allocated space is too large and will cause waste, and too small will cause overflow; although it will not overflow during dynamic allocation, expansion requires a large number of moving elements, and the operation efficiency is low
. The format is stored in the 需要时分配
node space, which is efficient and convenient, but the pointer needs to use extra space.
4. How to choose the storage structure of the linear table?
5. Common operations of linear tables
5.1 Find the best value
Sequence table
First, use two variables to mark the first element of the array as the maximum and minimum, and then traverse the sequence table. If the element is greater than the maximum, modify the variable of the maximum; if it is less than the minimum, modify the variable of the minimum. At the end of the traversal, the maximum and minimum values can be obtained.
Time complexity: O(n)
int min=L[0];
int max=L[0];
for(int i=0;i<n;i++){
if(min>L[i]){
min=L[i];
}
if(max<L[i]){
max=L[i];
}
}
Single list
The same is true and the sequence table.
Time complexity: O(n)
5.2 Inversion
Sequence table
Idea one, we apply for an auxiliary space, then traverse the entire sequence table from the end to the beginning, and then insert each element into the new space, but the time and space complexity of this is O(n). Here is another method:
We can use two tags to point to the values of the head element i
and the tail element j
respectively, and then exchange the values of the two tagged data elements, then move the i
tag back one bit, the j
tag forward one bit, and continue to exchange the value of the element... Until i<j
the end of the time. The time complexity of this method is O(n), and the space complexity is O(1)
Single list
In the singly linked list, we have a head pointer ( p
) and a tail pointer ( r
), we move the first data element node to the (r)
back of the pointer, and then continue to move the first data element node to the ( r
) pointer... continue to operate , Until p
the next node of the pointer is the end of the ( r
) node.
Code explanation: It
p->next!=r
is the judgment condition for the end of the movement, and the movement ends when ther
node is the first node.temp=p->next
It isp
the next node to save the node.p->next=temp->next
It is directlyp结点和temp后面的结点相连接
. [The first two statements are to take out and save the node to be moved]temp->next=r->next
Modify the next pointer of the node to be inserted to point to the nextr
node of the node, whichr->next=temp;
is to connect. [The last two sentences are connection operations]
Time complexity: O(n).
5.3 Merger
Sequence table
You need to apply for a new space, the size is the sum of the two sequential table spaces, and then you need to find the one with the smallest element value in the two book sequence tables, and then insert it into the new table. At this time, we need to use three markers, one ( i
) points to the first position of L1, one ( j
) points to the first position of L2, and one ( k
) points to the new array. 注意这里的顺序表的元素都是从小到大排列的,我们只需要保证合并后的元素值也是从小到大排列。
Then we need to compare the values of the elements of the two arrays at a time. If L1[i]<L2[i], add L1[i] to the new array at this time, and mark k++ at the same time, then leave i++ and j unchanged, and then continue the comparison. If the elements of the L1 array are compared, At this time, the L2 array is still left, and there is no need to compare at this time, just add the elements of the L2 array to the new array one by one.
Among them
i<L1_Size&&j<L2_Size
is to determine whether the two arrays are all removed. The following twowhile
judgments are to judge whether the two arrays are all removed, because I don't know which array is not removed.
Time complexity: O(n)
Single list
The sequence table is basically the same.