The construction of a linked list is actually the process of inserting nodes continuously. And the insertion of the node can be divided into the head interpolation method and the tail interpolation method. The head interpolation method is to insert the node after the head node, always using the node as the first node. The end interpolation method is to insert the element at the last node of the list, as the last node. If you want to understand the concept of linked lists and other linked list operations, please refer to the "Data structure and algorithm chain list" "C language implementation of linked list of basic operations" two articles. The sample code is uploaded to Https://github.com/chenyufeng1991/HeadInsertAndTailInsert.
main.c//headinsertandtailinsert////Created by Chenyufeng on 16/2/25.//copyright©2016 year chenyufengweb. All Rights reserved.///** * Set up single-link lists using head interpolation and tail interpolation respectively */#include <stdio.h> #include "stdlib.h" #include "string.h" typedef int elemtype;//Constructs a node typedef struct listnode{int element; struct ListNode *next;} node;//Initialize the linked list void initlist (Node *pnode) {pnode = NULL; printf ("%s function execution, head node initialization complete \ n", __function__);} Print List void printlist (Node *pnode) {if (Pnode = = NULL) {printf ("%s function executed, list empty, print failed \ n", __function__); }else{while (pnode! = NULL) {printf ("%d", pnode->element); Pnode = pnode->next; } printf ("\ n"); }}//head interpolation node *headinsert (node *pnode) {node *pinsert; Pinsert = (node*) malloc (sizeof (Node)); if (Pinsert = = NULL) {printf ("%s function performed, memory allocation failed, build list failed \ n", __function__); return NULL; } memset (Pinsert, 0, sizeof (Node)); scanf ("%d",& (pinsert->element)); Pinsert->next = NULL; If(pinsert->element <= 0) {printf ("%s function execution, incorrect input data, failed to build list \ n", __function__); return NULL; } while (Pinsert->element > 0) {if (Pnode = = NULL) {pnode = Pinsert; }else{//Note the order of the following statements, otherwise it may cause the chain break pinsert->next = Pnode; Pnode = Pinsert; } Pinsert = (node*) malloc (sizeof (Node)); if (Pinsert = = NULL) {printf ("%s function performed, memory allocation failed, build list failed \ n", __function__); return NULL; } memset (Pinsert, 0, sizeof (Node)); scanf ("%d",& (pinsert->element)); Pinsert->next = NULL; } printf ("%s function execution, header interpolation method to establish the linked list succeeded \ n", __function__); return pnode;} The tail interpolation method node *tailinsert (node *pnode) {node *pinsert;//nodes to be inserted node *pmove;//nodes that traverse the list Pinsert = (node*) malloc (sizeof (Node)); if (Pinsert = = NULL) {printf ("%s function performed, memory allocation failed, build list failed \ n", __function__); return NULL; } memset (Pinsert, 0, sizeof (Node)); scanf ("%d",& (pinsert->element)); Pinsert->next = NULL; if (pinsert->element <= 0) {printf ("%s function execution, incorrect input data, failed to establish linked list \ n", __function__); return NULL; } pmove = Pnode; while (Pinsert->element > 0) {if (Pnode = = NULL) {//note do not forget to modify the Pmove pointer pointing, initial pmove be sure to point to the head node Pnode = Pinsert; Pmove = Pnode; }else{//traversal found last node while (pmove->next! = NULL) {pmove = pmove->next; } pmove->next = Pinsert; } Pinsert = (node*) malloc (sizeof (Node)); if (Pinsert = = NULL) {printf ("%s function performed, memory allocation failed, build list failed \ n", __function__); return NULL; } memset (Pinsert, 0, sizeof (Node)); scanf ("%d",& (pinsert->element)); Pinsert->next = NULL; } printf ("%s function execution, tail interpolation method to establish link list succeeded \ n", __function__); return pnode;} int main (int argc, const char * argv[]) {Node *plist; Initlist (pList); Printlist (pList); The head interpolation method to establish the linked list pList = Headinsert(pList); Printlist (pList); The tail interpolation method establishes the linked list pList = Tailinsert (pList); Printlist (pList); return 0;}
C Implement the head interpolation method and the tail interpolation method to construct the linked list