473,320 Members | 1,857 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,320 software developers and data experts.

C++ Lists

46
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:

Expand|Select|Wrap|Line Numbers
  1. struct linked{
  2.   int element1;
  3.   int element2;
  4.   linked *next;
  5. };
  6.  
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
May 8 '07 #1
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

Expand|Select|Wrap|Line Numbers
  1. struct MyData {
  2.     enum DataTypeId {
  3.         DATA_TYPE_1_ID,
  4.         DATA_TYPE_2_ID
  5.     } type;
  6.     union DataUnion {
  7.         DATA_TYPE_1 type1;
  8.         DATA_TYPE_2 type2;
  9.     } data;
  10. }
  11.  
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.
May 8 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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>.
May 8 '07 #3
AdrianH
1,251 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. struct linked{
  2.   int element1;
  3.   int element2;
  4.   linked *next;
  5. };
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. struct linked{
  2.   int element1;
  3.   int element2;
  4. };
  5. list<linked> myList;
  6.  
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
May 8 '07 #4
sake
46
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:
Expand|Select|Wrap|Line Numbers
  1. struct linked{
  2.   int element1;
  3.   int element2;
  4. };
  5. list<linked> myList;
  6.  
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.
Expand|Select|Wrap|Line Numbers
  1. struct linked{
  2.   int element1;
  3.   int element2;
  4. };
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.   linked me;
  10.   list <linked> myList;
  11.   me.element1 = 6;
  12.   me.element2 = 2;
  13.   myList.push_back(me);
  14.   return 0;
  15. }
  16.  
From there, however, how do I print out the contents of the list?

-Austen
May 8 '07 #5
Ganon11
3,652 Expert 2GB
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).
May 8 '07 #6
AdrianH
1,251 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. struct linked{
  2.   int element1;
  3.   int element2;
  4. };
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.   linked me;
  10.   list <linked> myList;
  11.   me.element1 = 6;
  12.   me.element2 = 2;
  13.   myList.push_back(me);
  14.   return 0;
  15. }
  16.  
From there, however, how do I print out the contents of the list?

-Austen
Use an iterator. A simple method is to do this:
Expand|Select|Wrap|Line Numbers
  1. list<linked>::iterator end = myList.end()
  2. list<linked>::iterator item = myList.begin();
  3. for (; item != end; ++item) {
  4.   cout << item->element1 << endl;
  5. }
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
May 8 '07 #7
sake
46
Works great. Thanks alot :-)
May 8 '07 #8
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.
May 9 '07 #9
AdrianH
1,251 Expert 1GB
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
May 9 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
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? ...
41
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...
3
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...
1
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...
17
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...
16
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
51
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...
0
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...
3
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>
3
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 = , ...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
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...

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.