C implement the head plug and tail plug methods to build a linked list

Source: Internet
Author: User

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 ;}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.