python algorithm - arrays and linked lists

Copyright: Shallow @ Copyright original article may not be reproduced without permission https://blog.csdn.net/xili2532/article/details/90611043

Arrays and linked lists

The array having the same aggregate data type and a set of variables arranged in a certain order, these variables including the array element of an array called
the array address in memory are continuously adjacent, and linked list address in the memory is bulk column, a discontinuous
array is stored in memory element continuously, since the same memory for each element can be accessed by any element in the array subscripts rapidly. However, if an element in the array to increase, the need to move a large number of elements, the memory space in a hollow element, the element to be added and then placed therein. By the same token, if you want to delete an element, you also need to move large amounts of elements to be moved to fill out the elements. If the application requires fast access to data, with little or no insert and delete elements, you should use an array.
List the contrary, the list of elements is not sequentially stored in memory, but is linked together by the presence of a pointer element. For example: the element has a pointer pointing to the next element, and so on, until the last element. If you want to access a list of elements need to start from the first element, the element has been to find the desired position. But adding and deleting an element for the linked list data structure is very simple, as long as the modified elements pointer on it. If the application requires frequent insert and delete elements you will need to use a linked list data structure.
Add an element in the list is easy: just put it in memory, and stores it in an address to the previous element.

(1) From a logical point of view the structure of
   a, the array must be defined fixed length (number of elements), the case can not adapt to dynamic changes in the data. When the data is increased, the number of elements may exceed the previously defined; when the data is reduced, resulting in wasted memory.
   B, list dynamically allocated storage, the data can be dynamically increased or decreased adaptation, and easy to insert, delete data items. (When inserted into the array, delete a data item, the other data items need to move)
(2) from the viewpoint of the storage memory
   A, (static) array from the stack space allocated, fast and easy for programmers, but a small degree of freedom.
   b, the list allocated from the heap space, but a large degree of freedom to apply management is too much trouble.

Differences arrays and linked lists

When using a linked list. When you need to read the last element of the list, you can not directly read, because you do not know the address of which it is located, you must first access element # 1, derive element # 2 address, and then access the elements and to obtain from # 2 address element # 3, and so on, until the last element visit. You need to read all the elements at the same time, the high efficiency of the list: you read the first element, the address of which is re-read the second element, and so on. But if you need to jump, the efficiency of the linked list is really low.
Unlike this array: Do you know where the address of each element. For example, suppose you have an array that contains five elements, from the
start address is 00, then the address element # 5 is how much? Just perform simple math to know: 04. When required random read element, an array of highly efficient, since quickly find any element of the array. In the list, the elements are not close together, you can not quickly calculate the memory address of the fifth element, and must have access to the first element to get the address of the second element, and then access the second element for the first address three elements, and so on until the fifth element of access.

Arrays and linked lists usefulness

Arrays and linked lists which use a lot more of it? Obviously it depends on the circumstances. But the array used a lot, because it supports random access. There are two
visits ways: random access and sequential access . Sequential access means that read elements from the first element to start one by one. List only
able to sequential access: To read the tenth element of the list, you must first read the first nine elements, and find the tenth element along the link. Random
access means that can jump directly to the tenth element. Often said array of read speed faster, because they support random access
ask. Many cases have to be capable of random access, so the array used a lot.

Summary arrays and lists:
 Computer memory is like a lot of drawers.
When  need to store a plurality of elements, an array or linked list may be used.
 element array all together.
 list elements are separated, wherein each element stores the address of the next element.
 array of read speed quickly.
 list insertions and deletions fast.
 in the same array, of all elements must be the same (are int, double, etc.)

Guess you like

Origin blog.csdn.net/xili2532/article/details/90611043