Replication of complex linked lists

Source: Internet
Author: User
Question: copying complex linked lists. In a complex linked list, each node has a next pointer pointing to the next node, and a sibling pointing to any node or null in the linked list. Complex linked list:

Ideas:It can be divided into three steps: 1. create a corresponding n 'based on each node N of the original linked list, and place n' behind N 2. set the sibling pointer of the copied node to assume that p is a node of the original linked list, the corresponding node p '-> sibling of the copied linked list is equal to P-> sibling-> next3. split the linked list obtained in step 2 into two linked lists, and the odd digit is the original linked list, even bits are copies linked lists.
Code:
/* Copy a complex linked list by rowandjj2014/8/6 */# include <iostream> using namespace STD; typedef struct _ node _ {int data; struct _ node _ * next; struct _ node _ * sibling;} node, * pnode; // -------------------------------------- // Step 1: Create the corresponding 'n' according to each node N of the original linked list ', and put n' behind n void clonenodes (pnode phead) {pnode ptemp = phead; while (ptemp! = NULL) {pnode pnew = (pnode) malloc (sizeof (node); If (! Pnew) {exit (-1);} pnew-> DATA = ptemp-> data; pnew-> next = ptemp-> next; pnew-> sibling = NULL; ptemp-> next = pnew; ptemp = pnew-> next ;}// Step 2: Set the sibling pointer of the copied node. // assume that p is a node of the original linked list, the corresponding node p '-> sibling of the copied linked list is equal to P-> sibling-> nextvoid connectsiblingnodes (pnode phead) {pnode ptemp = phead; while (ptemp! = NULL) {pnode pclone = ptemp-> next; If (ptemp-> sibling! = NULL) {pclone-> sibling = ptemp-> sibling-> next;} ptemp = pclone-> next ;}// Step 3: split the linked list obtained in step 2 into two linked lists. The odd digits are the original linked list, and the even digits are the pnode reconnectnodes (pnode phead) {pnode ptemp = phead; pnode pclonehead = NULL; // clone the head node pnode pclonenode of the linked list = NULL; If (ptemp! = NULL) {pclonehead = pclonenode = ptemp-> next; // locate the head node ptemp-> next = pclonehead-> next; ptemp = ptemp-> next ;} while (ptemp! = NULL) {pclonenode-> next = ptemp-> next; pclonenode = pclonenode-> next; ptemp-> next = pclonenode-> next; ptemp = ptemp-> next ;} return pclonehead;} // encapsulate the pnode clone (pnode phead) {If (phead = NULL) {return NULL;} clonenodes (phead); connectsiblingnodes (phead ); return reconnectnodes (phead);} // ----------------------------------- // create a single-chain table void createlinkedlist (pnode * phead, int num) {If (Num <= 0) {return;} int data; * Phead = (pnode) malloc (sizeof (node); If (! Phead) {exit (-1);} CIN> data; (* phead)-> DATA = data; (* phead)-> next = NULL; (* phead) -> sibling = NULL; num --; pnode ptemp = * phead; while (Num> 0) {CIN> data; pnode pnew = (pnode) malloc (sizeof (node); If (! Pnew) {exit (-1);} pnew-> DATA = data; pnew-> next = NULL; pnew-> sibling = NULL; ptemp-> next = pnew; ptemp = pnew; num -- ;}// create a complex linked list. For test convenience, void createcomplexlist (pnode * phead) is disabled) {// create a single-chain table createlinkedlist (phead, 5); // then set the sibling pointer (* phead) for each node-> sibling = (* phead) -> next; (* phead)-> next-> sibling = (* phead)-> next; pnode ptemp = (* phead)-> next; ptemp-> sibling = (* phead)-> next;} // Print the linked table void display (pnode phead) {pnode ptemp = phead; while (ptemp! = NULL) {cout <ptemp-> data <"; ptemp = ptemp-> next;} cout <Endl ;}// destroy the linked table void destroy (pnode phead) {If (phead = NULL) {return;} pnode P = phead, Q; while (P! = NULL) {q = p-> next; free (p); P = Q ;}} int main () {pnode phead = NULL; createcomplexlist (& phead ); cout <"original linked list:" <Endl; display (phead); pnode pclonehead = clone (phead); If (pclonehead! = NULL) {cout <"clone linked list as follows" <Endl; display (pclonehead ); cout <"print the value of each node in the clone list and the value of the sibling node as follows \ n"; pnode P = pclonehead; while (P! = NULL) {cout <p-> data <""; if (p-> sibling! = NULL) {cout <p-> sibling-> data <Endl ;}else {cout <"null \ n" ;}p = p-> next ;}} destroy (phead); Return 0 ;}


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.