Dynamic Linked list of OBJECTS

Hello 2 evryone :)

I need to allocat a dynamic linked list of objects...
If someone can give me any possiable syntax i'll be glad...

thanks for all of you bro's.
1
2
#incude <list>
std::list< OBJ > l;
OK i read some matirial about the list

but let's get to the point...

If for example my class name is Gas_station and i want to creat a list of Gas_station Object's at size of (argc-2) what do I write down ?
is it

list<Gas_station> lst(argc-2,Gas_station() );

I alreadt know how to creat an object but not for a list...
I mean
1
2
Gas_station Gas1;
Gas1.**** //some method  


but how dose it works in the list ?
Last edited on
You use an iterator to traverse the list. If you dereference the iterator then you have the object and can use the methods you want.
1
2
3
4
list<Gas_station> l;
//...
list<Gas_station>::iterator it = l.begin();
it->some_method(); // equivalent to (*it).some_method(); 
you didn't understand me ...
I draw what I'm trying to do

any help will be great...

http://www.israup.net/images/b4b21ec6d8b59e9789ce16c873fd2c96.png
I write like this ...

1
2
3
4
5
6
7
8
9
10
	list<Gas_station> lst( 0,Gas_station() ); // Creates list 
	for( int counter=0 ; counter<3 ; counter++ )
	{
		Gas_station::Gas_station();//default constructor
		Gas_station Station1;//creat an object
		lst.push_front(Station1);//pushing it to the front of the list

                                // inserting the data to members

                 }


Is it right ?
you can write it as
1
2
3
4
5
6
7
8
9
    list<Gas_station> lst; // Creates list 
    for( int counter=0 ; counter<3 ; counter++ )
    {
        Gas_station Station1;//creat an object
        lst.push_front(Station1);//pushing it to the front of the list

                                // inserting the data to members

     }
no secrets there

EDIT: Note that 'push_front' takes a copy hence you have to set Station1 properly before you 'push_front'. Or you manipulate lst.front() afterwards.
Last edited on
GOOD !
thanks a lot bro's!
The code that you give as an example ... is it saving the Station1 members?

and if yes how do i get to their values?
and how I can chack if it works ?
The code that you give as an example ... is it saving the Station1 members?
yep

and if yes how do i get to their values?
list<Gas_station>::iterator (for example) or front() or back()

and how I can chack if it works ?
however, you can iterate and print the member or alike
Topic archived. No new replies allowed.