Replication of complex linked lists in c Language
// Header file # pragma oncetypedef int DataType; typedef struct ComplexNode {DataType _ data; // data struct ComplexNode * _ next; // pointer to next node struct ComplexNode * _ random; // point to random node} ComplexNode, * pComplexNode; void BuyNode (pComplexNode & pNode, DataType x); void PushBack (pComplexNode & pHead, DataType x); void CopyCplexList (pComplexNode pHead, pComplexNode & NewpHead); pComplexNode Find (pComplexNode pHead, DataType Data); Void PrintLinkList (pComplexNode pHead); // function file # include <stdio. h> # include "ComplexList. h "# include <assert. h> # include <malloc. h> // create the node void BuyNode (pComplexNode & pNode, DataType x) {pNode = (pComplexNode) malloc (sizeof (ComplexNode); pNode-> _ data = x; pNode-> _ next = NULL; pNode-> _ random = NULL;} // void PushBack (pComplexNode & pHead, DataType x) {pComplexNode tmp = pHead; if (pHead = NULL) {BuyNode (pHead, X); return;} while (tmp-> _ next! = NULL) {tmp = tmp-> _ next;} BuyNode (tmp-> _ next, x);} // copy void CopyCplexList (pComplexNode pHead, pComplexNode & NewpHead) to a complex linked list) {pComplexNode tmp = pHead; pComplexNode head = pHead; if (pHead = NULL) return; while (head! = NULL) {tmp = head-> _ next; BuyNode (NewpHead, head-> _ data); head-> _ next = NewpHead; NewpHead-> _ next = tmp; head = tmp;} head = pHead; while (head! = NULL) {tmp = head-> _ next; if (head-> _ random = NULL) tmp-> _ random = NULL; else tmp-> _ random = head-> _ random-> _ next; head = head-> _ next;} head = pHead; newpHead = head-> _ next; while (head! = NULL) {tmp = head-> _ next; head-> _ next = tmp-> _ next; head = tmp-> _ next; if (head = NULL) return; tmp-> _ next = head-> _ next ;}// Find pComplexNode Find (pComplexNode pHead, DataType Data) {pComplexNode tmp = pHead; assert (pHead ); while (tmp) {if (tmp-> _ data = Data) return tmp; tmp = tmp-> _ next;} return NULL ;} // output void PrintLinkList (pComplexNode pHead) {pComplexNode tmp = pHead; if (pHead = NULL) {p Rintf ("the linked list is empty! \ N ") ;}while (tmp) {printf (" % d ", tmp-> _ data); tmp = tmp-> _ next ;} printf ("\ n");} // main function of the test case # include <stdio. h> # include "ComplexList. h "void test1 () {pComplexNode pHead = NULL, NewpHead = NULL; PushBack (pHead, 1); PushBack (pHead, 2); PushBack (pHead, 3 ); pushBack (pHead, 4); PrintLinkList (pHead); Find (pHead, 1)-> _ random = Find (pHead, 3); Find (pHead, 2) -> _ random = Find (pHead, 4); Find (pHead, 3)-> _ random = Find (pHead, 2); Find (pHead, 4) -> _ random = Find (pHead, 1); CopyCplexList (pHead, NewpHead); PrintLinkList (NewpHead);} int main () {test1 (); return 0 ;}