Control of the chain list in C ++ in writing

zhaozj2021-02-16  130

Author Name: Fun Fruit (linchao14)

Email address: linchao14@xinhuaet.com

If you need to reprint, please contact the author.

text

I saw some netizens on the forum said that the chain table is not very good in the C learning process, and I want to write some experience I have to study the list. I hope to help these friends! If you have any good way, let us share it!

First of all, it is necessary to understand what C introduces the latheclable list is for what is the role of the linked list. Second, we must figure out the nature of the chain; finally the method of use of the list. Then I will give an example, if I have established a score management system related to the student needs to create a structure, as follows:

Struct student

{Char Name [20];

Long id;

Float score;

}

If you are determined by the number of students, as long as the array of this structure is ok. But if the number is uncertain, then you need to use a list. The reason why the list can solve this problem, which involves the nature of the chain list - using the dynamic memory allocation technology. In fact, the internal implementation of the database system is being implemented on the basis of dynamic allocation technology. To turn the structure into a list, you only need to join a project member (a explanation for a while). There is a problem here to note that the structural member cannot be a variable of its own structure, but the pointer can. Why do you need to add a pointer to your own structure, please see the picture. After all, the graphics are very intuitive.

We clearly see that the address of each structure in memory is discontinuous. The "Link" name is very image, that is, the structure of the structure is like a chain, a ring buckled a ring to make it in order to inquire , Delete and insert. It is only a pointer to this heavy responsibility! Look at the picture below:

At a glance, it is introduced into the structure of its own structural pointer to store the address of the next structural member. Let them string together! Now let's look at a complete example (traversing the list, also known as the query chain list), there is a concept to say - nodes, in this structure, each Student structure variable is called node.

#include

Struct student

{

Long LID;

Float fscore;

Student * pnext;

}

Student * Phead; // Create a header pointer

Student * crete ()

{

Student * pstart; // Create a node pointer

Student * pend; // Create a node tail pointer to insert a node after it

PStart = new student; // Create a node

CIN >> PSTART-> LID >> PSTART-> fscore; // Assignment

PHEAD = NULL; // Initialize the header pointer

Pend = pstart; // The start address of the first node is also loaded into the tail pointer of the node, because PStart is to load the next node's start address

While (Pstart-> LID! = 0)

{

if (PHEAD == Null) // If it is a new table ...

PHEAD = PStart; // Header pointer loads the address of the first node

Else

Pend-> pnext = pstart; / / is not a new table, connect the new node behind the last node

Pend = pstart;

Pstart = new student; / / Assign new space for PStart, ready to connect one node

CIN >> PSTART-> LID >> PSTART-> fscore;}

Pend-> pnext = null; // When the LID is placed 0, the loop is jumped, and the current node is the last node, so pointing null

Delelte pstart; // Because Pstart is more assigned a new space before jumping out of the cycle, so you have to reclaim it!

Return (PHEAD);

Void Print (Studient * HEAD)

{

COUT << "List of the items / n";

While (Head)

{

Cout << Head-> LID << "," << Head -> fscore;

PHEAD = PHEAD-> PNEXT; // This statement is more important, it migrates the header pointer to the header pointer, and output member values ​​in order

}

}

void main ()

{

Print (CREATE ()); // Tune the print () function using the value of the PHEAD pointer returned by Create ().

}

Delete Link Plop Node

Link list delete operation To ensure that the link between the list is not destroyed, this is also the difficulty - a node delete, its previous node's pointer member should point to the latter node, this will not be deleted To interrupt the link. The relationship is as follows:

Delete the first node:

Remove the non-chain first node:

The following function implements the deletion of the chain table node

Void delete (student * head, long number)

{

Student * p;

IF (! HEAD)

{

COUT << "this is the last node / n";

Return;

IF (PHEAD-> LID == Number) // Delete the node of the chain

{

P = head;

HEAD = PHEAD-> PNEXT;

Delete P;

COUT << "The Head of List Have Been Deleted / N";

Return;

}

/ * In short, the most important thing is the order of operation. If P does not point to the first node of the list, when PHEAD points to the next node of the chain head, the original chain is straightened, causing the node address to be lost, so that the heap space cannot be released. If the chain head node is first removed, the PHEAD pointer cannot be derived from the next node address exported by the chain head. The role of the P pointer also has a buffer role. * /

For (student * pguard = head; pguard-> pnext; pguard = pguard-> pnext)

{

IF (pguard-> pnext-> lid == number) // pguard pointing to the previous node to delete the node

{

P = pguard-> pnext; // P pointing to the node to delete

pguard-> pnext = p-> pnext; // Take the next node of the node to be deleted into the next node of its previous junction (make the node to be deleted)

Delete P;

COUT << "Have Been Deleted! / N";

Return;

}

}

Cout << "not found / n";

}

/ * When finding the node to be deleted, Pguard is important to the previous node to delete the node. Otherwise, the previous node address of the node is lost, and the PNEXT member cannot link to the post-nodes of the node to be deleted. In the data structure, the Pguard pointer is usually called "sentinel". * / In Main () main function is as follows:

PHEAD = CREATE (); // This step is not allowed, this is to make Phet point to the head

Delete (phead, fscore); // change Number to the corresponding number of scores

Insert a list of linked list

Similarly, the insertion operation cannot damage the link relationship. The PNEXT member of the insertion node should point to its latter node, and then point the PNEXT member of the previous node to the newly inserted node, so that the new list is available. Figure:

When we assume that the node is made by the LID value by the LID value.

Void insert (student * head, student * stud)

{

If (head == null) // Plug the node into an empty linked list, there is a new insertion node after the operation is completed

HEAD = STUD;

Stud.pnext = null;

Return;

}

IF (head-> limited> stop-> limited) // Insert a node into the header of the list

{

Stud.pnext = head;

HEAD = STUD;

Return;

}

Student * pguard = head; // Insert the node between two nodes

While (pguard-> pnext && pguard-> pnext-> lid> stud-> limited)

pguard = pguard-> pnext; // Metall with loop lookup meets the node

Stud-> pnext = pguard-> pnext; // make the insertion node points to the next node of the current node (from right left thought)

pguard-> pnext = stud; // Point the current node to the insertion node

}

So far, the control of the linked list has been described. In short, the essence is how to delete or insert the order of operation in the row. With a white voice, I want to take a step before operation, leave a backward road - when you realize that there is no way to go, it is definitely not prepared for the previous step. This is only some of my personal feelings, I don't know if it is right, I will not write this anyway!

转载请注明原文地址:https://www.9cbs.com/read-13631.html

New Post(0)