Data structure learning_04_The difference and advantages and disadvantages of sequential table and linked list in linear table

What is a sequence table?
The sequence table is a linear table stored in the form of an array in the computer memory. Sequence table storage is to put data elements in a continuous memory storage space, and the storage addresses of adjacent data elements are also adjacent, which is logical and physical.
What is a linked list?
A linked list is a non-contiguous, non-sequential storage structure on a physical storage unit, and the logical sequence of data elements is achieved through the link order of pointers in the linked list. The linked list is composed of a series of nodes (each element in the linked list is called a node), which can be dynamically generated at runtime.
Node: Each node contains two parts: one is the data field for storing data elements, and the other is the pointer field for the address of the next node.
Comparison of advantages and disadvantages:
1. Time
1) The time complexity of accessing random elements.
Because the structure of the sequence table is the same as that of an array, you can use subscripts to access its elements, so the sequence table supports random access; compared to singly linked lists Its elements in chain storage do not support random access. If you want to find an element, you can only traverse the entire linked list from the beginning node until the element is found. Therefore, the time complexity of accessing random elements in a sequential table is O(1), while the time complexity of accessing random elements in a singly linked list is O(n).
2) The time complexity of inserting and deleting elements at random positions:
because the elements of the sequential table are It is stored continuously, so inserting or deleting an element at a specific position requires moving all the elements after it back or forward, which costs a lot of time. When inserting or deleting an element in a singly linked list, you only need to change the point of its predecessor element. Therefore, the time complexity of inserting and deleting at random positions in the sequence table is O(n), while the singly linked list is O(1).
2. Space:
when we don’t know how much data to store, if the space opened up with the dynamic sequence table is too large, it will cause a certain degree of waste, and at this time, when using a singly linked list, several developments are needed. , Although there are non-data pointers occupying space, it is acceptable compared to the sequence table.
When we know the amount of data stored, use the sequence table to open up the corresponding space size to store the data. Because the storage density of each element in the sequence table is 1, there will be no wasted space at all, and a singly linked list , Because each node will have pointers to non-data items, it will cause a waste of space. Furthermore, the compiler will allocate a section of memory from the memory for each program for use by the program. However, every time we open up a space, we open it in a random location, so if we use a singly linked list, we will open up space in the space allocated by the program many times, because every time we open up the space, the location is random. , Then this space may be broken up, and there will be many small fragmented spaces that are generally not used, which largely causes a waste of space. If you use a sequence table, you will not often open up space. This reduces the appearance of fragmented space, and then saves space to a certain extent.
3. The impact on the CPU cache.
Because the space of the sequence table is generally opened up continuously, and the space for storing multiple elements will be opened up at a time, so in use In the sequence table, multiple data can be written into the cache at a time, and then written to the main memory. The CPU cache efficiency of the sequence table is more efficient, and the CPU pipeline will not always be interrupted; while the singly linked list needs to be stored every time One piece of data only opens up space once, so each data must be separately written into the cache area and then into the main memory when storing it. This causes the singly linked list CPU cache efficiency to be low, and the CPU pipeline will often be interrupted

So what are the usage scenarios of the two?
*Frequent search but few insertion and deletion operations can be stored in sequential table, heap sorting, binary search is suitable for sequential table.
*If frequent insertion and deletion operations are few queries, you can use linked list storage
*Sequential table is suitable Do static operations like lookup; linked lists are suitable for dynamic operations like insert and delete.
*If the length of the linear table does not change much, if the approximate length of the linear table is known in advance, for example, 12 months a year, a week is seven days from Monday to Sunday, and its main operation is search, then the sequence table is used; if the length of the linear table changes When it is large or does not know how large it is, and its main operations are insertion and deletion, a linked list is used, so there is no need to consider the size of the storage space.
*Sequence table: sequential storage, random read
chain: random storage, sequential read (must traverse)

Guess you like

Origin blog.csdn.net/CZHLNN/article/details/112233705