Data structure review-linear table concept (the difference between sequential table and linked list)


1 The meaning of linear table

   A finite sequence of data elements with the same characteristics is called a linear table. Linear table is the most basic, simplest, and most widely used data structure.


2 Linear table characteristics

  • There must be a unique "first element" in the set

  • There must be only one "last element" in the set

  • Except for the last element, there is a unique successor

  • Except for the first element, there is a unique precursor

  • Simple logical structure, easy to realize and operate


3 Linear table composition

  Linear tables are classified into sequential storage and chained storage according to the storage structure type of data elements. These two types correspond to sequential tables and linked lists (linked lists) respectively. The element addresses of the sequence table are continuous, and the element addresses of the linked list are non-continuous. Linked lists can be further divided into single linked lists, double linked lists, circular linked lists, and static linked lists.


Insert picture description here

4 Sequence table and linked list

4.1 own characteristics

  Contrast and contrast the characteristics of sequence list and linked list.

Sequence table Linked list
Storage address continuous non-continuous
storage Static allocation Usually dynamically allocated
Storage density =1 <1
Access structure Random access and sequential storage Sequential access
Find time complexity O (1) O (n)
Insert and delete time complexity O (n) O (1)

  By comparing the sequence table and the chain table, the main difference between the two is reflected in the storage structure.

  The elements of the sequence table are stored in a continuous storage space, and their characteristics are:

  • Search by index number, high efficiency
  • Insertion and deletion are inefficient because of the need to move the elements before and after
  • Only store valid data, storage density is 1, high storage utilization
  • Usually static memory allocation is used, which is prone to scenes of wasting memory or insufficient memory, and memory usage is not flexible


    Insert picture description here
    Sequence table storage structure

  The element storage space of the linked list is non-continuous, and its characteristics are:

  • Through node search, the efficiency is low.
  • High efficiency of inserting and deleting, no need to move elements, just change the node pointer
  • In addition to storing valid data, it also needs to store node address relationship information, and the storage density is less than 1
  • Usually use dynamic memory allocation, memory usage flexibility is good


    Insert picture description here
    Linked list storage structure

4.2 Applicable scenarios

  By comparing the characteristics of linear watch and chain watch, the advantages and disadvantages of the two complement each other, and the applicable scenarios are different.

Application scenarios for linear tables:

  • Frequent search static operation
  • Linear table length is fixed

Application scenarios of linked list:

  • Frequent insert and delete dynamic operations
  • The length of the linear table is not fixed and changes greatly

4.3 Derivation

  The sequence table (array) in the C language does not support dynamic memory allocation. Therefore, the memory usage is not flexible enough, and the length of the table is also fixed. In the C++ language, the sequence table supports variable length, that is, supports dynamic memory allocation. Such as STL sequential container <vector>. <vector>The principle is to first open up a section of memory to store the collection of elements; when the collection is full, when new data is inserted, a larger memory space is allocated, and the original data is copied to the memory space, and then released The memory previously requested, and finally the new element is inserted.

Guess you like

Origin blog.csdn.net/qq_20553613/article/details/108010894