views:

1154

answers:

12

Are data structures like linked lists something that are purely academic for real programming or do you really use them? Are they things that are covered by generics so that you don't need to build them (assuming your language has generics)? I'm not debating the importance of understanding what they are, just the usage of them outside of academia. I ask from a front end web, backend database perspective. I'm sure someone somewhere builds these. I'm asking from my context.

Thank you.

EDIT: Are Generics so that you don't have to build linked lists and the like?

+4  A: 

It will depend on the language and frameworks you're using. Most modern languages and frameworks won't make you reinvent these wheels. Instead, they'll provide things like List<T> or HashTable.

EDIT:

We probably use linked lists all the time, but don't realize it. We don't have to write implementations of linked lists on our own, because the frameworks we use have already written them for us.

You may also be getting confused about "generics". You may be referring to generic list classes like List<T>. This is just the same as the non-generic class List, but where the element is always of type T. It is probably implemented as a linked list, but we don't have to care about that.

We also don't have to worry about allocation of physical memory, or how interrupts work, or how to create a file system. We have operating systems to do that for us. But we may be taught that information in school just the same.

John Saunders
List<T> is implemented as an array. It dynamically resizes as necessary (doubling in length each time).
DancesWithBamboo
List<T> is not probably implemented as a linked list. It's definitely implemented as a array, as the front page (http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) says. Furthermore, you may not have to worry about data structures, but other business programmers do. And...maybe you should be too.
Matthew Flaschen
Yeah, you probably do have to care about that, to be honest.
mquander
@all: please contribute your reasons why we "business" programmers should care what's under the hood, given that it performs properly. It's what the OP really wanted to know.
John Saunders
@john Yes. Thank you. Why would I care other than to be well rounded and have that knowledge in case of emergency.
johnny
The question/answer looks language-agnostic, so you may want to specify the language/platform/environment for comments about XXX is implemented as YYYY.For example, in Java, List<T> is an interface that allows you to hide the underlying implementation in one place and thus hide this from parts of the code that shouldn't care. Java provides ArrayList<T> (most used) and LinkedList<T> (if needed) implementations of List<T>, but you may (rare) implement your own by extending AbstractList<T> (a List<T> starter kit) or implementing List<T> completely from scratch.
Bert F
@johnny: being well-rounded is good, when you have time for it. Knowing that linked lists exist may help you if you get to the point where your chosen list is a performance problem. You can look at the documentation and see how it's implemented. You may find you want to switch to a list that uses a linked list underneath. Until then, you mostly won't need to worry about it.
John Saunders
+3  A: 

Certainly. Many "List" implementations in modern languages are actually linked lists, sometimes in combination with arrays or hash tables for direct access (by index as opposed to iteration).

Linked lists (especially doubly linked lists) are very commonly used in "real-world" data structures.

I would dare to say every common language has a pre-built implementation of linked list, either as a language primitive, native template library (e.g. C++), native library (e.g. Java) or some 3rd party implementation (probably open-source).

That being said, several times in the past I wrote a linked list implementation from scratch myself when creating infrastructure code for complex data structures. Sometimes it's a good idea to have full control over the implementation, and sometimes you need to add a "twist" to the classic implementation for it to satisfy your specific requirement. There's no right or wrong when it comes to whether to code your own implementation, as long as you understand the alternatives and trade-offs. In most cases, and certainly in very modern languages like C# I would avoid it.

Another point is when you should use lists versus array/vectors or hash tables. From your question I understand you are aware of the trade-offs here so I won't go too much into it, but basically, if your main usage is traversing lists by-order, and the list size may vary significantly, a list may be a viable option. Another consideration is the type of insertion. If a common use case is "inserting in the middle", than lists have a significant advantage over arrays/vectors. I can go on but this information is in the classic CS books :)

Clarification: My answer is language agnostic and does not relate specifically to Generics which to my understanding have a linked list implementation.

Roee Adler
I'm confused. Are you saying that the "list" implementations (generics I think) are the linked lists that are commonly used? Or are linked lists something else that you have to commonly build? Thanks for your comment.
johnny
In some languages, the most fundamental list structure is actually a linked list (although in C derivatives, it's usually an array, and then a vector for resizable lists.) If it's not fundamental to the language, then usually you can find a linked list data structure in the standard libraries (i.e. .NET, Java.)
mquander
On what basis do you say Python has linked lists built in? http://stackoverflow.com/questions/280243/python-linked-list and other sources strongly imply otherwise. The "main" list type is based on an array (http://mail.python.org/pipermail/python-list/2007-January/594523.html), like .NET and Java.
Matthew Flaschen
@Matthew: I'm not sure about this point so I corrected my post and deleted all references to Python, to be on the safe side. Thanks.
Roee Adler
A: 

I've been developing line of business applications (.NET) for years and I can only think of one instance where I've used linked list and even then I did not have to create the object.

This has just been my experience.

Cody C
A: 

I would say it depends on the usage, in some cases they are quicker than typical random access containers.

Also I think they are used by some libraries as an underlying collection type, so what may look like a non-linked list might actually be one underneath.

Skurmedel
A: 

In a C/C++ application I developed at my last company we used doubly linked lists all the time. They were essential to what we were doing, which was real-time 3D graphics.

ChrisF
+2  A: 

Yes, there are real world application that use linked list, I sometimes have to maintain a huge application that makes very have use of linked lists.

And yes, linked lists are included in just about any class library from C++/STL to .net.

And I wish it used arrays instead.

In the real world linked lists are SLOW because of things like paging and CPU cache size (linked lists tend to spread you data and that makes it more likely that you will need to access data from different areas of memory and that is much slower on todays computers than using arrays that store all the data in one sequence).

Google "locality of reference" for more info.

Nir
You seem to be saying that traversing a list is less efficient than a vector (using C++ data structures), which is true. However, lists have other properties that make them faster in some circumstances. In general, use vector<>, but remember that other data structures exist for other needs.
David Thornley
This is an over-simplistic picture. Not all data access patterns are equivalent. Linked lists are very useful for some apps, e.g. when large lists need inserts in the beginning or deletes in the middle.
Matthew Flaschen
@Matthew: my answer is make the code work using something more general, change to linked list or such as performance testing suggests.
John Saunders
+2  A: 

Never used hand-made lists except for homeworks at university.

Arnis L.
that was what I thought and is what prompted my question. I enjoy reading it but was thinking...I probably won't use this much. thanks.
johnny
I think it's not bad to know how it works. There isn't useless knowledge after all.
Arnis L.
A: 

Yes all sorts of data-structures are very useful in daily software development. In most languages that I know (C/C++/Python/Objective-C) there are frameworks that implement those data-structures so that you don't have to reinvent the wheel.

And yes, data-structures are not only for academics, they are very useful and you would not be able to write software without them (depends on what you do).

You use data-structures in message queues, data maps, hash tables, keeping data ordered, fast access/removal/insertion and so on depends what needs to be done.

stefanB
+1  A: 

A singly-linked list is the only way to have a memory efficient immutable list which can be composed to "mutate" it. Look at how Erlang does it. It may be slightly slower than an array-backed list but it has very useful properties in multithreaded and purely-functional implementations.

Karl the Pagan
@Karl: the OP asked about "business" programming. I wonder if he would consider your examples to be "business" programming.
John Saunders
A: 

Yes, I do. It all depends on the situation. If I won't be storing a lot of data in them, or if the specific application needs a FIFO structure, I'll use them without a second thought because they are fast to implement.

However, in applications for other developers I know, there are times that a linked list would fit perfectly except that poor locality causes a lot of cache misses.

San Jacinto
A: 

Depending on usage a linked list could be the best option. Deletes from the front of the list are much faster with a linked list than an array list.

In a Java program that I maintain profiling showed that I could increase performance by moving from an ArrayList to a LinkedList for a List that had lots of deletes at the beginning.

James A. N. Stauffer
A: 

I cannot imagine many programs that doesn't deal with lists. The minute you need to deal with more than 1 thing of something, lists in all forms and shapes becomes needed, as you need somewhere to store these things. That list might be a singly/doubly linked list, an array, a set, a hashtable if you need to index your things based on a key, a priority queue if you need to sort it etc.

Typically you'd store these lists in a database system, but somewhere you need to fetch them from the db, store them in your application and manipulate them, even if it's as simple to retrieve a little list of things you populate into a drop-down combobox.

These days, in languages such as C#,Python,Java and many more, you're usually abstracted away from having to implement your own lists. These languages come with a great deal of abstractions of containers you can store stuff in. Either via standard libraries or built into the language.

You're still at an advantage of learning these topics, e.g. if you're working with C# you'd want to know how an ArrayList works, and wheter you'd choose ArrayList or something else depending on your need to add/insert/search/random index such a list.

nos