To further learn the important knowledge point of the structure, let's take a look at the chain table structure today.Struct can be seen as a type of custom data. It also has an important feature: struct can be nested with each other, but it is also conditional. struct can contain struct pointers, however, the struct variable cannot be included in the struct.
Struct Test
{
Char name [10];
Float socret;
Test * next;
}; // This is correct!
Struct Test
{
Char name [10];
Float socret;
Test next;
}; // This is incorrect!
With this special feature of struct, we can generate a ray structure with a ring and a ray structure pointing to the other.
Linked List learning is not as easy as you think. Many people encounter difficulties when they learn it. Many people give up learning because of this. Here I say that they must not give up, corresponding to its learning, we need to conduct decomposition-based learning, the method is very important, understanding takes time, and it is not necessary to force ourselves so tight, before learning, you have to make some basic preparations. You must have basic knowledge about heap memory and basic understanding of struct, with these two important conditions, you can easily grasp the difficulties of this section by performing decomposition learning.
Next we will provide a complete program for creating a linked list. Whether you understand it or not, you may want to take a look at it carefully. If you think about it, it doesn't matter if you don't understand it, because I will have a break-down tutorial below, but the previous basic thinking must be done, or even if I break it down, you cannot understand it.
The Code is as follows:
# Include
Using namespace STD;
Struct Test
{
Char name [10];
Float socret;
Test * next;
};
Test * head; // create a global pointer to the linked list
Test * Create ()
{
Test * ls; // node pointer
Test * le; // link tail pointer
Ls = new test; // point ls to the heap memory address that is dynamically opened
Cin> ls-> Name> ls-> socrer;
Head = NULL; // when entering, do not set the head pointer to point to any address, because you do not know whether to enter null to jump out of the program as soon as it comes up.
Le = ls; // set the tail pointer of the chain to the heap memory address that has just been dynamically opened up. It is used to wait for the next le-> next, that is, the location of the next node.
While (strcmp (LS-> name, "null ")! = 0) // The value of LS-> name in the create cycle condition is not null and is used to add nodes cyclically.
{
If (Head = NULL) // determines whether the first entry into the loop
{
Head = ls; // if this is the first time you enter the loop, point the pointer that directs to the linked list to the heap memory address that was newly opened for the first time.
}
Else
{
Le-> next = ls; // if it is not the first time you enter the link, use the previous link tail pointer le-> next to the heap memory address dynamically created before the end of the previous loop.
}
Le = ls; // set the tail pointer of a chain to the node pointer in the current loop, used to indicate the next of the last node to the heap memory address dynamically created before the end of the previous loop during the next cycle.
Ls = new test; // dynamically opens up space for the next node in the heap memory
Cin> ls-> Name> ls-> socrer;
}
Le-> next = NULL; // set the next of the link tail pointer to null, because no matter how the loop ends, if this parameter is set to null, the linked list is displayed cyclically without endless loops.
Delete ls; // The Last dynamically opened memory is invalid at the end, so it must be cleared.
Return head; // return the first pointer of a chain.
}
Void showl (test * head)
{
Cout <"first link pointer:" <
While (head) // use the memory to point to null as a condition to display the previous input content in a loop
{
Cout <name <"|" <socret <
Head = head-> next;
}
}
Void main ()
{
Showl (create ());
Cin. Get ();
Cin. Get ();
}
The above Code aims to store the names and scores you enter and combine them into a chain structure.
The program has two components:
Test * Create ()
And void showl (test * head)
Create is used to create a linked list and showl is used to display the linked list.
The return type of the create function is a struct pointer. We use showl (create () when calling the program ());, the reason for not referencing is that the bootstrap pointer is a global pointer variable and we cannot change it within showl () Because showl () in the function, there is a moving operation head = head-> next;. If it is a reference, we will destroy the head pointer position, so that we can no longer find the first address location.
Next we will break down the entire program, and use the idea of a beginner to think about the whole program.
First of all, we should write this program to consider that because it is a linked list structure, we cannot know the size of the program. We can use dynamic heap memory to solve this problem, because the existence of programs in the heap is always valid before the end, and is not subject to the life cycle of the function stack space, however, it should be noted that we must have a pointer variable to store the incoming address of this chain structure. It is obviously inappropriate to establish this pointer variable within the function, once the function exits, this pointer variable also becomes invalid, So we declare a global pointer variable at the beginning of the program.
Test * head; // create a global pointer to the linked list
After solving these two problems, let's continue to think.
If there is an input, there must be an output. Because the output function and the input function are relatively independent, in order to continuously test the correctness of the program and debug the program, we should first write the call of the output function and the main function, create a function with the name "CREATE" first.
First, write the following code:
# Include
Using namespace STD;
Struct Test
{
Char name [10];
Float socret;
Test * next;
};
Test * head; // create a global pointer to the linked list
Test * Create ()
{
Return head; // return the first pointer of a chain.
}
Void showl (test * head)
{
Cout <"first link pointer:" <
While (head) // use the memory to point to null as a condition to display the previous input content in a loop
{
Cout <name <"|" <socret <
Head = head-> next;
}
}
Void main ()
{
Showl (create ());
Cin. Get ();
Cin. Get ();
}
The program is written here, and the basic form has come out, and we have input and call.
Next we will solve the input problem. The implementation of the linked list is achieved through circular input. Since it is a loop, we must consider the conditions for terminating the loop, avoid endless loops and invalid loops.
In the CREATE () function, we first write the following:
Test * Create ()
{
Test * ls; // node pointer
Test * le; // link tail pointer
Ls = new test; // point ls to the heap memory address that is dynamically opened
Cin> ls-> Name> ls-> socrer;
Head = NULL; // when entering, do not set the head pointer to point to any address, because you do not know whether to enter null to jump out of the program as soon as it comes up.
Le = ls; // set the tail pointer of the chain to the heap memory address that has just been dynamically opened up. It is used to wait for the next le-> next, that is, the location of the next node.
Le-> next = NULL; // set the next of the link tail pointer to null, because no matter how the loop ends, if this parameter is set to null, the linked list is displayed cyclically without endless loops.
Delete ls; // The Last dynamically opened memory is invalid at the end, so it must be cleared.
Return head; // return the first pointer of a chain.
}
Before creating a loop, we must consider a situation where no input is required.
When a program enters the create function, we must first create a node. First, we must create a node pointer, the latter points the node pointer to the dynamically opened test type dynamic memory address location.
Test * ls;
Ls = new test;
Since the program is a loop input, and the structure member test * Next is used to store the memory address of the next contact, we need to dynamically create a new memory space for each loop, therefore, we must have a pointer to store the memory address dynamically opened up in the last loop.
Test * le;
Next, before entering the loop, we will create the first node of the linked list. The first node must be created outside the loop, so we have
Cin> ls-> Name> ls-> socrer;
The execution of the program is the location, so we must consider that we do not want to continue running the program as soon as it comes up, so we first set the head pilot pointer to not pointing to any address, that is
Head = NULL;
In order to comply with the design philosophy of Le, that is, the link tail pointer, we must save the memory address that has just been dynamically opened before the loop, fortunately, in the next loop, we set the next member pointing to the previous node, so we have:
Le = ls;
The following code is used to implement loop input: