The advantages and disadvantages of the [data structure] array and the linked list

Source: Internet
Author: User

Overview

Arrays are contiguous in memory, and because each element occupies the same memory, any element in the array can be quickly accessed by subscript. But if you want to add an element to an array, you need to move a large number of elements, empty the space of an element in memory, and then put the added elements in it. Similarly, if you want to delete an element, you also need to move a large number of elements to fill out the moved element. If your app requires fast access to data, and you rarely insert and delete elements, you should use arrays.

The elements in the list are not stored sequentially in memory, but are linked by pointers in existing elements, each of which consists of two parts: one is the data field that stores the data element, and the other is the pointer that stores the next node address.
If you want to access an element in a linked list, you need to start with the first element and always find the desired element position. But adding and removing an element is very simple for a linked list data structure, as long as you modify the pointer in the element. If your app needs to insert and delete elements frequently, you'll need to use a linked list .

Memory Storage Differences
    • The array allocates space from the stack , which is quick and easy for programmers, but small in freedom.

    • The linked list allocates space from the heap , the freedom is large but the application management is more troublesome.

Logical Structure Differences
    • The array must define a fixed length (number of elements) beforehand, and it cannot accommodate the dynamic increase or decrease of the data. When the data increases, it may exceed the number of elements originally defined, and when the data is reduced, memory is wasted.

    • The dynamic storage allocation of the linked list can adapt to the dynamic increment and decrement of data, and can easily insert and delete data items. (when inserting or deleting data items in an array, you need to move other data items)

Summarize

1, access mode, the array can be sequentially accessed or random access, and the list can only be sequentially accessed;

2, the storage location, the array logically adjacent elements in the physical storage location is also adjacent, and the linked list is not necessarily;

3, the storage space, the linked list because with the pointer field, storage density is not as large as the array;

4, by ordinal search, the array can be randomly accessed, the time complexity of O (1), and the list does not support random access, the average need O (n);

5. When searching by value, if the array is unordered, the array and the time complexity of the list are O (1), but when the array is ordered, the time complexity can be reduced to O (logn) with binary lookup;

6, insert and delete, the average array needs to move N/2 elements, and the linked list only need to modify the pointer;

7. Space allocation:
Array in the case of static storage allocation, the number of storage elements is limited, dynamic storage allocation situation, although storage space can be expanded, but need to move a large number of elements, resulting in reduced operational efficiency, and if there is no larger block of contiguous storage space will lead to allocation failure;
The node space stored in the list is allocated only when needed, as long as there is space in the memory can be allocated, the operation is more flexible and efficient;

The advantages and disadvantages of the [data structure] array and the linked list

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.