The difference between an array and a linked list

Source: Internet
Author: User

1, in the allocation of space:
Array is allocated in a contiguous data space, so in allocating space must determine the size, the list is a discontinuous dynamic space, through the pointer to practice the connection, so the length is variable;
2. Arrays
Advantages:
1, can use the offset address to access the element, high efficiency, O (1);
2, can use binary method to find elements, high efficiency;
Disadvantages:
1. Continuous space, low storage efficiency
2, the insertion and deletion of elements of low efficiency, and more trouble;
3. Linked List
Advantages:
1, insert and delete elements do not need to move the remaining elements, high efficiency, for O (1);
2. No continuous space required, high efficiency of space utilization
Disadvantages:
1. Do not increase the limit of random access elements
2, find elements and search elements of inefficient, the fastest situation is O (1), the average is O (N);
Therefore, for frequently inserted and deleted operations, the data structure uses a linked list or a two-fork search tree;

Linked lists and arrays can be used to hold the specified data type.
First, we introduce the linked lists and arrays separately.
The characteristic of a linked list is that it is very fast to add deleted elements anywhere in the middle, and no other elements need to be moved. Typically, each element of a linked list holds a pointer to the next element (a single-linked list). Double-linked lists each element is a pointer to the next element, and a pointer to the previous element is saved. The Loop list saves the last element with the next element pointer pointing to the first element.
An array is a set of variables that have the same type and name. These variables are called array elements, each array element has a number, this number is called subscript, we can use subscript to distinguish these elements. The number of array elements is sometimes referred to as the length of the array.

Arrays are storing elements in memory continuously, because each element occupies the same memory, so you can quickly access any element in the array by subscript. But if you want to add an element to the 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.

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 location of the element you need. 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.

From the above comparison you can see that if your application requires fast access to data, little or no insertion and deletion of elements, you should use arrays; Conversely, if your application needs to insert and delete elements frequently you need to use a linked list data structure. Then you can think about what kind of app is appropriate for your list.

The C + + 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.

A linked list is a common form of data organization, which is implemented by dynamically allocating 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.


In terms of the logical structure
A-1. 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, overflow, and when the data is reduced, the memory is wasted. When inserting or deleting data items in an array, you need to move other data items.

A-2. 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.

From a memory storage perspective
B-1. (static) array allocates space from the stack, easy and fast for programmers, but with little freedom
B-2. The linked list allocates space from the heap, and the degree of freedom is large but the application management is cumbersome.

This is the one-way linked list and static array, the following is a brief introduction to the doubly linked list and dynamic array.
Dynamic arrays are relatively simple, which is an array whose length can be changed according to the actual situation. If we are looking for an element in a dynamic array, we can find it through the Get () method, as long as we know that the element is subscript.
In addition to the original data, the two-way linked list has its precursor and post-drive junction.

In a dynamic array, if we want to add or delete an element in one place, each of the remaining elements moves forward or backward accordingly. If there are many elements in the dynamic array, then every time we add or delete an element, we need to move a lot of elements, so the efficiency is relatively low.

Two-way list efficiency is much higher. If we want to add an element in one place, for example, to insert 5 between 1 and 3. Originally 1 was pointing to 3,3 also pointing to 1. Now, just put 5 between 1 and 3, and let 5 point forward to 1, backwards to 3, and 1 to 3 from 5, so 3 points 1 from 5. If there are so many elements in the list, we just have to do this and we don't need to move the rest of the elements.

Therefore, a doubly linked list is much more efficient than a dynamic array in adding and deleting elements. In Java, the system provides both two-way linked list (LinkedList) and dynamic Array (ArrayList) mechanisms. Also, there is an iterator called Listiterator in Java. The iterator can not only iterate over the elements backwards, but also iterate forward, and add () is handy for adding elements at a certain location. However, the efficiency of the search is reversed, and the efficiency of the dynamic array is higher than that of the doubly linked list, as you simply provide the subscript of the element.

Note: The list of links is distributed in memory, so the base address registers of the CPU and so on must be re-assigned, and the process of the array is not. As a result, arrays are more efficient for queries.

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: 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.