1, to create a single chain, single chain insertion method to assign the initial value with the tail, and print out the list of all the data
1 / * Tail insertion method 2 to create a single list . 3 * / . 4 . 5 . 6 #include <stdio.h> . 7 #include <stdlib.h> . 8 . 9 // list memory structure 10 typedef struct LinkList { . 11 int Data; // Data domain 12 is struct LinkList * Next; // pointer field 13 is } Link; 14 15 // create a single linked list (created with a head node) 16 Link * createLink () { . 17 Link head * = (* Link) the malloc ( the sizeof (Link)); //Applying the first node 18 is head-> Data = - . 1 ; // head node data field is -1 . 19 head-> Next = NULL; // head node pointer field is NULL 20 is IF (head) { 21 is / * 22 is the printf ( "single chain successfully created \ n-"); 23 is the printf ( "head node data field is:% D \ n-", head-> data); 24 the printf ( "head node pointer field is: P% \ n-", head-> Next); 25 * / 26 is return head; 27 } 28 the else { 29 the printf ( " single chain creation failed \ n- " ); 30 return ; 31 is } 32 33 is } 34 is 35 36 // single list initialization (tail insertion method) 37 [ int * initLink (Link head_node *, int Data [ 10 ]) { // with Data [] stored list initialization data, pass after ShowLink () traverse the print 38 is Link * p = head_node; // the head node is assigned to p, when p is the initial header node, the node to continue application 39 Link head_node * = R & lt; // while the head node assigned point r, r is always the last node in the linked list 40 for ( int I = 0 ; I < 10 ; I ++ ) { 41 is P = (Link *) the malloc ( the sizeof (Link)); //p continue next application memory space 42 is p-> Data = I; // the data field node assignment new application 43 is p-> Next = NULL; // node pointer field is initialized to empty a new application 44 is R & lt = p; // after a successful application p junction space, this node will be p new application is assigned to r, r always the last to make a list of the node 45 printf ( " % d " , R-> the Data); // print the value of the data field in the initialization of each node in the linked list 46 is data [I] = R-> data; // the value of the data field are stored in data [] array 47 } 48 the printf ( " \ n- " ); 49 // the printf ( "final pointer field r is: P% \ n-", R-> Next); // final r is the last node, the pointer fields are empty 00000000 50 return data; 51 } 52 53 //打印链表数据 54 void showLink(int data[10]) { 55 for (int i = 0; i < 10; i++) { 56 printf("%d ", data[i]); 57 } 58 } 59 60 void main() { 61 createLink(); 62 63 int data[10]; 64 the printf ( " initialization list is: \ n- " ); 65 initLink (createLink (), Data); 66 67 the printf ( " print data list: \ n- " ); 68 ShowLink (Data); 69 70 } 71 is
2, the initialization assignment was changed to user input
1 / * Tail insertion method 2 to create a single list . 3 * / . 4 . 5 . 6 #include <stdio.h> . 7 #include <stdlib.h> . 8 . 9 // list memory structure 10 typedef struct LinkList { . 11 int Data; // Data domain 12 is struct LinkList * Next; // pointer field 13 is } Link; 14 15 // create a single linked list (created with a head node) 16 Link * createLink () { . 17 Link head * = (* Link) the malloc ( the sizeof (Link)); //Applying the first node 18 is head-> Data = - . 1 ; // head node data field is -1 . 19 head-> Next = NULL; // head node pointer field is NULL 20 is IF (head) { 21 is / * 22 is the printf ( "single chain successfully created \ n-"); 23 is the printf ( "head node data field is:% D \ n-", head-> data); 24 the printf ( "head node pointer field is: P% \ n-", head-> Next); 25 * / 26 is return head; 27 } 28 the else { 29 the printf ( " single chain creation failed \ n- " ); 30 return ; 31 is } 32 33 is } 34 is 35 36 // single list initialization (tail insertion method) 37 [ int * initLink (Link head_node *, int Data [ 10 ]) { // with Data [] stored list initialization data, pass after ShowLink () traverse the print 38 is Link * p = head_node; // the head node is assigned to p, when p is the initial header node, the node to continue application 39 Link head_node * = R & lt; // while the head node assigned point r, r is always the last node in the linked list 40 for ( int I = 0 ; I < 10 ; I ++ ) { 41 is P = (Link *) the malloc ( the sizeof (Link)); //p continue next application memory space 42 is 43 is int NUM = 0 ; 44 is the printf ( " Please enter the values to be added: \ n- " ); 45 Scanf ( " % D " , & NUM); 46 is 47 p-> Data NUM = ; // to node data field assigned new applications 48 p-> Next = NULL; // node pointer field is initialized to empty a new application 49 R & lt = p; // after the node p successful application space, the p this application is assigned to the new node r, r so that always the last linked list node 50 // the printf ( "% D", R-> data); // print out the data for each node in the linked list initialization the value of the field 51 is Data [I] = R-> Data; //The value of the data field are stored in Data [] array 52 is } 53 is the printf ( " \ n- " ); 54 is // the printf ( "final pointer field r is: P% \ n-", R-> Next); / / final r is the last node, the pointer field is empty 00000000 55 return data; 56 is } 57 is 58 // print data list 59 void ShowLink ( int data [ 10 ]) { 60 for ( int I = 0 ; I < 10 ; ++ I ) { 61 is the printf ( " % D " , Data [I]); 62 is } 63 is } 64 65 void main () { 66 createLink (); 67 68 int Data [ 10 ]; 69 the printf ( " initialization list: \ n- " ); 70 initLink (createLink (), Data); 71 is 72 the printf ( " print data list: \ n- " ); 73 is ShowLink (data); 74 75 } 76