What is the difference between a linked list and an array?

Source: Internet
Author: User

I have always been confused: What is the difference between a linked list and a number? The array is a linear structure and can be directly indexed, that is, to go to the I element, a [I. The linked list is also a linear structure. To obtain the I-th element, you only need to use the pointer to traverse the I. It seems that the linked list is more troublesome than the array, and the efficiency is lower.

Thinking of some subtle differences in these similarities, their real differences gradually become apparent: Why are linked lists less efficient than arrays? Start with the initialization of both. Array does not need to be initialized, because the array elements are in the memory stack area, the system automatically applies for space. The node element of the linked list is in the heap area of the memory. Each element must be manually applied for space, such as malloc. That is to say, the array is static memory allocation, while the linked list is dynamic memory allocation. Why are linked lists so troublesome? Cannot an array completely replace a linked list? Back to this question, just think about how we completed the student information management system. Why should linked lists be used at that time? Because the insert and delete operations in the student management system are flexible, while the array is fixed in size and cannot be inserted or deleted flexibly and efficiently. Because the heap operation is more flexible. Each time an array inserts an element, it needs to move the existing element, and the linked list element is on the heap, so it does not need to be so troublesome.

The differences between arrays and linked lists are as follows:

Static Memory Allocation in arrays and dynamic memory allocation in linked lists;

The array is continuous in the memory, and the linked list is not continuous;

Array elements are in the stack area, and linked list elements are in the heap area;

The array uses subscript positioning. the time complexity is O (1), and the time complexity O (n) of the positioning element in the linked list );

The time complexity O (n) of inserting or deleting elements in an array, and the time complexity O (1) of 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.