Replication of complex linked lists in c Language

Source: Internet
Author: User

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

 

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.