Application scenarios for commonly used data structures

Source: Internet
Author: User

1. One-way link

A one-way list is intended for applications where only one-way access is available, typically in this scenario:

(1), when deleted, only suitable to delete the first element;

(2), when added, is added directly after the last element or added to the front of the first element;

(3), belonging to a one-way iterator, can only go from one Direction to the head (forward or backward only, depending on the implementation), the search efficiency is very poor. A situation that is not suitable for large queries.

The typical application scenario is the implementation of various buffer pools and stacks.

2. Doubly linked list

The doubly linked list has forward and back two pointer addresses compared to one-way linked lists, so it is suitable for the following occasions:

(1), delete, you can delete any element, and only need a very small cost;

(2), when added, it requires minimal overhead when it knows the elements of its previous or next position.

(3), belongs to bidirectional iterators, can go from head to tail or from the end of the head, but the same search needs to traverse, efficiency and unidirectional linked list no improvement, not suitable for a large number of queries.

This typical application scenario is a variety of data list management that does not need to be sorted.

3, array (including dynamic array in Delphi), list (tlist in delphi/c++ builder) vector (std::vector in C + +)

This data structure uses a contiguous space to store elements, so that an element can be obtained directly from the index, and the content of the element can be sorted and then searched using a dichotomy to provide a lookup efficiency. The most suitable occasions are:

(1), do not delete the element frequently, because the elements are involved in the redistribution of element space, frequent memory allocation operations will greatly reduce operational efficiency. However, when adding an operation, you can optimize the efficiency of the addition by pre-allocating enough space.

(2), belonging to a random iterator, random access to arbitrary elements. It is more efficient to find sorted elements.

4, binary tree (including red black tree, balanced binary tree, etc.)

This data structure is similar to a doubly linked list, which is automatically sorted when any element is inserted, and the red black tree and the balanced binary tree make the two fork tree as balanced as possible, making the query time similar to the dichotomy method. It is suitable for the occasion mainly:

(1), it is necessary to ensure the orderly arrangement of the list elements;

(2), the need for frequent additions and deletions and query operations;

(3), belong to bidirectional iterators, can not randomly access arbitrary elements;

5. Hash bucket

This data structure uses arrays and linked lists to manage elements and, with the support of good bucket sizes and hashing algorithms, can ideally achieve random access efficiency near arrays. The most suitable occasions are:

(1), do not need to guarantee the order of the elements (because it is determined by the hash value inserted into the bucket, regardless of the original data content);

(2), the need for frequent additions and deletions and query operations;

(3), one-way or two-way iterators (depending on the implementation), can not randomly access arbitrary elements.

Application scenarios for commonly used data structures

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.