Create a list 10 insertion method

1  / * bit insertion to create a single linked list * / 
2 #include <stdio.h>
 . 3 #include <stdlib.h>
 . 4  
. 5 typedef struct Link {
 . 6      int Data;
 . 7      struct Link * Next;
 . 8  } Link;
 . 9  
10 Link * initLink () {
 . 11      Link head * = (* Link) the malloc ( the sizeof (Link));
 12 is      IF (head) {
 13 is          the printf ( " creating the first node successfully \ n- " );
 14          head-> Data = - . 1 ;
 15         head-> Next = NULL;
 16          return head;
 . 17      }
 18 is      the else {
 . 19          the printf ( " Creating the first node fails \ n- " );
 20 is          return ;
 21 is      }
 22 is  }
 23 is  
24  void insertFromHead (Link head_node *, int NUM) {
 25      Link new_node = * (* Link) the malloc ( the sizeof (Link)); // get a new node 
26 is      new_node-> Data = NUM; // parameter value received from the new node 
27      new_node-> Next = head_node ; //Pointer field of the new node is the head node defined 
28      head_node = new_node; // first node becomes the new node 
29      the printf ( " value after the first node in the linked list head is: D% \ n- " , head_node-> Data );
 30  }
 31 is  
32  void main () {
 33 is      insertFromHead (initLink (), . 3 );
 34 is      insertFromHead (initLink (), . 4 );
 35 }

 

Guess you like

Origin www.cnblogs.com/shanlu0000/p/12457772.html