C. Implement the header insertion and tail insertion methods to build a non-cyclic double-stranded table (no leading node)
In practice, double-stranded tables are much more convenient and flexible than single-linked tables. For the basic operations on non-cyclic double-stranded tables with no leading nodes, I have detailed implementation in the article "basic operations on two-way non-cyclic linked list with no leading node" in C language. Today, we will use two different methods of header insertion and tail insertion to create a double-stranded table.
The core code is as follows:
// Create a non-cyclic two-way linked list Node * TailInsertCreateList (Node * pNode) {Node * pInsert; Node * pMove; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pInsert-> prior = NULL; scanf ("% d", & (pInsert-> element); pMove = pNode; if (pInsert-> element <= 0) {printf ("% s function execution, the input data is invalid. Create a linked list to stop \ n ",__ FUNCTION _); return NULL;} while (pInsert-> element> 0) {if (pNode = NULL) {pNode = pInsert; pMove = pNode;} else {pMove-> next = pInsert; pInsert-> prior = pMove; pMove = pMove-> next;} pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pInsert-> prior = NULL; scanf ("% d", & (pInsert-> element);} printf ("% s FUNCTION execution. The linked list is successfully created by the end-Insertion Method \ n" ,__ FUNCTION __); return pNode;} // create a non-circular bidirectional linked list Node * HeadInsertCreateList (Node * pNode) {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pInsert-> prior = NULL; scanf ("% d", & (pInsert-> element); if (pInsert-> element <= 0) {printf ("% s function execution, illegal input data, create a linked list to stop \ n ",__ FUNCTION _); return NULL;} while (pInsert-> element> 0) {if (pNode = NULL) {pNode = pInsert ;} else {pInsert-> next = pNode; pNode-> prior = pInsert; pNode = pInsert;} pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pInsert-> prior = NULL; scanf ("% d", & (pInsert-> element ));} printf ("% s FUNCTION execution, the head plug method successfully creates the linked list \ n" ,__ FUNCTION _); return pNode ;}