In c/c ++, the structure (struct) knowledge point is enhanced, and the linked list is learned.

Source: Internet
Author: User

In the previous tutorial, We have briefly explained what structure is. To further learn the important knowledge point of 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:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
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 <"chain first pointer:" While (head) // use the memory to point to null as a condition to display the previous input content in a loop
{
Cout 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:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
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 <"chain first pointer:" While (head) // use the memory to point to null as a condition to display the previous input content in a loop
{
Cout 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; // you can set the tail pointer of a 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

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.