C implement the head plug and tail plug methods to build a linked list
The construction of the linked list is actually the process of constantly inserting nodes. Node insertion can be divided into header insertion and tail insertion. The header Insertion Method inserts the node after the header node and always uses the node as the first node. The end Insertion Method inserts an element at the last node of the linked list as the last node. If you want to understand the concept of a linked list and other linked list operations, see the linked list of data structures and algorithms and the basic operations for implementing a linked list in C language.
/// Main. c // HeadInsertAndTailInsert /// Created by chenyufeng on 16/2/25. // Copyright©In 2016, chenyufengweb. All rights reserved. // *** create a single-chain table by using the header insertion and tail insertion Methods */# include
# Include "stdlib. h "# include" string. h "typedef int elemType; // construct the Node typedef struct ListNode {int element; struct ListNode * next;} Node; // initialize the linked table void initList (Node * pNode) {pNode = NULL; printf ("% s FUNCTION execution, header Node initialization complete \ n" ,__ FUNCTION _);} // print the linked table void printList (Node * pNode) {if (pNode = NULL) {printf ("% s FUNCTION execution, empty linked list, print failed \ n" ,__ FUNCTION _);} else {while (pNode! = NULL) {printf ("% d", pNode-> element); pNode = pNode-> next;} printf ("\ n ");}} // Node * HeadInsert (Node * pNode) {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node); if (pInsert = NULL) {printf ("% s FUNCTION execution, memory allocation failed, linked list creation 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 create a linked list \ N ",__ FUNCTION _); return NULL;} while (pInsert-> element> 0) {if (pNode = NULL) {pNode = pInsert ;} else {// pay attention to the order of the following statements, otherwise the chain may break into pInsert-> next = pNode; pNode = pInsert;} pInsert = (Node *) malloc (sizeof (Node); if (pInsert = NULL) {printf ("% s function execution, memory allocation failed, linked list creation failed \ n ", __function _); return NULL;} memset (pInsert, 0, sizeof (Node); scanf ("% d", & (pInsert-> element )); pInsert-> next = NULL;} printf ("% s function Run the command. The chain table is successfully created by the header Insertion Method \ n ",__ FUNCTION _); return pNode;} // The tail insertion method Node * TailInsert (Node * pNode) {Node * pInsert; // The Node * pMove; // The Node pInsert = (Node *) malloc (sizeof (Node); if (pInsert = NULL) {printf ("% s FUNCTION execution, memory allocation failed, linked list creation 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 create linked list \ n ",__ FUNCTION _); return NULL;} pMove = pNode; while (pInsert-> element> 0) {if (pNode = NULL) {// do not forget to modify the point of the pMove pointer. The initial pMove must point to the header node pNode = pInsert; pMove = pNode ;} else {// traverse to find the last node while (pMove-> next! = NULL) {pMove = pMove-> next;} pMove-> next = pInsert;} pInsert = (Node *) malloc (sizeof (Node )); if (pInsert = NULL) {printf ("% s FUNCTION execution, memory allocation failed, linked list creation failed \ n" ,__ FUNCTION _); return NULL ;} memset (pInsert, 0, sizeof (Node); scanf ("% d", & (pInsert-> element); pInsert-> next = NULL ;} printf ("% s FUNCTION execution. The linked list is successfully created by means of the tail plug-in \ n" ,__ FUNCTION _); return pNode;} int main (int argc, const char * argv []) {Node * pList; initList (pList); printList (pList); // create the linked list pList = HeadInsert (pList); printList (pList ); // create a linked list using the end-Insertion Method pList = TailInsert (pList); printList (pList); return 0 ;}