473,574 Members | 3,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked list within a linked list

Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in the
direction of a good article which would help me out? Because for
something that should be so simple, it's really got me stumped.

Thanks,
Josh

Sep 30 '06 #1
12 3937
"joshd" <jo***@joshd.ca wrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com
Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in
the direction of a good article which would help me out? Because for
something that should be so simple, it's really got me stumped.
I don't see how this question relates to the title of your post. There is
nothing in your description to suggest a linked list within a linked list.

More generally, stating the problem clearly would help. Is this a one-off
synchronisation of the lists or does it have to be done repeatedly as the
lists are added to? If so, how are the lists added to? Are the two classes
friends of each other? Is the "correspond ing node" the node that occurs in
the same position in the list?

Assuming the answer to the last question is yes, for a one-off
synchronisation , you can do it by iterating over both lists. The address of
a list item is given by

&(*iter)

where iter is the iterator. Iterating over both lists simultaneously means
you can retrieve the addresses of corresponding items at the same time and
then store a pointer in each. Obviously the T1 in

list<T1list1;

will need to have a member variable that is a pointer to the T2 type in

list<T2list2;

and vice versa.

If you want synchronisation on the run, then you will need to add to both
lists at the same time (or else do a search over each list to find the nodes
that are "corresponding" ). If you add to the end of both lists, then you can
get the address from

&(*list1.end ()) and &(*list2.end ())

after you have made the addition. Adding at the beginning is analogous.

If you add in the middle using insert to add a single element, then insert
returns an iterator pointing to the new element. You retrieve a pointer from
this iterator following the same pattern as illustrated above.

--
John Carson


Sep 30 '06 #2
joshd wrote:
Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in the
direction of a good article which would help me out? Because for
something that should be so simple, it's really got me stumped.
This is just a proof of concept, i.e. how to do it using std::list.
These are using iterators to point to one another.

#include <list>

// declare 2 structs
struct A;
struct B;

// define the respective list type
typedef std::list<A Alist_t;
typedef std::list<B Blist_t;

// define the classes
struct A
{
Blist_t::iterat or bitr;

A( const A & ) {} // no copying bitr
A() {}
};

struct B
{
Alist_t::iterat or aitr;
B( const B & ) {} // no copying aitr !
B() {}
};

// some silly example code
void tst()
{
Alist_t al;
Blist_t bl;

// make an A element
al.push_front( A() );

// make a B element
bl.push_front( B() );

// Set up iterators to point to each other
al.front().bitr = bl.begin();
bl.front().aitr = al.begin();

}

int main()
{
tst();
}
Sep 30 '06 #3
I apologize that I wasnt too clear with my first post, Im still trying
to grasp the linked list concept. Maybe this visual representation I
jotted out, with a parent-child scenario, will help you understand my
needs.

parent linklist
|
parent node -child linklist + child2 list + child3 list + ....
parent node -child linklist + child2 list + ....
parent node -child linklist + child2 list + ....

Can this be done without iterators? or should I study more to wrap my
mind around STL and iterators?

Thanks for all your help!

Josh

Sep 30 '06 #4
Gianni Mariani wrote:
joshd wrote:
>Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
correspondin g node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in the
direction of a good article which would help me out? Because for
something that should be so simple, it's really got me stumped.

This is just a proof of concept, i.e. how to do it using std::list.
These are using iterators to point to one another.

#include <list>

// declare 2 structs
struct A;
struct B;

// define the respective list type
typedef std::list<A Alist_t;
typedef std::list<B Blist_t;

Formally, this is undefined behavior because A and B are incomplete types
(see [17.4.3.6/1 and 2]). Accordingly, the code fails with g++ and stdlibc++
when g++ is compiled with --enable-concept-check.

// define the classes
struct A
{
Blist_t::iterat or bitr;

A( const A & ) {} // no copying bitr
A() {}
};

struct B
{
Alist_t::iterat or aitr;
B( const B & ) {} // no copying aitr !
B() {}
};

// some silly example code
void tst()
{
Alist_t al;
Blist_t bl;

// make an A element
al.push_front( A() );

// make a B element
bl.push_front( B() );

// Set up iterators to point to each other
al.front().bitr = bl.begin();
bl.front().aitr = al.begin();

}

int main()
{
tst();
}

Best

Kai-Uwe Bux
Oct 1 '06 #5
"joshd" <jo***@joshd.ca wrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com
I apologize that I wasnt too clear with my first post, Im still trying
to grasp the linked list concept. Maybe this visual representation I
jotted out, with a parent-child scenario, will help you understand my
needs.

parent linklist
|
parent node -child linklist + child2 list + child3 list + ....
parent node -child linklist + child2 list + ....
parent node -child linklist + child2 list + ....
Still not clear. You have used three symbols, | -and +. I think I have got
the first two, but what does + denote? And what does the switch from
"linklist" to "list" signify?

Is it simply that you have a parent linked list and that each node in the
parent linked list contains a linked list?
Can this be done without iterators? or should I study more to wrap my
mind around STL and iterators?
If you are writing your own linked list, then you can avoid iterators. If
you are using the standard library linked list, then iterators are
fundamental. Writing your own linked list can be a useful learning exercise,
but using the standard library containers is almost always preferable for
any program that isn't a learning exercise. You should definitely learn the
standard library containers, along with iterators.

--
John Carson

Oct 1 '06 #6
Kai-Uwe Bux wrote:
....
>
Formally, this is undefined behavior because A and B are incomplete types
(see [17.4.3.6/1 and 2]). Accordingly, the code fails with g++ and stdlibc++
when g++ is compiled with --enable-concept-check.
Ah - well, this makes lists much stl containers much less
interesting...s igh.

I'd better go back and finish up the Austria containers then.

Just as a matter of interest, do you know why does the standard need this ?

BTW - the version of the gcc compiler I have does not support
--enable-concept-check.
Oct 1 '06 #7
Kai-Uwe Bux wrote:
....
>
Formally, this is undefined behavior because A and B are incomplete types
(see [17.4.3.6/1 and 2]). Accordingly, the code fails with g++ and stdlibc++
when g++ is compiled with --enable-concept-check.

OK - this should conform now.

#include <list>

template <typename T>
struct Foo
{
// declare 2 structs
struct A;
struct B;

// define the respective list type
typedef std::list<A Alist_t;
typedef std::list<B Blist_t;

// define the classes
struct A
{
typename Blist_t::iterat or bitr;

A( const A & ) {} // no copying bitr
A() {}
};

struct B
{
typename Alist_t::iterat or aitr;
B( const B & ) {} // no copying aitr !
B() {}
};
};

typedef Foo<int X;

// some silly example code
void tst()
{
X::Alist_t al;
X::Blist_t bl;

// make an A element
al.push_front( X::A() );

// make a B element
bl.push_front( X::B() );

// Set up iterators to point to each other
al.front().bitr = bl.begin();
bl.front().aitr = al.begin();

}

int main()
{
tst();
}
Oct 1 '06 #8
Gianni Mariani wrote:
Kai-Uwe Bux wrote:
...
>>
Formally, this is undefined behavior because A and B are incomplete types
(see [17.4.3.6/1 and 2]). Accordingly, the code fails with g++ and
stdlibc++ when g++ is compiled with --enable-concept-check.

Ah - well, this makes lists much stl containers much less
interesting...s igh.
Yeah, it's a real bummer. I once had a nice caching container (generically
implementing a last recently used drop policy) but it suffers from this
flaw.
I'd better go back and finish up the Austria containers then.
Huh?

Just as a matter of interest, do you know why does the standard need this?
It does not: most implementation will work just fine and implementors have
to put in extra code to make it fail :-)

However, the standard just takes the easy wording option: since templates
like pair need complete types, the standard just put a general requirement.
Also, at the time they were just careful: you can always lift requirements
in future version of the standard, but you cannot tighten them. Probably, a
proposal to replace the general requirement by more finetuned ones could
pass.

BTW - the version of the gcc compiler I have does not support
--enable-concept-check.
--enable-concept-check is a built option, you have to pass it to the
configure script before you build g++ from the source.
Best

Kai-Uwe Bux
Oct 1 '06 #9
Gianni Mariani wrote:
Kai-Uwe Bux wrote:
...
>>
Formally, this is undefined behavior because A and B are incomplete types
(see [17.4.3.6/1 and 2]). Accordingly, the code fails with g++ and
stdlibc++ when g++ is compiled with --enable-concept-check.


OK - this should conform now.

#include <list>

template <typename T>
struct Foo
{
// declare 2 structs
struct A;
struct B;

// define the respective list type
typedef std::list<A Alist_t;
typedef std::list<B Blist_t;
Still incomplete types.
// define the classes
struct A
{
typename Blist_t::iterat or bitr; // line 17

A( const A & ) {} // no copying bitr
A() {}
};

struct B
{
typename Alist_t::iterat or aitr; // line 25
B( const B & ) {} // no copying aitr !
B() {}
};
};

typedef Foo<int X;

// some silly example code
void tst()
{
X::Alist_t al; // line 36
X::Blist_t bl;

// make an A element
al.push_front( X::A() );

// make a B element
bl.push_front( X::B() );

// Set up iterators to point to each other
al.front().bitr = bl.begin();
bl.front().aitr = al.begin(); // line 47

}

int main()
{
tst();
}
[Line numbers added for reference]

Still fails using concept checks:

mariani_002.cc: In instantiation of 'Foo<int>::B':
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++
/4.1.1/bits/boost_concept_c heck.h:216: instantiated
from '__gnu_cxx::_SG IAssig
nableConcept<Fo o<int>::B>'
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++
/4.1.1/bits/stl_list.h:402: instantiated from 'std::list<Foo< int>::B,
std::all
ocator<Foo<int> ::B'
mariani_002.cc: 17: instantiated from 'Foo<int>::A'
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++
/4.1.1/bits/boost_concept_c heck.h:216: instantiated
from '__gnu_cxx::_SG IAssig
nableConcept<Fo o<int>::A>'
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++
/4.1.1/bits/stl_list.h:402: instantiated from 'std::list<Foo< int>::A,
std::all
ocator<Foo<int> ::A'
mariani_002.cc: 36: instantiated from here
mariani_002.cc: 25: error: no type named 'iterator' in 'class
std::list<Foo<i nt>:
:A, std::allocator< Foo<int>::A'
mariani_002.cc: In function 'void tst()':
mariani_002.cc: 47: error: 'struct Foo<int>::B' has no member named 'aitr'
Best

Kai-Uwe Bux

Oct 1 '06 #10

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

Similar topics

11
3084
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
7
2601
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the...
10
2498
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The problem I get is that I am trying to link a struct that I have defined and its refusing to link. I have tried casting my struct into char * but...
4
4271
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that...
6
16125
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: struct node { int x; struct node *next; };
5
3354
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran...
22
8021
by: joshc | last post by:
In an interview for an embedded software position recently I was asked to write code, in C, for printing the contents of a linked list backwards. After a few minutes I came up with the recursive solution. Being that recursion is frowned upon in embedded software, the answer was not what the interviewer expected, but alas it was correct. I...
1
6177
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the linked list Delete() – Deletes a string from the linked list Search() – Search for a string and return 1 if found otherwise return 0. isEmpty() –...
1
1321
by: vijit | last post by:
can anyone help me in this linked list please am bit confused 1. get_data: This function finds the data stored in the specified row and column of a 2 dimensional linked list. If data retrieval was successful return true otherwise return false. The prototype is given below: template <class T> bool get_data(int row, int col, T& data);...
0
7842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8110
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. ...
0
8273
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...
0
6514
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...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
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...
0
3775
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...
1
2277
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
0
1102
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...

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.