Interview questions (1)-the difference between an array and a linked list

Source: Internet
Author: User

The difference between an array and a linked list:

Both belong to a data structure
In terms of the logical structure
1. The array must define a fixed length (number of elements) in advance, and it cannot adapt to the dynamic increase or decrease of data . When the data increases, it may exceed the number of elements originally defined, and when the data is reduced, the memory is wasted; the array can be accessed directly from the subscript.

2. The dynamic storage allocation of the linked list can adapt to the situation of the data increment and decrease dynamically , and can easily insert and delete data items. (when inserting and deleting data items in an array, it is tedious to move other data items) The list must find the next element according to the next pointer


From a memory storage perspective
1. (static) array allocates space from the stack, easy and fast for programmers, but with little freedom
2. linked list from the heap allocation of space , large degrees of freedom but the application of management is more troublesome
As can be seen from the above comparison, if you need to quickly access the data, little or no elements are inserted and deleted, you should use the array, conversely, if you need to insert and delete elements often need to use linked list data structure.

The *c++ language can handle a set of data of the same data type in 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.

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.

Interview questions (1)-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.