linked lists/trees
by Matias Pecile · in Technical Issues · 12/14/2001 (8:25 am) · 7 replies
I know that a tree is a bunch of linked lists put together, and I know what a linked list is, data structures arranged together.
Now, what I am wondering is, how useful can these things be, what are their purpose in life...or C++... And I guess if someone has some nice commented code that uses linked lists so I can further understand it (I'm using a book, and my teacher says we won't cover these sections) so I have to do it on my own. BTW, I'm only 15 and not native to the U.S. (even though I live here) so be gentle with the lenguage, nothing fancy :D
Now, what I am wondering is, how useful can these things be, what are their purpose in life...or C++... And I guess if someone has some nice commented code that uses linked lists so I can further understand it (I'm using a book, and my teacher says we won't cover these sections) so I have to do it on my own. BTW, I'm only 15 and not native to the U.S. (even though I live here) so be gentle with the lenguage, nothing fancy :D
About the author
#2
Ryan
12/14/2001 (9:15 am)
Horray for cs132 concepts! that sort of stuff was part of my cs132 class last year. heh hehRyan
#3
Example:
We have a network of railroads, with a city in each node.
We want to get the cheapest route and the shortest route between two cities.
This is done by sorting all cities in a tree with the city we travel from as root. (The sorting is based on the available routes and the route traveling prices.)
Then we get both wanted routes just by searching the tree in two different ways.
//hyzen
12/14/2001 (10:06 am)
With lists and trees you can also easily solve a lot of problems, like optimizations.Example:
We have a network of railroads, with a city in each node.
We want to get the cheapest route and the shortest route between two cities.
This is done by sorting all cities in a tree with the city we travel from as root. (The sorting is based on the available routes and the route traveling prices.)
Then we get both wanted routes just by searching the tree in two different ways.
//hyzen
#4
12/14/2001 (11:52 am)
thanks guys, this cleared up a bunch of questions I had, now I am wondering, can you have linked lists made out of classes? and if so, how would you write that?
#5
Now I use the Standard Template Library (STL) when I need lists and similar functionality. STL comes with several compilers (including VC++). It's a little tricky to understand the STL and you need to know what a c++ template is, but there are several tutorials on it and it's definitively worth it!
//hyzen
12/14/2001 (12:15 pm)
I wrote a simple linked list class in c++ a while ago, I could send it to you if you want it.Now I use the Standard Template Library (STL) when I need lists and similar functionality. STL comes with several compilers (including VC++). It's a little tricky to understand the STL and you need to know what a c++ template is, but there are several tutorials on it and it's definitively worth it!
//hyzen
#6
The article talks about various ways to implement a stack but the discussion is directly applies to lists as well. Great read.
Sixteen Ways to Stack a Cat
--Rick
12/14/2001 (12:18 pm)
Here is a great article from the guy who designed and implemented the C++ language, Bjarne Stroustrup.The article talks about various ways to implement a stack but the discussion is directly applies to lists as well. Great read.
Sixteen Ways to Stack a Cat
--Rick
#7
12/14/2001 (8:06 pm)
that would be great hyzen, and I know what a template is: like, it let's you make a function so that the type you send through it can be different everytime.
Torque 3D Owner Eric Armstrong
Bloody Tongue Games
The simplist reason for using a linked list is if you need to store a number of object together, like in an array, but you don't know how many you are going to need to store. a very basic structure for a linked list would look like this:
struct node { int data; node *next; };You would then have a pointer, usually called head, that points to the first node in the list. Each node then points to the next node, with the last node in the list next pointer being null.
The above structure if used in a linked list would act like an array of ints. The advantage is if you need to start anywhere from 1 to 100 ints, for an array you would have to declare the array with the full 100 unit capacity, wasting memory if it is only storing a single value. A linked list only uses as much memory as is needed to story the number of items currently being used.
I some of that helps, and that it is understandable. You can do a lot with linked lists, and once you have a handle on them, the applications will show themselves.
Eric