473,657 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to copy a link list

when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another
node
int value;
}

Jul 25 '05 #1
8 4035
"Chinmoy Mukherjee" <a1****@motorol a.com> wrote in message
news:42******** *******@motorol a.com...
when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another
node
int value;
}

Traverse list, create copy of each node, link nodes correctly
together.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 25 '05 #2
Chinmoy Mukherjee sade:
when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another
node
int value;
}


node * node::copy()
{
COPY THIS TO TEMP
IF NEXT THEN
LINK TEMP TO CALL NEXT COPY
ENDIF
RETURN TEMP
}

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 25 '05 #3
If that node *rand really points to a random other node you need to add
in an unique identifier (uid) in each node for copying purposes.
Then you a have two/three step copy process.
First create the new nodes with value and uid copied & next containing
a pointer to a new node.
Then traverse the newly created list adding the rand values.
pseudocode
dummy = first copied node
while dummy->uid != original node->rand->uid dummy =
dummy->next;
copied node->rand = dummy;

Third step is optional, assigning unique uids to the copied nodes.

Jul 25 '05 #4
* Chinmoy Mukherjee:
when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another node
int value;
}


Nitpick: missing semicolon.

Since this is not a C++ question but a general programming question I've
cross-posted this to [comp.programmin g], with follow-up there.

Assumption 1. What you have is presumably a singly linked list embedded in a
general graph, where each node has at most two outgoing connections, and at
most one node has zero outgoing connections.

Assumption 2. Copying the singly linked list structure is also presumably
easy for you, whereas the 'rand' connections are problematic.

Assumption 3. And finally, the reason they're problematic is presumably that
you have some constraints, like (3.1) you want this done in time O(n) or
thereabouts, where n is the number of nodes, and (3.2) you can't add
additional fields.

With no limitations on the graph structure other than what's mentioned above
it seems your only option is to use an associative container. Current
standard C++ doesn't have hash tables in the standard library, but they're
common in actual implementations . If you want to only use standard
containers a std::map will do the job, but then with total time O(n log n)
(cirka).

One way is to first copy only the singly linked list structure, noting in
the associative container, for each node copied, an association from the
original's pointer value to the copy's pointer value; then traverse the
original list and update the 'rand' pointers in the copy.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #5
The cleanest way:
Provide iterators, use STL algorithms

Ben
when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another
node
int value;
}

Jul 25 '05 #6
Thanks Jakob.
But how to I remember the rand poniters of each node
the idea is to retain the strcuture

For example, assume we create a link list of size 5

now assign node0->rand = node4
node1->rand = node0
node2->rand = node3
node3->rand = node2

Now question is how do I copy the link list to
another link list so that the overall structure is retained.
Regards,
Chinmoy

Jakob Bieling wrote:
"Chinmoy Mukherjee" <a1****@motorol a.com> wrote in message
news:42******** *******@motorol a.com...
when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another
node
int value;
}


Traverse list, create copy of each node, link nodes correctly
together.

hth
--
jb

(reply address in rot13, unscramble first)


Jul 26 '05 #7
* Chinmoy Mukherjee:
[top-posting]
[extranous quoting]
Please don't top-post in this group

Please don't quote irrelevant material.

Read the FAQ.
* Chinmoy Mukherjee: * Jakob Bieling:
* Chinmoy Mukherjee:
when the node structure is like

struct node {
node *next;
node *rand; (// random is supposed to point to another
node
int value;
}


Traverse list, create copy of each node, link nodes correctly
together.


Thanks Jakob.
But how to I remember the rand poniters of each node
the idea is to retain the strcuture


Already answered in this thread, why don't you read the answers?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 26 '05 #8
> Now question is how do I copy the link list to
another link list so that the overall structure is retained.
Regards,
Chinmoy


Don't you read replies given to your question?
I and Steinbach gave you a way to do that.

Jul 26 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2817
by: Neo | last post by:
Hi Frns, Could U plz. suggest me how can i reverse a link list (without recursion)??? here is my code (incomplete): #include<stdio.h>
11
1478
by: Puneet | last post by:
Hi, I have to know how to delete a node in link list. i have a single link list like this 1->2->3->4->5->6->7 now i dont know head pointer. I know only the pointer which is pointing to item 5 in above list. Now i want to delete the item 5 so how i will
4
606
by: emanshu, Munish Nayyar | last post by:
Hi all, i am looking for different solutions for the fow:- problems please let me know if anyone have any idea. problem 1:- we have link list ( singly link list ), in which some node( say X node) is pointing to some previous node ( say E) and i want to search for node which is pointing back to previous nodes. problem 2:- there is some link list( singlu link list ) and I have
1
2079
by: sri2097 | last post by:
Hi all, I have written a Link list implementation in Python (Although it's not needed with Lists and Dictionaries present. I tried it just for the kicks !). Anyway here is the code - # Creating a class comprising of node in Link List. class linklist: def __init__(self, data=None,link=None): self.data = data self.link = link
10
9771
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
4
1751
by: bitshadow | last post by:
I've been working on a link list implementation and I'm driving myself crazy trying to understand something. assuming i have a list and i have the following quasi pseudocode: add(list *head): if(head==NULL) head=newnode else while (head)
9
4430
by: incredible | last post by:
how to sort link list of string
1
6559
by: ahoway | last post by:
I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far..... Thank you in advance. #include <iostream> #include "stdafx.h" using namespace std; class IntNode
36
2886
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray looks like this: typedef struct { vector origin; /* vector is an array of 3 doubles */
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.