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:30

Giving travel directions: With each step in the directions being a node, and the travel instruction between each node as your link.

Example:

Node 1: Start at Home

Link: Walk 3 blocks South to Bob's House

Node 2: Bob's house

Link: Walk 2 blocks North to Alice's House.

Node 3: Alice's house

If you want to get one place to another you have to follow the links(instructions) from each intermediate place(node), you can't just skip from home to Alice's.

查看更多
Bombasti
3楼-- · 2019-01-30 11:31

Look at Linked List as a data structure. It's mechanism to represent self-aggregation in OOD. And you may think of it as real world object (for some people it is reality)

查看更多
Anthone
4楼-- · 2019-01-30 11:32

Waiting line at a teller/cashier, etc...

A series of orders which must be executed in order.

Any FIFO structure can be implemented as a linked list.

查看更多
不美不萌又怎样
5楼-- · 2019-01-30 11:32

Inside the make program, you will often find that the dependency lists for a particular file that needs to be built are defined as linked lists of pointers to other files which also need to be built and in turn have dependencies in linked lists.

查看更多
时光不老,我们不散
6楼-- · 2019-01-30 11:35

In operating systems ... one may use a linked list to keep track of what processes are running and what processes are sleeping ... a process that is running and wants to sleep ... gets removed from the LinkedList that keeps track of running processes and once the sleep time is over adds it back to the active process LinkedList

Maybe newer OS are using some funky data structures ... linked lists can be used there

查看更多
够拽才男人
7楼-- · 2019-01-30 11:36

He did ask for a practical example; so I'll give it a shot:

Lets say you are writing a firewall; in this firewall you have an IP whitelist and an IP blacklist.

You know that your IP, your jobs IP, and some testing IP's need to be whitelisted. So, you add all of the IP's to the whitelist.

Now, you also have a list of known IP's that should be blocked. So, you add those IPs to the blacklist.

Why might use LinkedList for this?

  1. The operation is fast for adding/removing an item from the list.
  2. You do not know how many IP's are going to blocked/whitelisted. Thus, revealing one of the main advantages to a LinkedList (it's resizable).
查看更多
登录 后发表回答