Both data structures are linear tables and are widely used in algorithms such as sorting and finding: arrays:
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.
Linked list:
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.
The difference between an array and a linked list: 1. From a logical structure point of view:
A, the array must be defined in advance fixed length (number of elements), can not adapt to the dynamic increase or decrease in data situation. When the data increases, it may exceed the number of elements originally defined, and when the data is reduced, memory is wasted.
b, the chain table dynamically storage allocation, can adapt to the situation of the data dynamic increase and decrease, and can easily insert, delete data items. (when inserting or deleting data items in an array, you need to move other data items)
2. Array elements in the stack area, linked list elements in the heap area; 3. From a memory storage perspective:
A, (static) array allocates space from the stack, which is convenient and quick for programmers, but small in freedom.
b, the list allocates space from the heap, the degree of freedom is large but the application management is more troublesome.
The array uses subscript to locate, time complexity is O (1), the link list locates element time complexity O (n);
The time complexity of an array to insert or delete elements O (n), the time Complexity O (1) of the linked list.
Interview Road (8) The difference between the array and the list of-bat questions