A linked list can be used when you want cheap insertion and deletion of elements and when it doesn't matter that the elements aren't next to each other in memory.

This is very abstract and I would like to see some concrete examples of when a linked list is used rather than an array. I'm not very experienced with programming, so I haven't got much (if any) real-world experience.

Honestly, I don't think examples will mean that much to you until you have some experience of writing something, and then seeing the effect that changing data structures has on your code. Either try writing something you're interested in, and figure out what data structures and containers are the best fit, or look at some existing code and think about why each container was selected, and what effect changing it would have.
–
UselessJan 5 '12 at 12:39

I just don't think it's possible usefully to communicate the trade-offs and side-effects of data structure selection to the (inexperienced) OP without a much longer-form worked example than is reasonable here. The examples given as answers are fine, and do answer the question as asked, but not (what I read as) an implied request for actual understanding.
–
UselessJan 5 '12 at 14:43

6 Answers
6

Here is something part way between an example and an analogy. You have some errands to do, so you grab a piece of paper and write:

bank

groceries

drop off drycleaning

Then you remember that you also need to buy stamps. Because of the geography of your town, you need to do that after the bank. You could copy your whole list onto a new piece of paper:

bank

stamps

groceries

drop off drycleaning

or you could scribble on the one you had:

bank ....... STAMPS

groceries

drop off drycleaning

As you thought of other errands, you might write them at the bottom of the list, but with arrows reminding yourself what order to do them in. This is a linked list. It's quicker and easier than copying the whole list around every time you add something.

Then your cell phone rings while you're at the bank "hey, I got the stamps, don't pick up any more". You just cross STAMPS off the list, you don't rewrite a whole new one without STAMPS in it.

Now you could actually implement an errands list in code (maybe an app that puts your errands in order based on your geography) and there's a reasonable chance you would actually use a linked list for that in code. You want to add and remove lots of items, order matters, but you don't want to recopy the whole list after each insertion or deletion.

@Dennis yes, I know, thanks to move semantics. However this is not true for all languages so the example stands.
–
Kate GregoryMay 22 '14 at 1:38

1

@KateGregory fair enough, but it's still good to point out that "basic" data structures are often better than the cool data structures we learn about in school (or elsewhere). I don't want new grads to use LLs everywhere because it's theoretically appropriate while possibly being a poor choice realistically. Using the right data structure is important, and sometimes people overcomplicate it. Slightly related is this talk by Jonathan Blow (creator of "Braid") on data structures.
–
DennisMay 22 '14 at 14:23

@Dennis: I gave a presentation at C++Now 2014 that looked into this topic in more detail. youtube.com/watch?v=mMpBw7S2cqw Starting at 11:55 and going for a while, I look at whether this advantage of linked lists really does exist on modern hardware, comparing vector (dynamic array), a linked list (double-linked list), deque (a bit more complex than fits in this comment), and a vector of pointers to the data.
–
David StoneJan 10 at 5:55

That is, C-language procedure calling follows a first-in, last-out discipline. The result of executing (possibly recursive) function calls gets referred to as a "call stack", or sometimes even just "the stack".

The CALL x86 instruction ends up implmenting a linked list using the "call stack". A CALL instruction pushes the contents of %EIP register, the address of the instruction after the CALL onto stack memory. The called-fuction prolog pushes the contents of the %EBP register, the lowest address of local variables in the calling functio, onto stack memory. Then the called function prolog sets %EBP to the current function's stack base.

That means that %EBP is a pointer to a memory location that holds the address of the calling function's %EBP value. That's nothing more than a linked list, implemented partially in hardware via CALL.

As far as what this is good for, it's how x86 CPUs implement function calls, particularly function calls where the function has it's own copy of arguments, and variables local to the function. Every function call pushes some information on the "call stack" that allows the CPU to pick up where it left off in the calling function, without interference from the called function or the calling function.

A message queue is a structure in which we store information about events for later processing. For example, when the user presses a key or moves the mouse, this is an event. An application might be busy at the moment when the event occurs, so it cannot be expected to process the event at the precise moment when it occurs. So, the event is placed in a message queue, (information about which key was pressed, or where the mouse has moved,) and when the application has some time to spare, it checks its message queue, fetches events from it, and process them. (This happens within a time frame of milliseconds, so it is not noticeable.)

From the usage scenario that I just described, it should be obvious that we never care to have random access to events stored in the message queue; we only care to be able to store messages in it, and retrieve them. So, it makes sense to use a linked list, which provides optimal insertion/removal time.

(Please do not butt in to point out that a message queue is likely, or more likely, or almost as likely, to be implemented using a circular array-list; that's a technical detail, and it has a limitation: you can only store a limited number of messages in it.)

@Michael: A double-ended queue. It is a fairly complex structure but offers very good complexity on many operations and also very good memory performance in terms of contiguity.
–
DeadMGJan 5 '12 at 12:56

Linked lists are useful for many things. The simplest case would be as a more powerful array. You can grow and shrink dynamically(super important), you don't need to allocate a contiguous block of memory(which you might not have), and you can store arbitrary items(instead of all the same types).

Everywhere that you could use an array, a linked list gives you more power. It just has more overhead, which is rarely an issue these days.