Learning Data Structure-Chapter 2: Linear List (Sequential List VS Linked List)

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:

Insert picture description here
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.

Insert picture description here

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.

Insert picture description here

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 picture description here

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 isO(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)

Insert picture description here

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?

Insert picture description here

5. Common operations of linear tables

Insert picture description here

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.

Insert picture description here
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.

Insert picture description here

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 iand the tail element jrespectively, and then exchange the values ​​of the two tagged data elements, then move the itag back one bit, the jtag forward one bit, and continue to exchange the value of the element... Until i<jthe end of the time. The time complexity of this method is O(n), and the space complexity is O(1)

Insert picture description here
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 pthe next node of the pointer is the end of the ( r) node.

Insert picture description here

Code explanation: It p->next!=ris the judgment condition for the end of the movement, and the movement ends when the rnode is the first node. temp=p->nextIt is pthe next node to save the node. p->next=temp->nextIt is directly p结点和temp后面的结点相连接. [The first two statements are to take out and save the node to be moved] temp->next=r->nextModify the next pointer of the node to be inserted to point to the next rnode of the node, which r->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.

Insert picture description here

Among them i<L1_Size&&j<L2_Sizeis to determine whether the two arrays are all removed. The following two whilejudgments 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.

Insert picture description here

Welcome to public concern number 理木客more exciting waiting for you to discover

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106180993