views:

631

answers:

7

I've been coding for quite sometime now. And my work pertains to solving real-world business scenarios. However, I have not really come across any practical usage of some of the data structures like the Linked List, Queues and Stacks etc.

Not even at the business framework level. Of course, there is the ubiquitous HashTable, ArrayList and of late the List...but is there any practical usage of some of the other basic data structures?

It would be great if someone gave a real-world solution where a Doubly Linked List "performs" better than the obvious easily usable counterpart.

+2  A: 

Of course it’s possible to get by with only a Map (aka HashTable) and a List. A Queue is only a glorified List but if you use a Queue everywhere you really need a queue then your code gets a lot more readable because nobody has to guess what you are using that List for.

And then there are algorithms that work a lot better when the underlying data structure is not a plain List but a DoublyLinkedList due to the way they have to navigate the list. The same is valid for all other data structures: there’s always a use for them. :)

Bombe
By "work a lot better"...you mean more efficiently than the corresponding List counterpart? Please elucidate
Chouette
Imaging the (theoretically infinite) tape of a turing machine. You might implement it as a `List`; that would enable you to go right without any problems but going left would require you to start at the beginning and iterate through the slots until you’re at the one to the left of the one you started with. When using a `DoublyLinkedList` moving left is just as simple as moving right.
Bombe
(And I had to look up “elucidate”—I guessed it right, though. :)
Bombe
+1  A: 

Stacks can be used for pairing (parseing) such as matching open brackets to closing brackets.

Queues can be used for messaging, or activity processing.

Linked list, or double linked lists can be used for circular navigation.

astander
A: 

Most of these algorithms are usually at a lower level than your usual "business" application. For example indices on the database is a variation of a multiply linked list. Implementation of function calling mechanism(or a parse tree) is a stack. Queues and FIFOs are used for servicing network request etc.

These are just examples of collection structures that are optimized for speed in various scenarios.

Igor Zevaka
+1  A: 

LIFO-Stack and FIFO-Queue are reasonably abstract (behavioral spec-level) data structures, so of course there are plenty of practical uses for them. For example, LIFO-Stack is a great way to help remove recursion (stack up the current state and loop, instead of making a recursive call); FIFO-Queue helps "buffer up" and "peel away" work nuggets in a coroutine arrangement; etc, etc.

Doubly-linked-List is more of an implementation issue than a behavioral spec-level one, mostly... can be a good way to implement a FIFO-Queue, for example. If you need a sequence with fast splicing and removal give a pointer to one sequence iten, you'll find plenty of other real-world uses, too.

Alex Martelli
A: 

I use queues, linked lists etc. in business solutions all the time.

Except they are implemented by Oracle, IBM, JMS etc.

These constructs are generally at a much lower level of abstaction than you would want while implementing a business solution. Where a business problem would benifit from such low level constructs (e.g. delivery route planning, production line scheduling etc.) there is usually a package available to do it or you.

James Anderson
A: 

I don't use them very often, but they do come up. For example, I'm using a queue in a current project to process asynchronous character equipment changes that must happen in the order the user makes them.

Justin Johnson
A: 

A linked list is useful if you have a subset of "selected" items out of a larger set of items, where you must perform one type of operation on a "selected" item and a default operation or no operation at all on a normal item and the set of "selected" items can change at will (possibly due to user input). Because linked list removal can be done nearly instantaneously (vs. the traversal time it would take for an array search), if the subsets are large enough then it's faster to maintain a linked list than to either maintain an array or regenerate the whole subset by scanning through the whole larger set every time you need the subset.

With a hash table or binary tree, you could search for a single "selected" item, but you couldn't search for all "selected" items without checking every item (or having a separate dictionary for every permutation of selected items, which is obviously impractical).

A queue can be useful if you are in a scenario where you have a lot of requests coming in and you want to make sure to handle them fairly, in order.

I use stacks whenever I have a recursive algorithm, which usually means it's operating on some hierarchical data structure, and I want to print an error message if I run out of memory instead of simply letting the software crash if the program stack runs out of space. Instead of calling the function recursively, I store its local variables in an object, run a loop, and maintain a stack of those objects.

Matthew