Linked Lists...

Started by
94 comments, last by AcidJazz 22 years ago
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
Why is 99% of the Comp Sci population guys, and the other 1% not female? ;/
Advertisement
Why code your own list? The STL list is tried and tested.

If you''re desperate to code your own though, try google.
ahhh well i forgot all about that, but ok =)
Why is 99% of the Comp Sci population guys, and the other 1% not female? ;/

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
Edo

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
Edo
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..."
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".
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..."
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
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..."

This topic is closed to new replies.

Advertisement