Sequential storage of linear tables and chain storage differences __ Data structure

Source: Internet
Author: User

The sequential and chained storage of linear tables has different time complexity in storing and deleting data.

Typical examples of sequential storage are arrays, and a typical example of chained storage is a single linked list. As we all know, when the reading data is more frequent, we choose the sequential storage mode, when inserting and deletion operation is more frequent, we choose the chain type storage way. Next, we will analyze the reason for doing this:

1. Sequential storage:

1 when reading the data, read the elements of any position I, can be obtained by a[i], therefore, the time complexity of O (1).

2 when inserting and deleting data, it is impossible to always deal with the last element, for any other position I corresponding to the processing of elements, if inserted, you need to move the elements after i from the last element to the beginning of the translation of a unit, if deleted, you need to move the elements after I from the I+1 elements are moved forward a unit. Therefore, the time complexity is O (n).

2. Chained storage:

1 when reading the data, read the elements of any position I, all need to start back through the A1, until the AI, therefore, the time complexity of O (n).

2 when inserting and deleting data, it also needs to be traversed from A1 to the AI, so the time complexity is also O (n).

So here's the problem, there is no doubt that, from the time complexity, read data selection time complexity of O (1) sequential storage mode, but, insert and delete operation, the time complexity of both storage mode is O (n), why choose the chain of storage structure.

This is mainly because, when multiple data is inserted or deleted continuously in one location, for the cis-vinegar storage structure, the time complexity of each operation is O (n), and for the chain storage structure, only the first time complexity is O (n), after each operation time complexity is O (1), so we say, When inserting and deleting frequently, select a chain-type storage structure.

In addition, it is worth mentioning that the sequential storage structure has the following advantages and disadvantages:

Advantages:

1 No additional storage space is added because the logical relationship between elements is to be represented.

2 quick to save any position elements in the table.

Disadvantages:

1 inserts and deletes need to move a large number of elements.

2 The capacity of the storage space is difficult to determine when the linear table length changes greatly.

3 It is easy to create fragments of storage space.



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.