Why do you use “array” when you have “linked list” in C language? Because you are stupid, I’m afraid you can’t learn!


1、 Talk

This article mainly shares the similarities and differences between the two memory organization types of array and linked list, so as to help you correctly understand these two data structures and apply them reasonably.


2、 Introduction to arrays and linked lists

1. Array

Array — an ordered, continuous storage structure with the same elements.



✿ same element type;

✿   Storage in sequence;

✿   It can be accessed directly through subscript.


two   Linked list

Linked list — a storage structure that is not necessarily orderly, not necessarily continuous and not necessarily the same elements.



✿   The elements are not necessarily the same, only the link information needs to exist;

✿   Memory continuity is not required;

✿   Non subscript access, traversing through link information.



3、 Similarities and differences between arrays and linked lists

one   Same point

There are few similarities. Both of them are an organization of memory data. Arrays access nodes through the characteristics of continuous allocation of the same elements, while linked lists access indexes through link relationships (generally through pointer links). (all the following array items and linked list items are collectively called nodes)

two   Different points

There are relatively few similarities, otherwise one party must replace the other, so here we focus on the differences:

1) Dynamic capacity expansion

Through the characteristics of the first two, we know that the array belongs to continuous allocation, and the given size is generally allocated at the time of definition, while the linked list can realize dynamic node insertion and removal. In this way, for some cases where the memory utilization space is changeable, using the linked list will bring more flexibility and memory utilization. As shown in the figure below:


If there are only 7 nodes in the allocated array, when you need to insert 7 nodes, you need to copy all memory to a larger memory space, and then insert 7.


In fact, there is no problem of expanding the capacity of the linked list. If the space is enough and the pointer can be indexed, it can be expanded “infinitely”.

2) Better use of cache

In the system with cache, because the CPU access speed is not in a quantitative level compared with ordinary memory, in order not to drag the CPU, the cache will be used as a buffer in the middle, which can greatly improve the speed of CPU access to main memory.


Then, as a continuous memory organization, arrays are easier to be loaded into the cache at the same time, so as to improve the hit of CPU to memory data and improve the operation speed and efficiency.

3) Access node mode

In this way, it is obvious that the array can directly access the corresponding node through the subscript, and the linked list needs to continuously traverse through the header pointer to find the corresponding node.

For example, we want to directly access the third node of the array through array [2], while for the linked list, we can constantly find the next node through the header pointer, and finally find the location of the third node. In this way, the time complexity of the linked list is greater than that of the array.


4) Save memory

The array can be accessed directly through subscript because of its fixed sequential storage format. However, for the discontinuity of the linked list, each node must store its precursor or subsequent link information, so it needs to use additional memory space for information storage. When there are many nodes, it can be a large memory expenditure.



4、 Analysis of a discussion point

When you ask about the application of arrays and linked lists, you will generally think of a sentence: “array for query and modification, linked list for insertion and deletion”. Then the bug bacteria will analyze this sentence with you here:

one   Case 1


The array in the figure above inserts a node after the fourth element. In this way, you need to move nodes 4, 5 and 6 to the back one node in turn, and then add new elements.

In the above linked list, a new node is inserted at four locations. First, you need to know the fourth node through traversal, and then directly insert the new node into the linked list by changing the pointer.

Case 1 Summary:

In this case, the complexity of the array and the linked list is not much different. The array needs to move back all nodes after the new insertion point, and then insert new nodes. For the linked list, you need to traverse to find the corresponding nodes first, and then insert them.

two   Case 2


The array in the figure above inserts a new node after data 1. First, the array needs to traverse data 1, then move 3,5,6 data nodes and insert a new node.


The array in the figure above inserts a new node after data 1. First, the linked list needs to traverse to know data 1, and then directly insert the new node.

Case 2 Summary:

Therefore, for this case, the complexity of the linked list is lower than that of the array, so the specific situation is analyzed. For example, when a new node is directly inserted into the head node, the linked list is better than the array. Just search and modify, the two are not much different, but the array is more convenient.


Final summary

After reading this article, you should have a clearer understanding of arrays and linked lists. In fact, these comparisons are very helpful for you to optimize and design some code in the future. Let’s have a good experience!


Whether you are a career change or a beginner, you can also advance——[noteworthy entry]C / C + + programming learning advanced Club

It involves: C language, C + +, windows programming, network programming, QT interface development, Linux programming, game programming, hackers and so on


An active, high-profile and high-level programmer programming learning hall; The introduction to programming is only incidental, and the improvement of thinking is valuable!