Comprehensible data Structure C language version (4)--table and linked list

Source: Internet
Author: User

Before we talk about the specifics of this article, let's start by explaining something. In the real life we say the "table" is often two-dimensional , such as the curriculum, there are rows and columns, the score table also has rows and columns. But in the data structure, or in the scope of our discussion, what we call "table" is one dimension, that is, all "elements" are arranged in front and back. As far as I am concerned, this kind of "table" uses "queue" to describe more appropriate. However, the term "queue" in the data structure is occupied by a special "table", so we can no longer use "queue" to represent "table", later we will introduce "queue".

  

In the first paragraph of the description process, we have actually said the definition of "table", that is, if the elements in the collection are the same as a team before, then this set is a table .

Obviously, our usual array is one of the tables, which conforms to the "element one after the other" requirements. The table is the most basic data structure ( Although the structure can also store multiple data, but its overall is considered an object ), whenever we need to store the data there is no special relationship ( such as a one-to-many relationship ), or the existence of the relationship is "a previous" ( For example, when a user enters multiple characters, there is a "one after one" relationship between the characters in the input order. In the application, for example, we can store characters in a character array, and then determine whether the user entered the correct password , and so on, we can use the table.

So, what about the table, if it ends here? (after all, the array should all be used) obviously not, we said, the array is a kind of table, but also just a kind of table AH ~

Next we'll talk about the drawbacks of using arrays as tables . First let's assume a situation (as mentioned in the comprehensible data structure (1) ):

We want to write a program, the process of this program is very simple: to get user input ( here it is necessary to use the table to store the user's input ), the user input to the calculation or operation, output results.

But this program to consider a situation: users sometimes only need to enter a few data, and sometimes need to enter tens of thousands of data ( Some people may say how to enter so many, people are exhausted ...) But don't forget that there is an operation called file redirection, although I do not remember ╮ (╯_╰) ╭). For C programs, this problem has to be considered, if you decide to use an array to store the user's input, then the size of the array should be determined at the time of creation (the impression that the C11 standard allows the array to be created when the size is no longer fixed, but always with the latest standards to write the program is not a good thing, Because the standard of popularization always takes time ). It is clear that the size of the array is OK, such as 100,000 (which is definitely greater than the number of user input possible anyway). However, this can be a serious problem, that is, your program will not need so much space, still use so much space , such as the average user input only hundreds of, but each run the program first occupies 100,000 size of the array space. Obviously this kind of procedure is very bad, the user's memory may have other programs to use, second, perhaps other users do not need 100,000 storage space and his memory is not enough.

So what do we do with this situation?

First we find that we still use the table (assuming that the relationship between the user's input data is a previous one), so let's first make sure that we use the data structure of the table type. And then the key point we're hoping for is that the table can change size as needed . So how do we implement such a table? Now, there is no need for you to think about the solution, we already have a ready-made data structure, that is linked list!

Looking back at the array, we found that we actually know two of the information:

  1. Where is the first element of the array?

2. The elements in the array are contiguous, the latter element is behind the previous element, and there is no "null" in the middle

Because we know these two pieces of information, we don't need to know the exact location of each element, as long as the position N of an element in the table, we can add N to the location of the first element to get the element.

But this is the problem with the array, in order to satisfy the 2nd, we must specify its size when the array is created, only in this way can we find an entire chunk of the array that satisfies 2 . So, in order to solve this shortcoming, we have to do is no longer use a whole area, each element to get the necessary ( that is, the element is not necessarily adjacent, as long as the element is added to find a place to put down the element of the House on the line )!

But this will bring about a problem, that is , we are scattered, and how to find everyone?

Let's revisit the information of the array 2, and then do another explanation of the angle:

  The position of an element in an array is actually based on the position of the previous element + one, and the position of the first element requires us to save it manually. In other words, the position of an element is its previous element "Tell" us!

Let's apply this idea to our new ideas! That makes each element "remember" the position of its next element ! In this way, we can add elements to the table as needed, so long as the new element is added, the last element of the original "Remember" where the new element can "chain" the table like a chain! Our "linked list" thought was born ~:-P

What we have to do next is to put this idea into practice. I believe very soon, we will find that the key to implementing the list is how to "make the element remember where the next element is"? Here we have a big mouth, as the first blog post says, data structures not only determine how to store, but sometimes decide or have to consider "what data to store." Now we have this problem . Obviously, the data itself does not remember where the next data is, such as the data type int, and how does it remember where the next int is? At this point, we need to "encapsulate" the data to form a new type of data, and then this data type will be able to remember the location of the next same data type.

The idea is to "encapsulate" the new data type and then ask it to remember where the next data of the same data type is located . "Encapsulation" can make us think of the structure ( perhaps you can't imagine but it doesn't matter, now you know ╮ (╯_╰) ╭), and "position" will let us think of pointers. So the new data type that we "encapsulate" should be a struct like this:

struct   element{      DataType  data;    // datatype depends on the actual element type      struct  Element  *  next;   // next means that it stores the position of the next element }

And then in our program, we just need to know where the first element is (the first one will tell us where the second one is, and so on):

struct  Element  a={data,null}; struct  Element  * list=&A; // then add the element to the list of lists that begin with lists

Now, we should all understand why we should use linked lists and the basic idea of how the list should be implemented and how to "encapsulate" the elements of a list.

But while knowing what to do when adding new elements to a list has been described verbally, it has not yet given the implementation of the Code, and how to remove an element from the list and how to find the nth element has not yet been implemented.

These can be referred to as the operation of the linked list, or can be said to be linked to the data structure of the use of the algorithm. regarding these, we will introduce in the next blog post.

Finally, what are we talking about this time?

First, what is a table.

Second, the table is of any possible use.

Third, the array as a disadvantage of the table.

The idea of a linked list and the "encapsulation" of basic ideas and basic elements

The next blog post, we'll show you the operation of the list, and the special "linked list"--the cursor array

Comprehensible data Structure C language version (4)--table and linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.