473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4016
"Chinmoy Mukherjee" <a1****@motorola.com> wrote in message
news:42***************@motorola.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.programming], 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****@motorola.com> wrote in message
news:42***************@motorola.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
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
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...
4
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...
1
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...
10
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
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):...
9
by: incredible | last post by:
how to sort link list of string
1
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>...
36
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...

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.