IRL Example — Data Structures(2)

Photo by Cookie the Pom on Unsplash

In part two of IRL Examples of Data Structures, we’ll be going through Linked Lists.

What is a Linked List?

A Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers.

In the illustration above, each element called a node is pointing to the next node and so on until it reaches the tail node which indicates the end of that list. There is a sequential order, so each node remembers the order that they are in.

In Real Life

Imagine you’re working a part-time job at a major fast food place (Chipotle or Subway) and have to follow a queue or steps to create their meal. A customer comes in and you start taking their order. You first start off at the very start of the counter by asking what they want and what size. Then you go through each section of the items in sequential order, adding toppings and sides based on what the customer wants.

If you were in a customer’s perspective, you would notice that the employee in front of you moves from right to left (or vice versa) until you finalize your order. The last part is when you finally reach the cashier. The cashier will search for your order, give you the total, and then take your payment for the order. And that would be the end of their workflow for you to have a nice meal after!

From the employee's perspective, sometimes a customer would change their mind or even ask to remove an addition to their order or doesn’t want anything from the displayed trays on the next side of the counter.

In Code

A structure of what a Linked List would look like in C# goes like this:

The first class contains the Node head, then the next class will be the Node. The Node class contains data and the node constructor to go create a new node.

A good code snippet from GeeksForGeeks shows how a Linked List is implemented in C#, following the creation of three nodes and some pseudo-code to explain how each line works.

Hope that helps in explaining a simple example of a Linked List in real life and what it would look like when executed in C#!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store