Hi, Just a quick question about lists.
This seemed a bit elementary at first, but nothing turned up on the web:
How do I get a linked list in C++ with multiple elements.
I need to copy this sort of thing, but using the <list> header: -
struct linked{
-
int element1;
-
int element2;
-
linked *next;
-
};
-
Is this even possible? If so, can I mix up the types of data? Like have an integer and a string element in the same list.
Thanks,
Austen
9 2313 Banfa 9,065
Expert Mod 8TB
If you are using C++ then you would be much better off using a <list> which will do a lot of the hardwork for you.
If you want to hold different data types then the easiest method is to use a structure with an enum and a union in it -
struct MyData {
-
enum DataTypeId {
-
DATA_TYPE_1_ID,
-
DATA_TYPE_2_ID
-
} type;
-
union DataUnion {
-
DATA_TYPE_1 type1;
-
DATA_TYPE_2 type2;
-
} data;
-
}
-
However I would only recommend that only if your data types are of similar sizes, I worked with a system once where every data type (1000+ of them) where held in structure like this from 4 byte integers up to structures of 1500+ bytes. Since it is a union internally every time you allocate data for one it has to allocate the data for the largest element so when allocating data for a 4 byte int it was allocating 1500+ bytes. Couple with the fact it's pointer usage was abysmal and it kept overwriting pointers that still pointed to allocated data meant that it leaked memory like a hula-hoop.
Is this even possible? If so, can I mix up the types of data? Like have an integer and a string element in the same list.
You can do this but when you mix types the different types need different handling. Generally, a list with ints and strings can't be sorted because there's no way to compare elements. However, if you insist, research the MIcrosoft VARIANT discriminated union.
You will be much better off with a list<int> and a list<string>.
Hi, Just a quick question about lists.
This seemed a bit elementary at first, but nothing turned up on the web:
How do I get a linked list in C++ with multiple elements.
I need to copy this sort of thing, but using the <list> header: -
struct linked{
-
int element1;
-
int element2;
-
linked *next;
-
};
-
Is this even possible? If so, can I mix up the types of data? Like have an integer and a string element in the same list.
Thanks,
Austen
I think (but I am not positive) that the others have misunderstood your question. Are you asking "How do I use list to hold a bunch of different elements?" If so, generate a struct and then pass that struct to the list as a template parameter: - struct linked{
-
int element1;
-
int element2;
-
};
-
list<linked> myList;
-
Like any other struct, you can put whatever you want in it. Including strings, ints, doubles, user defined types, etc...
Let me know if I have understood you correctly.
Adrian
I think (but I am not positive) that the others have misunderstood your question. Are you asking "How do I use list to hold a bunch of different elements?" If so, generate a struct and then pass that struct to the list as a template parameter: - struct linked{
-
int element1;
-
int element2;
-
};
-
list<linked> myList;
-
Like any other struct, you can put whatever you want in it. Including strings, ints, doubles, user defined types, etc...
Let me know if I have understood you correctly.
Adrian
Thanks for your reply.
So after I've declared that, how do I access the members? The only way I can think of is to declare a linked variable. -
struct linked{
-
int element1;
-
int element2;
-
};
-
-
using namespace std;
-
-
int main(){
-
linked me;
-
list <linked> myList;
-
me.element1 = 6;
-
me.element2 = 2;
-
myList.push_back(me);
-
return 0;
-
}
-
From there, however, how do I print out the contents of the list?
-Austen
If you want to have a variable that knows how to print itself, you should use a class and add the functions yourself (or overload the << operator).
Thanks for your reply.
So after I've declared that, how do I access the members? The only way I can think of is to declare a linked variable. -
struct linked{
-
int element1;
-
int element2;
-
};
-
-
using namespace std;
-
-
int main(){
-
linked me;
-
list <linked> myList;
-
me.element1 = 6;
-
me.element2 = 2;
-
myList.push_back(me);
-
return 0;
-
}
-
From there, however, how do I print out the contents of the list?
-Austen
Use an iterator. A simple method is to do this: - list<linked>::iterator end = myList.end()
-
list<linked>::iterator item = myList.begin();
-
for (; item != end; ++item) {
-
cout << item->element1 << endl;
-
}
Where I put element1, you can put any member element or member function you wish that is used by your struct. An iterator works very much like a pointer.
If you are outputing all of the data in a consistant way, overload the operator <<() function for use with that struct/class. (if you haven't realised it yet, struct and class are almost identical).
Adrian
Works great. Thanks alot :-)
it is not reply just about link list.
I want comlete code of link list through class in c++. Pls send me as soon as possible on my e-mail.
it is not reply just about link list.
I want comlete code of link list through class in c++. Pls send me as soon as possible on my e-mail.
Sorry, we don't give out the complete code here on TSDN. But you can ask for help where you are having difficulty.
If you really want the complete code though, try the header file that came with the compiler. But I will warn you, it is most likely fairly difficult to read. It would also get you a definite F in any course you are taking as it would be way beyond your abilities, considering that you are asking use to provide you code for a list ;) (which is fairly simple BTW).
Good luck with that :D,
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave H |
last post by:
Hello,
I have a query regarding definition lists. Is it good practice
semantically to use the dt and dd elements to mark up questions and
answers in a frequently asked questions list, or FAQ?
...
|
by: Odd-R. |
last post by:
I have to lists, A and B, that may, or may not be equal. If they are not
identical, I want the output to be three new lists, X,Y and Z where X has
all the elements that are in A, but not in B, and...
|
by: s_subbarayan |
last post by:
Dear all,
1)In one of our implementation for an application we are supposed to
collate two linked lists.The actual problem is like this:
There are two singularly linked lists, the final output...
|
by: Simon Forman |
last post by:
I've got a function that I'd like to improve.
It takes a list of lists and a "target" element, and it returns the set
of the items in the lists that appear either before or after the target...
|
by: Gal Diskin |
last post by:
Hi,
I am writing a code that needs to iterate over 3 lists at the same
time, i.e something like this:
for x1 in l1:
for x2 in l2:
for x3 in l3:
print "do something with", x1, x2, x3
What I...
|
by: Michael M. |
last post by:
How to find the longst element list of lists?
I think, there should be an easier way then this:
s1 =
s2 =
s3 =
if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist längster
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: Joeyeti |
last post by:
Hi fellow VB knowers (I am but a learner still).
I have a question for you which I struggle with. I need to convert nested Lists in MS WORD (whether numbered or bulleted or mixed) from their...
|
by: =?Utf-8?B?QWRhbSBN?= |
last post by:
Hi all,
What is the syntax for a method that accepts a list of generic lists?
For example, I have a List that contains the following lists:
List<Car>
List<Boat>
List<Plane>
|
by: Qbert16 |
last post by:
Hi, I'm quite new to python and am looking for help with lists inside lists
This is an example I'm trying to do. I have the following thesaurus set...
thesaurus = ,
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |