C. Create a single-chain table (Leading node) by using the header plug and tail plug)
In my previous blog titled "C Implementation of the header plug-in and tail plug-in method to build a single-chain table (no leading node) it details how to use the header plug-in and tail plug-in to create a single-chain table with no leading nodes. However, in actual use, we use the most single-chain table with leading nodes. Today, we will implement the head plug and tail plug of the linked list of the leading node.
The core code is as follows:
// Create a single-chain table with the leading Node (tail Insertion Method) void CreateListTailInsert (Node * pNode) {/*** even if the number entered at the beginning is less than or equal to 0, the single-chain table of the lead node is created successfully, but the single-chain table is empty, that is, there are no other nodes except the header node. */Node * pInsert; Node * pMove; pInsert = (Node *) malloc (sizeof (Node); // check whether the allocated memory is successfully pInsert = NULL? Memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; scanf ("% d", & (pInsert-> element); pMove = pNode; while (pInsert-> element> 0) {pMove-> next = pInsert; pMove = pInsert; // pMove always points to the last Node pInsert = (Node *) malloc (sizeof (Node); // you need to check whether the allocated memory is successfully pInsert = NULL? Memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; scanf ("% d", & (pInsert-> element ));} printf ("% s FUNCTION execution, the single-chain table of the leading node is successfully created by means of the End Plug \ n" ,__ FUNCTION __);} // create a single-chain table with the leading Node (header Insertion Method) void CreateListHeadInsert (Node * pNode) {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node )); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; scanf ("% d", & (pInsert-> element )); while (pInsert-> element> 0) {pInsert-> next = pNode-> next; pNode-> next = pInsert; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; scanf ("% d ", & (pInsert-> element);} printf ("% s FUNCTION execution. The single-chain table at the leading node is successfully created using the header Insertion Method \ n" ,__ FUNCTION __);}