Data structure of block linked list _ data structure

Source: Internet
Author: User
1. Overview

In the design of the algorithm, we commonly used two linear data structures are arrays and linked lists. They each have advantages and disadvantages. The array feature is that the elements are stored next to each other in memory, so the advantage is to locate fast (O (1)), the disadvantage is that the insert deletion is slow (o (n)), and the linked list is different, and it links the elements of different positions through the pointer, so the pros and cons are the opposite of the array: Slow positioning (O (n)), insert delete Fast (O (1)). This paper introduces a new data structure: block linked list, which combines the advantages of array and list, and the time complexity of various operations is O (sqrt (n)).

2, the basic operation of block linked list

The block linked list integrates the advantages and disadvantages of arrays and lists, which makes the time complexity of each operation an O (sqrt (n)). On the whole, a block list is a linked list, and on each node of the list, a set of elements is stored as an array. Specifically as follows:

Basic operations:

(1) Positioning: First locates the linked list node where the element is located, and then locates the element's position in the array.

(2) Splitting: Splitting a linked list node into two nodes.

(3) Insert: First locate the position to insert, then split the node into two nodes and place the data at the end of the first node. If you want to insert a large chunk of data, first cut the data into multiple blocks (each block corresponds to one node of a blocky list) and chain them up, then insert them between the two nodes.

(4) Delete: Locate the deletion element first, and then delete the data by the way the array deletes the element. If you delete a large chunk of data, you first locate the first and last elements of the block, then split the nodes into two nodes, and then delete the nodes between the first and last elements.

3, key points and complexity analysis

The core of the algorithm is to determine the length of the linked list and the length of each node, and how to guarantee the length value. The total number of elements in a block list is X, the list length is n, and the length of the data in each node is M, then when M=n=sqrt (X), M and n are guaranteed to be the smallest while the time complexity of various operations is the lowest. In practical applications, it is necessary to maintain the size of each node in the block list at [sqrt (n)/2, 2*SQRT (n)], otherwise the block linked list will degenerate. The maintenance method is, when appropriate, to merge and split the nodes (the maintenance itself does not increase the complexity).

4. Application

(1) Text editor design: http://download.noi.cn/T/noi/noi2003A.pdf

Program Implementation Reference:

Http://hi.baidu.com/wuyijia/blog/item/7085fa004423cd86e850cdeb.html

(2) Maintenance queue: Http://download.noi.cn/T/noi/noi2005A.pdf

Program Implementation Reference:

Http://hi.baidu.com/zbwmqlw/blog/item/54cce1004e799c0c1d9583b7.html

5, summary

Because each node of a block list stores an array, if the array is static (the size is fixed), the storage space is wasted, and if it is dynamic, it requires frequent application or free space, severely reducing the system energy. Therefore, when the data volume is very large, it is very important to design a reasonable maintenance strategy of the array space.

6. Reference materials

(1) A study of the block list

(2) array + linked list = block array: http://starforever.blog.hexun.com/3184024_d.html

————————————————————————————————————-

For more information on data structures and algorithms, please see: Data structure and algorithm summary

————————————————————————————————————-

Original articles, reproduced please specify: Reprinted from Dong's Blog

This article link address: http://dongxicheng.org/structure/blocklink/

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.