How to Implement bidirectional non-circular linked list (tail node of the leading node) using C language
In my previous blog, I implemented a lot of operations on a bidirectional non-circular linked list without taking the lead at the end of the node. In fact, like a single-chain table, many operations on the linked list without leading nodes are troublesome. It is often necessary to make additional judgments on the first node to increase the error cost. Today, we are going to implement the bidirectional non-circular linked list operation at the end of the leading node. Although the two nodes are maintained, the Operation simplicity is greatly improved. Code upload to https://github.com/chenyufeng1991/DoubleLinkedList_HeadList.
(1) define the node type of the non-cyclic bidirectional linked list of the End Node of the lead Node
typedef int elemType;typedef struct NodeList{ int element; struct NodeList *prior; struct NodeList *next;}Node;
(2) initialize a double-linked table
// 1. initialize the void InitialList (Node ** pHead, Node ** pTail) {* pHead = (Node *) malloc (sizeof (Node) of the non-cyclic bidirectional linked list of the lead and tail nodes )); * pTail = (Node *) malloc (sizeof (Node); if (* pHead = NULL | * pTail = NULL) {printf ("% s function execution, memory Allocation failed, failed to initialize the double-stranded table \ n ",__ FUNCTION _);} else {// This is the key and an important condition for empty determination (* pHead) -> prior = NULL; (* pTail)-> next = NULL; // when the linked list is empty, link the header and end nodes (* pHead) -> next = * pTail; (* pTail)-> prior = * pHead; printf ("% s function execution, the bidirectional non-cyclic linked list of the lead node and the End Node is initialized successfully \ n ",__ FUNCTION __);}}
(3) create a double-chain table by using the plug-in method
// 2. create the void CreateList (Node * pHead, Node * pTail) {Node * pInsert; Node * pMove; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> prior = NULL; pInsert-> next = NULL; scanf ("% d", & (pInsert-> element); pMove = pHead; while (pInsert-> element> 0) {pMove-> next = pInsert; pInsert-> prior = pMove; pInsert-> next = pTail; pTail-> prior = pInsert; pMove = pInsert; pInsert = (Node *) malloc (sizeof (Node )); memset (pInsert, 0, sizeof (Node); pInsert-> prior = NULL; pInsert-> next = NULL; scanf ("% d ", & (pInsert-> element);} printf ("% s function execution completed, two-way non-circular linked list created at the leading node and the tail node \ n ", __function __);}
(4) print the linked list in positive order
// 3. Print the void PrintList (Node * pHead, Node * pTail) {Node * pMove; pMove = pHead-> next; while (pMove! = PTail) {printf ("% d", pMove-> element); pMove = pMove-> next;} printf ("\ n % s function execution, print in positive order the two-way non-circular linked list at the end of the lead node is successfully created \ n ",__ FUNCTION __);}
(5) print the linked list in reverse order
// 4. Print the linked table void PrintReverseList (Node * pHead, Node * pTail) {Node * pMove; pMove = pTail-> prior; while (pMove! = PHead) {printf ("% d", pMove-> element); pMove = pMove-> prior;} printf ("\ n % s function execution, two-way non-circular linked list of the End Node of the leading node is successfully created in reverse order \ n ",__ FUNCTION __);}
(6) Clear the node and make it an empty table
// 5. clear all elements in the linked list to make it an empty table void ClearList (Node * pHead, Node * pTail) {Node * pMove; pMove = pHead-> next; while (pMove! = PTail) {pHead-> next = pMove-> next; pMove-> next-> prior = pHead; free (pMove); pMove = pHead-> next ;} printf ("% s FUNCTION execution, two-way non-cyclic linked list cleared successfully \ n" ,__ FUNCTION __);}
(7) Calculate the length of the linked list
// 6. Calculate the length of the linked list. int SizeList (Node * pHead, Node * pTail) {int I = 0; Node * pMove; pMove = pHead-> next; while (pMove! = PTail) {I ++; pMove = pMove-> next;} printf ("% s FUNCTION execution, the chain table length is % d \ n" ,__ FUNCTION __, i); return I ;}
(8) judge whether the linked list is empty
// 7. determines whether the bidirectional non-cyclic linked list of the End Node of the lead Node is null. If it is null, 1 is returned. Otherwise, 0 int IsEmptyList (Node * pHead, Node * pTail) is returned) {if (pHead-> next = pTail) {printf ("% s FUNCTION execution, the current linked list is empty \ n" ,__ FUNCTION _); return 1 ;} printf ("% s FUNCTION execution, the current linked list is not blank \ n" ,__ FUNCTION _); return 0 ;}
(9) returns the pos position element in the linked list.
// 8. returns the element of the pos Node in the linked list. If-1 is returned, int GetElement (Node * pHead, Node * pTail, int pos) {int I = 1 is not found; node * pMove; pMove = pHead-> next; while (pMove! = PTail) {if (I = pos) {printf ("% s FUNCTION execution, element at position pos = % d is % d \ n" ,__ FUNCTION __, pos, pMove-> element); return pMove-> element;} I ++; pMove = pMove-> next;} printf ("% s function execution, failed to find position element at pos = % d \ n ",__ FUNCTION __, pos); return-1 ;}
(10) Find the node with the value of x. If yes, return the address.
// 9. search for the first element of the given value x from the linked list, and return the memory address of the data domain. Otherwise, NULLint * GetElemAddr (Node * pHead, Node * pTail, int x) is returned) {Node * pMove; pMove = pHead-> next; while (pMove! = PTail) {if (pMove-> element = x) {printf ("% s function execution, element memory address with the value of % d is 0x % x \ n ", __function __, x, & (pMove-> element); return & (pMove-> element);} pMove = pMove-> next ;} printf ("% s FUNCTION execution, failed to find the element address with the value of % d \ n" ,__ FUNCTION __, x); return NULL ;}
(11) change the pos node value to x
// 10. change the value of the pos Node in the linked list to xint ModifyElem (Node * pHead, Node * pTail, int pos, int x) {int I = 1; Node * pMove; pMove = pHead-> next; while (pMove! = PTail) {if (I = pos) {pMove-> element = x; printf ("% s function execution, change pos = % d position value to % d success \ n ",__ FUNCTION __, pos, x); return 1;} I ++; pMove = pMove-> next ;} printf ("% s FUNCTION execution, modifying pos = % d position element failed \ n" ,__ FUNCTION __, pos); return-1 ;}
(12) insert an element in the header
// 11. insert an element int InsertHeadList (Node * pHead, Node * pTail, int x) to the table header of the linked list {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node )); memset (pInsert, 0, sizeof (Node); pInsert-> element = x; pInsert-> prior = NULL; pInsert-> next = NULL; pInsert-> next = pHead-> next; pHead-> next-> prior = pInsert; pHead-> next = pInsert; pInsert-> prior = pHead; printf ("% s FUNCTION execution, insert % d in the header successfully \ n" ,__ FUNCTION __, x); return 1 ;}
(13) insert an element at the end of the table
// 12. insert an element int InsertTailList (Node * pHead, Node * pTail, int x) to the end of the table in the linked list {Node * pInsert; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> element = x; pInsert-> prior = NULL; pInsert-> next = NULL; pTail-> prior-> next = pInsert; pInsert-> prior = pTail-> prior; pInsert-> next = pTail; pTail-> prior = pInsert; printf ("% s FUNCTION execution, % d inserted at the end of the table \ n" ,__ FUNCTION __, x); return 1 ;}
(14) test code
Int main (int argc, const char * argv []) {Node * pHead; // header Node * pTail; // End Node InitialList (& pHead, & pTail ); createList (pHead, pTail); PrintList (pHead, pTail); PrintReverseList (pHead, pTail); SizeList (pHead, pTail); Combine (pHead, pTail); GetElement (pHead, pTail, 2); GetElemAddr (pHead, pTail, 5); ModifyElem (pHead, pTail, 2,111); PrintList (pHead, pTail); InsertHeadList (pHead, pTail, 100 ); printList (pHead, pTail); InsertTailList (pHead, pTail, 900); PrintList (pHead, pTail); ClearList (pHead, pTail); PrintList (pHead, pTail ); isEmptyList (pHead, pTail); return 0 ;}