The difference between an array and a linked list

Source: Internet
Author: User
Tags in degrees

The difference between an array and a linked list
    • 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. An array should be used if the application requires fast access to the data, with little or no elements inserted and removed.

    • The list is reversed, and the elements in the list are not stored sequentially in memory, but are linked by pointers in the elements that exist. For example: The previous element has a pointer to the next element, and so on, until the last element. 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 the application needs to insert and delete elements frequently, you need to use the linked list data structure.

     * c++ The language can handle a set of data of the same data type with an array, but does not allow the size of the array to be dynamically defined, that is, the size of the array must be determined before the array is used. In practice, the user may not be able to accurately determine the size of the array before using the array, only the array can be defined to a sufficient size, so that some space in the array might not be used, resulting in a waste of memory space. list is a common form of data organization , it is implemented in the form of dynamically allocated memory. You can allocate memory space with new when you need it, freeing allocated space without the need for delete, without causing wasted memory space.
(1) from the logical structure point of view
   a,  arrays must be defined in advance to define a fixed length (number of elements), not to accommodate the dynamic increase or decrease in data. When the data increases, it may exceed the number of elements originally defined, and when the data is reduced, memory is wasted.
   B, the chain table dynamically storage allocation, can adapt to the situation of data dynamic increase and decrease, and can easily insert, delete data items. (when inserting or deleting data items in an array, you need to move other items of data)
(2) from a memory storage perspective
   A, (static) array allocates space from the stack, and is quick and easy for programmers, but small in degrees of freedom.
   B, the linked list allocates space from the heap, the freedom is large but the application management is more troublesome.

-----introduced in very detailed, simple but some places easy to forget, so turn around and share with you!

Transferred from:

--------------------------------------------------------------------------------------------------------------- -----


Personal understanding, the association of Life examples to facilitate the understanding of memory.

Array---> Like a train with a car number, a seat in each compartment, and a numbered seat, which is like a two-dimensional array.

The train is fixed between each section, it is not easy to split into the new compartment, if the new compartment is inserted, the affected car will have to change the number, which is much like modifying the array of the foot tag.

List---> It's more like we're in line, just remember who I'm behind and who's behind me. Pointer-based for easy insertion and deletion of data. However, the contents of linked list connections can be discontinuous in memory because they depend on pointers.

The difference between an array and a 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: 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.