Linked Listed

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Linked Listed

Postby merixa » Fri Oct 07, 2005 7:48 pm

Hi guys

I want use the C++ STL to create a Linked Listed that has two items in a single node.

for example: an int and a float

So a single list can hold two different types.

How can I create this linked list.

Thank you for your help
User avatar
merixa
 
Posts: 4
Joined: Sun Sep 25, 2005 6:47 pm

Postby MXP » Fri Oct 07, 2005 7:50 pm

Code: Select all
struct two_item_thing {
  int the_int;
  float the_float;
};

std::list< two_item_thing > a_two_itemed_list;
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby merixa » Fri Oct 07, 2005 8:12 pm

Colin Jeanne wrote:
Code: Select all
struct two_item_thing {
  int the_int;
  float the_float;
};

std::list< two_item_thing > a_two_itemed_list;


Can you please tell me one more thing please. How can I insert into the linked list
two items.

list<two_item_thing> a_two_itemed_list;

a_two_itemed_list.push_back( ); // I don't know how to insert items

Thanks
User avatar
merixa
 
Posts: 4
Joined: Sun Sep 25, 2005 6:47 pm

Postby MXP » Fri Oct 07, 2005 8:54 pm

Code: Select all
two_item_thing thing;
thing.the_int = 5;
thing.the_float = 5.5;

a_two_itemed_list.push_back(thing);
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby Alvaro » Fri Oct 07, 2005 9:43 pm

You can also provide a constructor and do the same thing using a temporary object.
[syntax="cpp"]struct two_item_thing {
int the_int;
float the_float;

two_item_thing(int the_int, float the_float)
:the_int(the_int),the_float(the_float){}
};

std::list< two_item_thing > a_two_itemed_list;

a_two_itemed_list.push_back(two_item_thing(5,5.5));
[/syntax]
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 2 guests