[Programming question] copying complex linked lists ☆

Source: Internet
Author: User

76. Replication of complex linked lists (linked lists and algorithms)
Question: there is a complex linked list, except that the node has a m_pnext pointer pointing to the next node,
There is also a m_psibling pointing to any node in the linked list or null. The node C ++ is defined as follows:
Struct complexnode
Int m_nvalue;
Complexnode * m_pnext;
Complexnode * m_psibling;
Complete the complexnode * clone (complexnode * phead) function to copy a complex linked list.


Idea: copy the data according to m_pnext, and then search for m_psibling from the traversal chain table.

# Include <stdio. h> # include <stdlib. h> typedef struct complexnode {int m_nvalue; complexnode * m_pnext; complexnode * m_psibling;} complexnode; complexnode * clone (complexnode * phead) // completely replicate {If (phead = NULL) {return NULL;} // first copy the header node complexnode * chead = (complexnode *) malloc (sizeof (complexnode); chead-> m_nvalue = phead-> m_nvalue; chead-> m_pnext = NULL; chead-> m_psibling = NULL; complexnode * x = phead-> M_pnext; complexnode * Y = chead; while (X! = NULL) // create all vertices Based on the next information and connect {Y-> m_pnext = (complexnode *) malloc (sizeof (complexnode )); y-> m_pnext-> m_nvalue = x-> m_nvalue; y-> m_pnext = NULL; y-> m_pnext-> m_psibling = NULL; X = x-> m_pnext; y = Y-> m_pnext;} // copy the pointer of m_psibling to X = phead; y = chead; while (X! = NULL) {If (X-> m_psibling = NULL) // copy the NULL pointer directly {Y-> m_psibling = NULL ;} else // a non-null pointer pointing to the nth node from the beginning {If (phead = x-> m_psibling) {Y-> m_psibling = chead;} else {complexnode * xx = phead; complexnode * YY = chead; while (XX-> m_pnext! = X-> m_psibling) {xx = XX-> m_pnext; YY = YY-> m_pnext;} y-> m_psibling = YY-> m_pnext ;}} X = x-> m_pnext; y = Y-> m_pnext;} return chead;} void createlist (complexnode * & phead) // create and add the relationship between the next {int data; scanf ("% d", & data); If (Data! = 0) {phead = (complexnode *) malloc (sizeof (complexnode); phead-> m_nvalue = data; phead-> m_pnext = NULL; phead-> m_psibling = NULL; createlist (phead-> m_pnext) ;}} void addsibling (complexnode * phead) // Add the relationship between psibling {int Len = 0; complexnode * x = phead; while (X! = NULL) {Len ++; X = x-> m_pnext;} X = phead; while (X! = NULL) {int n = 0; // number indicates p_sibling pointing to the nth node in the linked list printf ("Please input a number between 1-% d or 0 (means null) ", Len); scanf (" % d ", & N); If (n = 0) {X-> m_psibling = NULL;} else {complexnode * Y = phead; N = n-1; while (n! = 0) {Y = Y-> m_pnext; n --;} X-> m_psibling = y;} X = x-> m_pnext;} int main () {complexnode * phead = NULL; createlist (phead); addsibling (phead); complexnode * chead = clone (phead); Return 0 ;}


I found that my methods on the Internet are complicated and there is a clever method:


Method 3:

Clever, just traverse the linked list three times, the time cost is O (n), the space cost is 0, divided into three steps

1: traverse the original linked list, copy the n 'corresponding to node N, and insert it to the end of node N, as shown in

2: determine the direction of each m_psibling pointer. You only need to traverse the linked list to determine the direction of the m_psibling pointer of each node.

3: traverse it again to separate the original linked list and the copy linked list. The odd number is the original linked list, and the even number is the copy linked list.

The code on this website may be slightly faulty.

However, it is easy to write code after you know the idea.

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.