Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AcidJazz

Linked Lists...

This topic is 5957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Grr for some reason i forgot how to load and read from a linked list... Its been awhile ;/ struct LIST { int number; LIST *next; LIST *temp; }; err i understand the concept (well sorta or i''d figure it out ) but i just cant seem to remember Also if you wanna throw in how to do doubly linked lists i''d be ever so appreciative... I know a single linked list looks something like LIST *bah; bah = new LIST; bah->next = new LIST; bah->temp = bah->next; bah->next->next = new LIST; grr BAHHH im done thinking about this, its something like that, help? :0 thanks

Share this post


Link to post
Share on other sites
Advertisement
Why code your own list? The STL list is tried and tested.

If you''re desperate to code your own though, try google.

Share this post


Link to post
Share on other sites

Well, something like this will do,

class LinkedList {

public:
LinkedList();
~LinkedList();
void add(int n);

private:
int number;
LinkedList *next;

};

LinkedList::LinkedList() {
next = NULL;
}

LinkedList::~LinkedList() {
delete next; // this will call the destructor of the next item
} // and so on and so on...

void LinkedList::add(int n) {
if (next != NULL)
next->add(n);
else
number = n;
}

Edo

edotorpedo

Share this post


Link to post
Share on other sites

Well, something like this will do,

class LinkedList {

public:
LinkedList();
~LinkedList();
void add(int n);

private:
int number;
LinkedList *next;

};

LinkedList::LinkedList() {
next = NULL;
}

LinkedList::~LinkedList() {
delete next; // this will call the destructor of the next item
} // and so on and so on...

void LinkedList::add(int n) {
if (next != NULL)
next->add(n);
else
number = n;
}

Edo

edotorpedo

Share this post


Link to post
Share on other sites
Yeah, but with linked lists, etc. it''s lots of times better to not use the STL just because you can make the nodes more integrated with what you''re doing much more easily (IE derive the node from the base class to make lots of method calls simpler). Though you can get the same effect from multiple inheritance from both the STL and your class, but multiple inheritance isn''t exactly a good idea to work with when you don''t have to, especially since linked lists are so easy to make on your own.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

Share this post


Link to post
Share on other sites
quote:
Original post by Matt Calabrese
Yeah, but with linked lists, etc. it''s lots of times better to not use the STL just because you can make the nodes more integrated with what you''re doing much more easily (IE derive the node from the base class to make lots of method calls simpler). Though you can get the same effect from multiple inheritance from both the STL and your class, but multiple inheritance isn''t exactly a good idea to work with when you don''t have to, especially since linked lists are so easy to make on your own.


Dude, this is the second post in row.

STOP SPREADING MISINFORMATION.

Thank you.
-scott

ps. One more and I''m going to have to ask you change the "Programmer" in your sig to "Congrammer".

Share this post


Link to post
Share on other sites
Deriving a node from an already created object makes things alot easier to work with -- IE Passing it to a function as a refrence/pointer can be done directly if the parameter is one of the base class. You completely eliminate travering through a pointer on every single use. Sure, the STL is great, but I''ll take the ability to have simpler data to work with anyday, especially since linked lists are so simple to put together.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

Share this post


Link to post
Share on other sites
What the heck are you going on about? Show me one example where STL is just so cumbersome and impossible to use.

I don''t know about you, but I don''t think implementation gets any easier than:
typedef std::list< int > myIntList;

-scott

Share this post


Link to post
Share on other sites
Nono, not like that. Thake this example:

class Someclass
{
...
}

class SomeclassNode : public Someclass
{
just with added next node and methods, etc.
}

now, take the funciton/method:

Somefunction( Someclass& thingee );

Then, you can pass either a node or the original class directly, and of course, have all of the other benefits of inheritance and polymorphism.

Sure it''s easier to just use the STL, but I''d rather take the extra 2 minutes to make a the methods for the linked list and derive it from the base object. In the long run, it makes things a lot simpler.





--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!