Archived

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

augustov3

linked lists don't make any sense to me...

Recommended Posts

I tried so many articles and my course book and teachers notes, but still I don''t really get how to use linked lists and what they are really used for. Any easier explanation out there! An example would help alot. "I haven''''t failed, I''''ve found 10,000 ways that don''''t work." --Thomas Edison--

Share this post


Link to post
Share on other sites
Linked lists are a way to store variable amounts of data. For example, if you''d have a lot of names you should store, you could use an array if you''d know how many they are. If you have 100 names, you can write char* names[100]; But if you don''t know at first how many they are, or you''d know the number can vary on every execution, from, say, 1 to 10000, you could not want to use an array of 10000 elements when the most probable amount of data is 10, nor you could want to use an array with 10 elements, so making your program useless for 10000 names...

Then, using linked lists you can have variable amounts of data in a array like structure (to say it simple)...

Also linked lists are very efficient to insert and delete elements (nodes) in the middle of the structure...

This explanation is very simple, but should be enough to understand the main idea.


--DK
--H. Hernán Moraldo
http://www.hhm.com.ar/

Share this post


Link to post
Share on other sites
An example to illustrate:


class linkedNames
{
public:
linkedNames();
~linkedNames();

private:
char * name;
linkedNames * nextName;
};


...this is just a class that shows a basic linked list.
The key is the linkedNames pointer inside of the linkedNames class. If you didn''t know you could use a class inside itself like that, now you do...that''s how linked lists work.

Share this post


Link to post
Share on other sites
Linked lists are a good way to store data for things like a queue where you insert and access elements at one side of the list or if you iterate thru the entire contents. They are not fast for operations where you need to access a single data object based on some type of index because you have to loop thru the data from the beginning until you get to your target.

So for things like Queues, FIFO buffers and stacks, linked lists work very well. For things where you would need to lookup single elements that are not at the head or tail of the list, they can be slow.

Share this post


Link to post
Share on other sites
Hey guys thanks for the information. I think now I got a better idea of what linked lists could be used for.

Do you guys know of any good online tutorials, where I can find more examples and information on this topic(linked lists).

Thanks anyways.

"I haven''''t failed, I''''ve found 10,000 ways that don''''t work."
--Thomas Edison--

Share this post


Link to post
Share on other sites
hi,

i don''t know of an on-line resource, but searching for "linked list" on the internet might help.

otherwise, you could buy a book by "Leen Ammeraal" (who used to be my C/C++ teacher) from fatbrain.com.

you can find more details at http://www.planetzztpp.com/store.html

good luck.
wep

WeP
http://www.planetzztpp.com

Share this post


Link to post
Share on other sites
Personally I don''t think DoctorK''s example with arrays of varying size is really accurate in explaining the use of linked lists. You can always dynamically allocate an array at runtime once your program determines the size of array it needs. It would be more accurate to say that linked lists are useful when the amount of data you are going to store varies during your program''s execution. You can easily add and remove elements from a linked list - this is not true with arrays.

Share this post


Link to post
Share on other sites