Advantages and disadvantages of sequential or linked table Storage for linear tables

Source: Internet
Author: User

Sequential StorageIt indicates that data elements are stored in a continuous bucket for sequential access or direct access (by subscript. It features high storage efficiency and fast access. However, once defined, the size of its space does not change during the entire operation of the program, so it is not easy to expand. In additionTo keep the original order (There is no rule on the sequence of elements going into the stack), The average needs to move half (or nearly half) of the elementThe modification efficiency is not high.

Linked StorageThe storage space is generally dynamically allocated and released during the running process of the program, and as long as there is space in the memory, there will be no storage overflow problem. At the same time, you do not need to keep the original data elementPhysical order, You only need to keep the originalLogical OrderTherefore, you do not need to move data. You only need to modify their link pointers, which is more efficient. However, when accessing data elements in a table, you can only access them in the chain order, so the access efficiency is not high.

 

1 Comparison of time performance between sequence tables and linked lists

The so-called time performance refers to the time complexity of implementing basic operations (that is, algorithms) based on this storage structure.

Like extracting the I-th element of a linear tableRandom Access by locationThe sequential table is faster. operations on the forward and next nodes can easily adjust the current position in the ordered table to forward or backward. Therefore, the time for these two operations isO(1); in contrast, a single-chain table cannot directly access the above elements. Access by location can only start from the header until a specific location is found. The average time required isO(N).

Given a pointer pointing to a proper position in the linked list,Insert and delete operationsThe time required is onlyO(1) While the insert and delete operations on the sequence table require moving nearly half of the table's length, the average time required isO(N). This requires a large number of elements in a linear table. In particular, when each element occupies a large amount of space, the time overhead of moving elements is high. For many applications, insertion and deletion are the most important operations, so their time efficiency is crucial. For this reason alone, linked lists are often better than sequential tables.

 

As a general rule, if a linear table needs to be searched frequently but seldom inserted or deleted, or its operations are closely related to the position of data elements in a linear table, sequence tables should be used as storage structures. If linear tables need to be inserted and deleted frequently, linked lists should be used as storage structures.

2 Comparison of sequential table and linked table space Performance

The space performance refers to the size of the storage space occupied by the storage structure.

First, defineStorage Density.


In an ordered table, each element has a storage density of 1, which does not waste space. In addition to storing data elements, each node in the linked list also has a pointer to indicate the logical relationship between elements, if the data domain occupies a small amount of space, the structural overhead of the linked list occupies most of the storage space. Therefore, in terms of the storage density of nodes, the storage space usage of the sequence table is high.

Because a sequence table needs to pre-allocate a certain length of storage space, if you do not know the approximate length of the linear table in advance, it is possible to pre-allocate a large amount of storage space, as a result, a large part of the storage space is not fully utilized during program execution, resulting in waste. If it is estimated that it is too small, it will cause frequent redistribution of the storage space. One of the notable advantages of a linked list is its flexibility in storage allocation. You do not need to pre-allocate space for the linked list. As long as there is available memory space allocation, there is no limit on the number of elements in the linked list.

 

As a general rule, when the number of elements in a linear table changes significantly or is unknown, it is best to use a linked list. If you know the approximate length of a linear table in advance, the space efficiency of a sequential table is higher.

 

In short,The sequential implementation of a linear table and the implementation of a linked list have their own advantages and disadvantages. We cannot say in general what kind of implementation is better. We can only balance the advantages and disadvantages of various aspects according to the specific needs of the actual problem, in order to finally select a more appropriate implementation method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.