What is a practical, real world example of the Lin

2019-01-30 11:04发布

I understand the definition of a Linked List, but how can it be represented and related to a common concept or item?

For example, composition (EDIT: originally said 'inheritance') in OOP can be related to automobiles. All (most) automobiles in real life are the essentially same thing; an automobile has an Engine, you can start() it, you can make the car go(), stop() and so on. An automobile would typically have a maximum passenger capacity but it would differ between a Bus and a SportsCar, which are both automobiles.

Is there some real life, intuitive example of the plain ole' singly Linked List like we have with inheritance? The typical textbook Linked List example shows a node with an integer and a pointer to the next, and it just doesn't seem very useful.

Your input is appreciated.

30条回答
爷、活的狠高调
2楼-- · 2019-01-30 11:15

My first reaction to this question was "Look around! This stuff is everywhere!" But after thinking about it for a bit, I couldn't think of any example that isn't contrived.

The concept of a linked list is a compound concept, a two-fer. You have the notion of a list, which is no problem. A grocery list, for example. Then you get to the link part. One grocery item doesn't know about the next grocery item, so the model breaks down.

I think that the reason you are having trouble with finding a real world eample is that the link part is a programming artifact, an implementation detail. There are many ways to implement lists programatically and one good way is to have each list item know about its neighbors. Another way is to have a List object that keeps track of the items and their order. This is how most lists work in real life. In the example above, the List object for the grocery list would be the paper (or whatever) it's written on.

Maybe it's more useful to think about lists in general and view linked lists as just a specific implementation of a list.

查看更多
beautiful°
3楼-- · 2019-01-30 11:16

A good example of a linked list is your text message, wherein a certain packet a message may be divided into several packets. Each packet holds a key which connects to the next key and to the n-th key to make the whole text message wherein it contains the key and the data.

查看更多
何必那么认真
4楼-- · 2019-01-30 11:18

Some example of single linked list.

  1. Undo button of any application like Microsoft Word, Paint, etc: A linked list of states.
  2. GPS Navigation: A linked list of map data. Travelling from origin to destination is example of traversing through all nodes. Rerouting by a GPS is an example of Add and Remove operations of map data.

Some example of double linked list.

  1. Browser's Next and Previous Button: a linked list of URLs
  2. Microsoft Image Viewer's Next and Previous Button: a linked list of images
  3. Undo and Redo button of Photoshop, a linked list of states.
查看更多
Bombasti
5楼-- · 2019-01-30 11:19

The way Blame moves around a bunch of software engineers working on different modules in a project.

First, the GUI guy gets blamed for the product not working. He checks his code and sees it's not his fault: the API is screwing up. The API guy checks his code: not his fault, it's a problem with the logger module. Logger module guy now blames database guy, who blames installer guy, who blames...

查看更多
淡お忘
6楼-- · 2019-01-30 11:19

Your DNA molecules are double-linked lists.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-30 11:19

well if a teacher took his students to a cartoon movie but she couldn't get the seats together, she'll ask students to remember the address(seat number) of next student and so on... so that she wouldn't have to face the trouble while going back!!!

查看更多
登录 后发表回答