Data Structures and Algorithms acyclic basis of a single list and create a linked list traversal

. 1 #include <stdio.h>
 2 #include < the malloc .h>
 . 3 #include <stdlib.h>
 . 4  // function declaration 
. 5 pNode create_list (); // return value is the address list of the first node 
. 6  void traverse_list ( PHEAD pNode);
 . 7  
. 8 typedef struct the Node {
 . 9      int data; // data field 
10      struct the Node * pNext; // pointer field     
. 11 } NODEs, * pNode; // NODEs equivalent to struct Node pNODE equivalent to struct Node * 
12 is  
13 is  int main () {
 14     = NULL PHEAD pNode; // equivalent to the Node * PHEAD = NULL struct; 
15      
16      PHEAD create_list = (); // create a single chain acyclic, and assign the address of the head node of the linked list to PHEAD 
. 17      traverse_list ( PHEAD); // traverse 
18 is      
. 19      return  0 ;
 20 is  }
 21 is  
22 is  pNode create_list () {
 23 is      int len; // number of active nodes 
24      int I;
 25      int Val; // temporary storage node value input by the user
 26 is      
27      // allocate a valid data is not stored in the first node 
28      pNode PHEAD = (pNode) the malloc( The sizeof (NODEs));
 29      IF (PHEAD == NULL) {
 30          the printf ( " allocation fails, the program terminates " );
 31 is          Exit (- . 1 );
 32      }
 33 is      
34 is       pNode PTAIL = PHEAD;
 35       pTail-> = pNext NULL; // If only one node, this time on the tail node pointer field should be blank 
36      the printf ( " the number of list node: " );
 37 [      Scanf ( " % D " , & len);
 38 is      
39      for ( = I 0 ; I <len; I ++) {
 40          the printf ( " Enter a value of% d nodes: " , I + . 1 );
 41 is          Scanf ( " % d " , & Val);
 42 is          
43 is          pNode PNEW = (pNode) the malloc ( the sizeof (NODEs)); // create a new node 
44 is               IF (PNEW == NULL) {
 45              the printf ( " allocation fails, the program terminates " );
 46 is              Exit (- . 1 );
 47          }
 48          pNew-> data = Val; // the data stored into the data domain
49          pTail-> pNext = PNEW; // the new node address is assigned to the end node pointer field 
50          pNew-> pNext = NULL; // the new node pointer assignment field blank 
51 is          PTAIL = PNEW; // the last step copy the new node address to the end node (the new node is to allow to become the tail node)
 52 is         // pHead-> pNext = PNEW;
 53 is         // pNew-> pNext = NULL; // pointer to the last field is a null node 
54      }
 55      return PHEAD;
 56 is  }
 57 is  
58  void traverse_list (pNode PHEAD) {
 59      pNode p = pHead-> pNext; // if the list is empty, then the head node pointer field is empty, then p is an empty 
60      
61 is      the while (p! =NULL){
62         printf("%d ",p->data);
63         p=p->pNext;
64     }
65     printf("\n");
66     return;
67 }

 

Guess you like

Origin www.cnblogs.com/sunbr/p/11318621.html