Speaking of linked list, the first reaction: linked list is a data type! It can be used to store multiple batch data of the same type.
With this understanding, it is easy to associate array. It is also a data type and can also be used to store batch data of the same type. Beginners often have a good impression of arrays. For example, if you want to save a batch of integers, you can define a one-dimensional shaping array, which is simple, intuitive and convenient to store with arrays.
Then why introduce linked lists?
The main reason here is that arrays have disadvantages: first, arrays are statically allocated.
What do you mean? In other words, if you want to use arrays in your program, you must first define the array and write a description statement before executing the statement. Our variables are defined first and then used. In the part of this description statement, the length of the array has been sentenced to death.
Some students may have questions, just static. What’s the problem? The problem is that in some cases, you cannot determine the approximate size of the data, or the data size needs to be adjusted accordingly for different users.
For example, for a student management system, if the data is stored in the form of array: if the user is a class, 100 units are enough; For a department, 1000 units may be required; For several departments, there may be tens of thousands of units.
If you want your system to meet the needs of these users at the same time, you should adopt the largest scale, that is, you should make the scale of the array 10000 in the description statement. As long as variables are defined, the system will allocate storage units.
In practical application, even if only one unit is used, the system will allocate 10000 units. The redundant unit program will not be used, and other variables can not be used. It is typical and occupies nothing.
Another drawback of arrays is that they must use contiguous memory units. For a tour guide who leads 100 tourists to stay in a hotel, if the array method is adopted, if the number of empty rooms in the hotel is far more than 100, but there are no continuous 100 rooms in any place.
That’s more embarrassing. The landlady has to remind you that the system memory space is not enough. She won’t let you stay in the store or let your program execute.
Then in the future, we can skillfully solve these two problems by introducing the linked list: first, our linked list is not defined in advance before the execution of the statement, but can be dynamically allocated according to the needs of users, which is more flexible and efficient; Secondly, the use of linked list also requires memory units, but there is no need for continuous memory.
That’s all for discussion: the linked list is so perfect, and the array is full of shortcomings. Wouldn’t it be better to use the linked list in the future?
But linked lists also have disadvantages. For example, its definition form is more complex, which gives programmers a certain learning threshold; Can not directly locate an element in the linked list, but can only traverse through the preamble in turn, which will reduce the access efficiency and so on.
In development practice, which data type should be used in the end should be changed flexibly according to the actual needs, and it is not allowed to drill a bull’s horn in a certain way.
For more developers, we should position ourselves as language users rather than language researchers. Therefore, in programming practice, how to adopt a reasonable, efficient and useful way is what we really need to consider, rather than blindly pursuing tricky skills and techniques. Don’t fall into the “strange circle of ability”.
Have you learned a lot of new knowledge here~
If you really want to learn programming, Xiaobian recommends meC language / C + + programming learning base [Click to enter]!
It’s all about learning programming, guys. It’s still simple to take you in. Learn together and cheer together~
There are many learning materials and videos, I believe you will like it!
Involving: game development, common software development, basic programming knowledge, curriculum design, hackers, etc