Data Structure Review - Chapter 2: Linear Table


Part One: Sequence List

1. Definition of sequence table

The sequential storage method of linear table is to use a set of storage units with consecutive addresses to store each element of the linear table in sequence, as shown in the figure . This can be achieved with the help of a one-dimensional array (+ length).
Sequence table physical structure

2. Operation of sequence table

Insertion, deletion, modification, and search of sequence tables (collectively: addition, deletion, modification, and search)

For example:
Insertion: Add C in the middle or behind AB
Modification: Change C to E
Delete: delete E
Search: find B

3. Advantages and Disadvantages of Sequence List

  • Advantages: (CompareSingly linked list)
    (1 ) Simple structure;
    (2) Can directly locate any element in the table, and can randomly access elements ;Serial access speed is fast.
  • Disadvantages: (CompareSingly linked list)
    (1 ) Storage space is difficult to allocate accurately and statically. A large allocation wastes space, and a small allocation may not be enough; ( 2) Insertion and deletion operations are inconvenient and require moving a large number of data elements, which results in low efficiency.

Part 1 Exercises

  1. In a sequentially stored linear list of length n, when inserting a new element into the i-th element (1≤i≤n+1), it needs to be moved backwards from back to front ( (In the future, directly substitute the specific number into this i)< /span> D.i C.n-i-1 B.n-i+1 A.n-i) elements. (Substitution method)B



  2. The time complexity of inserting an element at the end of a sequence list is of the order of (B).
    A.O( n n n) (This is the time complexity of the worst case and other average cases of head insertion) 1 1
    1) (tail insertion belongs to the best case time complexity)
    C.O(< /span> n ∗ n n*n nn)
    D.O( l o g 2 n log_2n log2n)

  3. The storage address of the first element of a vector (that is, a batch of storage units with consecutive addresses) is 100, and the length of each element is 2, then the address of the fifth element is (B).
    A.110
    B.108 (This is similar to the telephone pole problem, between the 1st and nth There are n-1 intervals * distance of each interval)
    C.100
    D.120


Part 2: Single Linked List

1. Definition of singly linked list

Under the chain storage structure, the storage space of the linear tablecan be scattered and discontinuous.
The structure of a pointer's storage node is shown in the figure.
Single node and linked list structure diagram

2. Node knowledge of singly linked list

several special nodes
(1)Node: Usually refers to the storage space related to a data element a> , so that the program is both simple and efficient. Empty lists and non-empty lists do not need to be considered separately. The function of the head node is to use linked list programming. first node (head node) of the linked list. before the : Attach a node Header node (5);The first data element in the linear table stores: In the chain storage structure, First node (4); The last data element in the linear table: Tail element (3);The first data element:Header element
(2);



  • For ordinary singly linked lists, we can judge whether it is based on whether the head node points to null (NULL, that is, ^ in the picture) Empty table;
  • For a circular linked list, the tail node needs to point to the head node, so you can judge whether it is based on whether the head nodepoints to itself Empty table.

3. Operation of singly linked list

Popular science knowledge:Pointers are variables that store memory addresses
Insertion, deletion and modification of singly linked lists , search (collectively: add, delete, modify, search)

  • 插入步骤:
    ① q -> next = p ->next;
    ② p -> next = q;
  • Head insertion and tail insertion:
    Schematic diagram of head insertion method and tail insertion method
  • 删除步骤:
    ① p -> next = p -> next -> next;
  • Modify and Find Steps:
    ① One by one Post search: p = p->next;
    ② Modify after finding;

4. Advantages and disadvantages of singly linked list

  • Advantages: (CompareSequence Table)
    (1 ) Storage spaceis dynamically allocated and can be used as needed.
    (2) When inserting/deleting nodes, you usually only need to modify the pointer and do not need to move the data elements.
  • Disadvantages: (CompareSequence Table)
    (1 ) Each node is attached with a pointer field.
    (2) Non-random access structure, the search and positioning operation needs to start from the head pointer and scan along the linked list.

Part 2 Exercises

  1. The tail node (pointed by p) of the non-empty circular singly linked list head satisfies (C).
    A.p->next==NULL (The tail node of an ordinary singly linked list points to null)
    B.p==NULL
    C.p->next==head
    D.p==head

  2. In a singly linked list with n nodes, the time complexity of deleting the predecessor of the specified node is (B).
    A.O( 1 1 1)
    B.O( n n C.O(< a i=4> n 2 n^2
    n2)
    D.O( n 3 n^3 n3)

  3. The process of inserting a link node pointed to by q after the link node pointed by p in a non-empty linear linked list is to perform actions in sequence (B
    < a i=2>). A.q->next=p;p->next=q;
    B.q->next=p->next;p-> next=q;
    C.q->next=p->next;p=q;
    D.p->next=g;q-> next=p;


Part 3: Double Linked List

1. Structure of double linked list

A double linked list is a single linked list with a predecessor pointer field added to each node, which is used to store the memory address of the previous node (that is, pointing to the previous node)< a i=1> The structure of a pointer's storage node is as shown in the figure.

Double linked list nodes and linked list structure

2. Operation of double linked list

Insertion, deletion, modification, and search of doubly linked lists (collectively: addition, deletion, modification, and search)

  • InsertionSteps: (p points to the node at the insertion position, q points to the inserted node)
    ① q -> next = p -> next ; q -> prior = p;
    ② p -> next -> prior = q;
    ③ p -> next = q;
    Insert operation diagram
  • DeleteSteps: (p points to the deleted node)
    ① p -> next -> prior = p -> prior;p -> prior - > next = p -> next;
    Delete operation diagram
  • Modify and Find Steps:
    ① and singly linked list Same, search backward one by one: p = p -> next;
    ② Modify after finding;
    ③ If it is < a i=7>Continuous searchoperations are possible one by oneforward/backwardsearch: p = p - > prior;

Part 3 Exercises

  1. In a doubly circular linked list, insert a new node pointed by pointer q before the node pointed by pointer p. The operation of modifying the pointer is (C D.q -> llink = p->llink;q -> rlink = p;p -> llink = q;p - > llink -> rlink = q; C.q -> rlink = p; q -> llink = p -> llink;p -> llink -> rlink = q;p -> llink = q; B.p -> llink = q; p -> llink -> rlink = q; q -> rlink = p; q -> llink = p -> llink;
    A.p -> llink = q; q -> rlink = p; p -> llink -> ; link = q; q -> llink = q; ). [Note: The node structure of a doubly linked list is (llink, data, rlink)]


  2. In the doubly linked list storage structure, when deleting the node pointed to by p, the pointer must be modified (A).
    A.p->prior->next=p->next;p->next->prior=p->prior;
    B.p->prior=p->prior->prior;p->prior->next=p;
    C.p->next->prior=p;p ->next=p->next->next;
    D.p->next=p->prior->prior;p->prior=p-> next->next;


Part 4: Static linked list

1. Definition of static linked list

In situations where it is impossible or inconvenient to dynamically generate nodes, for example, there is no "pointer" data type in some high-level languages. If you want to take advantage of the linked list structure, you can use a one-dimensional array space (the array element is a structure, including data elements and The array subscript of the next element) simulates the available space table and constructs a static linked list.

2. Operation of static linked list

Insertion, deletion, modification, and search of static linked lists (collectively: addition, deletion, modification, and search)
The structure and operation of static linked lists

  • Insert operation:
    ① Starting from the array element with subscript 0, search in sequence according to the value of cursor until accessing to before the inserted position of ③ Set the cursor value of the element (for example, the cursor of a4 is set to a1 cursor); before the insertion position of is set to the cursor value of the element element to be inserted The cursor value of ② Change the (for example, a1);The previous element at the inserted position (for example, set the cursor of a1 to 3).

  • Delete operation:
    ① Starting from the array element with subscript 0, search in sequence according to the cursor value until accessing (for example, the cursor of a1 is set to the cursor of a6); is set to the cursor value of the element to be deleted to be deleted The cursor value of ② Change the element before (such as a6);Element to be deleted

  • Modify and Find operations:
    ① From subscript Starting from the array element with 0, access the array element corresponding to the subscript in sequence according to the value of cursor (for example, a6);
    ② Just modify the data value after finding the element.

Part 4 Exercises

  1. Compared with the sequential storage structure of , the static storage structure of linear table has the following advantages: (A).
    A. All operation algorithms are simple to implement
    B. Convenient for random access (static linked list simulates a singly linked list)
    C. Convenient for insertion and deletion (The advantage is that it does not require a large number of moving elements, but it cannot achieve random access, so it cannot be said to be convenient)
    D. Facilitate the use of scattered memory space (statically allocate a large area)

  2. needs to allocate a large space, and insertion and deletion of linear tables do not require moving elements. Its storage structure is (B).
    A. Single linked list
    B. Static linked list (the definition is a linked list that statically allocates space in advance) < a i=6> C. Linear linked list D. Sequential storage structure


summary

Linear table classification summary chart

Guess you like

Origin blog.csdn.net/qq_50571974/article/details/126608667
Recommended