Advantages and disadvantages of arrays and linked lists

Source: Internet
Author: User

array , a continuous space is given in memory. Linked list, memory address can be discontinuous, each node of the list includes the original memory and the next node information (one-way, doubly linked list, there will be two).   the

array is better than the linked list:  
1. The memory footprint is small because the linked list node attaches the information of the next node on a block or two. But the array is fixed when it is built. Therefore, it is possible that the array is too large or insufficient to cause memory problems.  
2. The data within the array can be accessed randomly. But the list does not have random access. This is easy to understand. Arrays are contiguous spaces in memory. For example, if an array address is from 100 to 200, and each element occupies two bytes, Then any even number between 100-200 is the address of the array element. can be accessed directly. The linked list may be scattered in the memory address. Therefore, the information in the previous node must be found to find the next node.  
3. Find speed. This is also because of the memory address continuity problem.  
The list is better than the array:  
1. Insert and delete operations. If an element is inserted in the middle of an array, Then the memory address of all the elements behind this element will be moved backwards. The same is the case with delete. Only the last element of the data is inserted and deleted. The list only needs to change the node information within the node that is necessary to change it. You do not need to change the memory address of the node.  
2. The utilization aspect of the memory address. No matter how much space you have in memory, if there is no way to give the array the required space, then you will be prompted for insufficient memory, one of the reasons for disk space consolidation here. The linked list can be a decentralized space address.  
3. A linked list is better than an array. Because the amount of space that an array is built upon is fixed. If it is full, it cannot be extended. You can only create a new array of larger spaces. The list is not fixed and can be easily extended.

The advantages and disadvantages of array and linked list;
Array:

Advantages: Easy to use, query efficiency is higher than linked list, memory is a contiguous area

Cons: Fixed size, not suitable for dynamic storage, inconvenient to add dynamically
Linked list:

Pros: Dynamic Add Delete size variable
Cons: Only accessible by sequential pointers, low query efficiency

Add:

Advantages of sequential tables: easy to find, suitable for random search
Disadvantages of the sequential table: INSERT, delete operations are inconvenient because INSERT, delete operations cause a large number of elements to move

Advantages of linked tables: INSERT, delete operation is convenient, does not cause the element to move, because the element increases or decreases, only need to adjust the pointer.
Disadvantages of the sequential table: easy to find, not suitable for random search

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.
As the name implies, linking the elements is only a scatter.
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 (Baidu) with 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.
Array in the search, sorting operation is very convenient, the list does not need continuous space, and for easy insertion operation!
The allocation space is also different, the establishment process is also different!

The essential difference between a linked list and an array

1 on the way to access
The array can randomly access the elements
Linked list must be sequential access, not random access

2 Use of space
The list can be expanded arbitrarily
Array is not

Original link http://blog.csdn.net/kunikida/article/details/7657501

Advantages and disadvantages of arrays and linked lists

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.