Advantages and disadvantages of the array, and a linked list

Source: Internet
Author: User

A contiguous space in memory. List, memory address can be discontinuous, each node of a linked list contains the original memory and the next node information (one-way, two-way list, there will be two).

Arrays are better than linked lists:
1. Memory footprint is small, because the linked list node appends the information of the next node or two block. But the array is fixed when it is established. Therefore, it is also possible to cause memory problems due to the large or insufficient array of established arrays.
2. The data within the array can be visited randomly. But the list does not have random access. This is very easy to understand. Arrays are contiguous in memory. For example, suppose an array address is from 100 to 200, and each element occupies two bytes, So whatever an even number between 100-200 is the address of the array element. Be able to visit directly. The list may be scattered in the memory address. So you have to find the next node through the information in the previous node.
3. Find the speed. This is also due to the memory address continuity problem. No, No.
A linked list is better than an array:
1. Insert and DELETE operations. Assuming that an element is inserted in the middle of the array, the memory address of all the elements behind the element will be moved backwards. The same is the case with the deletion. Only when the last element of the data is inserted and deleted. It's quicker than that. The list simply needs to change the node information within the node that is necessary to change it. There is no need to change the memory address of the node.
2. The utilization aspect of memory address. No matter how much space you have in your memory, assuming there's no way to give the array the space you need, you'll be prompted for one of the reasons why there is not enough memory to defragment the disk. The linked list can be a decentralized space address.
3. The list is more scalable than an array. Since an array is built, the amount of space that is occupied is fixed. If it is full, it cannot be extended. You can simply create an 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 than linked list high. Memory is a contiguous area

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

Advantage: can dynamically join delete size variable
Cons: Only through the sequential pointer access, query efficiency is low

Add:

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

The advantages of linked tables: INSERT, delete operation is not 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 fast to add deleted elements anywhere in the middle. There is no need to move other elements.
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 list 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 element of the array has a number, which is called subscript, and we can use subscripts to vary 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. It is easy to insert and operate.
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 the interview
The array can randomly access the elements in the
Linked list must be sequential access, cannot be randomly interviewed

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

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Advantages and disadvantages of the 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.