Today engaged in one hours, the head is painful, it should be no rest well, learning the data structure of this section, feeling a lot of income, the following code and experience:
1 /*demonstration of 24_ linked list creation and chain list traversal algorithm*/2# include <stdio.h>3# include <malloc.h>4# include <stdlib.h>5 6typedefstructNode7 {8 intData//data fields9 structNode * PNEXT;//pointer FieldTen}node, *pnode;//node is equivalent to struct node One A //function Declaration -Pnode Create_list (void); - voidtraverse_list (Pnode phead); the - intMainvoid) - { -Pnode phead = NULL;//equivalent to struct node * Phead = NULL, assigning the address of the first node to Phead (after the first and last nodes in a list are NULL, there are no other elements) + //pnode equivalent to struct Node * -Phead =create_list (); + traverse_list (phead); A at - return 0; - } - - //Create address of address type, because create_list this function is the address to return - //function is to return the address of the head node, so that the head node can point to a list inPnode Create_list (void) - { to intLen//number of active nodes stored + inti; - intVal//a node that is used to temporarily store the user's cooked-in is a value the * //previously said that as long as the head node is found, for the operation of the list is convenient, so here we first define the head node $Pnode Phead = (pnode)malloc(sizeof(NODE));//defining head nodes (without valid data)Panax Notoginseng if(NULL = = Phead)//Dynamic memory allocation fails if the pointer is pointing to null - { theprintf"allocation failed, program terminated! \ n"); +Exit (-1); A } thePnode ptail = Phead;//Phead is a pointer to the head node, if the user is the number of input nodes is 0, then there is only one "head node" +Ptail->data = NULL;//at this point the Phead is assigned to Ptail, then it is reasonable l, and then the pointer field is empty (and the initialization of the same ...) ) - $printf"Please enter the number of linked list nodes you want to generate: Len ="); $scanf"%d", &len); - for(i =0; i < Len; i++) - { theprintf"Please enter a value for%d nodes", i +1); -scanf"%d", &val);WuyiPnode pnew = (pnode)malloc(sizeof(NODE));//Create a new node so that the pointer points to each node (it loops the Len Times) the if(NULL = = pnew)//Dynamic memory allocation fails if the pointer is pointing to null - { Wuprintf"allocation failed, program terminated! \ n"); -Exit (-1); About } $ /* - - pnew->data = val;//a temporary node - Phead->pnext = pnew;//to Hang pnew on Phead A pnew->pnext=null;//This temporary node is at the end of the empty + the */ - $ the //The above/**/commented out this line of code is problematic, the above commented out of the meaning of the code is to separate the head node behind the nodes are hanging on the head node, the //causes the pointer field of the node behind the head node to be lost, and we want to just hang the first node on the head node, followed by sequentially, that is, a second the //node hangs on the first node of the pointer field, and so on, it is obvious that the above commented out code is not able to implement this function the - //The following is an improved inPnew->data = val;//depositing valid data into Pnew thePtail->pnext = pnew;//Hang the pnew in the back of the Ptail (that is, the Ptail pointer field, string up) thePnew->pnext = NULL;//empty The Pnew's pointer field AboutPtail = pnew;//in the Pnew assignment to the Ptai, so that it can loop, to achieve the connection (and we want to just put the first node on the head node, followed by sequentially, that is, the second the //node hangs on the first node's pointer field) the } the returnPhead; + } - the Bayi //The traversal function does not need to return a value, the argument is still there, or how to know which object to traverse! the //or define pointer variable Pnode phead because Traverse_list (Phead) is calling the address the - - /* the The overall idea of the traversal of a linked list: the 1. Define a pointer variable p to point to the first valid node the 2. Determine if the pointer field for this node is empty (if not the last one) the 3. Do not empty the valid data for this node, and P-pnext to refer to the next node (not with p++ Oh!). ) - 4. If empty, the arrival tail node is indicated the */ the the 94 voidTraverse_list (Pnode phead)//How to traverse, is not the same as before with the array, thought the array is continuous, here discontinuous the { thePnode p = phead->Pnext; the 98 while(NULL! =p) About { -printf"%d", p->data);101p = p->Pnext;102 }103printf"\ n");104}
Run:
To recite a poem:
The spirit of the hundred refining, front sharp not down. --Liu Over
Presentation of linked list creation and chain list traversal algorithms _c language