List-o-Palooza (Algorithms)
By: Nolan Kovacik
Tags: Linked List, Dictionary, Lists, Array, Data Structures

List-o-Palooza (Algorithms)

Let’s begin by prefacing that any programming language can use a list. A list is inherently a logical model, and it can be represented by a formidable array of data structures!

Your Options

You have many options to choose from… how do you know which is right for your implementations? *warning: this is not a comprehensive list.

List

Array

Tuple

Hash Table

Dictionary

Set

Linked List

Stack

Queue

Tree

Graph

Let’s refine these ideas into two types of data-structures:

abstract and concrete. This will help understand what each actually is.

A concrete structure is in its most prime logical form. It is a fundamental building block of any programming language. These data-structures explain how operations are implemented, such as adding and removing.

Conversely, an abstract data-structure is built atop concrete ones. This type of data-structure usually is created by adding more rules and conditions to the foundations they are built upon, such as “you can only retrieve the last item”. These data-structures explain what operations are available.

Concrete Data-Structures

A concrete data-structure is the most basic logical form of a list, conceptually.

Array

Hash Table

Linked List

Arrays

An array is a contiguous block of memory cells within a computer. Each cell has a number associated with it. This means we know where every item is — so long as we know the first item’s location.

your memory has all these data points tucked away in these chips. source

Example. Say you have an array of five values. The first value is located at memory cell #10921, and we need to get to the fourth value in the array. We solve this by taking #10921, and adding +3; this results in #10924! Now we know which memory cell to peek into for our data.

Hash Tables

A Hash table is a seemingly random scattering of data across your computer’s memory. Unlike an array, one piece of data will usually not be located near another. How do we find any piece of data, then? Let’s solve this out with a theoretical problem.

Example. Imagine you have five values that you put into a hash table, and we try to revisit the data later. Uh-oh! Is the data lost? I don’t know where I put it! Fret not, friend. Since we know the piece of data we are looking for (“dog”) we can hash our data to find its address.

A hash function takes in any value and spits out an address. My hash function here is simple: it counts the number of characters in a given value. Since “dog” has three characters, it’s in memory location #3! For simplicity sake, we won’t talk about data collisions in this article.

Linked List

Linked Lists are the most interesting concrete data-structure to me. They use pointers! It’s a lot like one of those ridiculous scavenger hunt side quests in video games, which is why they are so much fun.

A linked list can be conceptualized like a sort of chain. source

When a computer needs to add an item to a linked list, it pretty much puts it at any address that it feels like. They aren’t in order like an array, and they aren’t calculated from the value like hash tables.

Example. Wait! How do you find the data! I don’t want to lose it… That’s a good point, friend! Luckily, we have a slip of paper that shows us the address of our first item. It’s at #12982! Let’s go there…Great, we arrived.

Oh, what’s this? This place has two data points. One says “cat”. the other says “#18214”. Huh, I wonder what happens if we visit #18214? Could this be another pointer?

…yep! It is. This place has two slips of data as well. The first is “dog”. The second is “ None ”. Oh, since it doesn’t have another address, I guess this is the last piece of data!

Abstract Data-Structures

List

Dictionary

Set

Stack

Queue

Tree

Graph

Ordered Lists

Depending on what you want to do, many of our concrete data-structures will work great, and almost all will work. Arrays and Linked Lists are obvious choices since they already present themselves in order. Hash Tables can work if you make its ordered number a key.

Already you can tell how abstract data structures can utilize a variety of techniques to get different effects.

A literal dictionary is made the same as the data-structure. source

Dictionaries

A dictionary almost directly maps to a hashtable, although one could implement it as a list of lists as well. In a dictionary, you have an item that directly maps to another value. There are many great real-world examples of this. A word has a definition. A person has a phone number. A recipe has ingredients. The list goes on!

Sets are a mathematical construct, too! source

Sets / Unordered Lists

A set is a list of unique items that have no particular order. You might think that a hash table would work great for this, and you would think right!

Although a set does not have any containing information (other than the ‘key’ itself), this structure will still work great if we just ignore this feature of hash tables altogether!

Stacks & Queues

These data structures are special, since they relate to physics and logic in a weird way. The rules of either are sort of opposites of one another.

A stack of books waiting to be read. source

A stack only allows data to be entered at the top, and removed at the top. If you add three items, you have to remove three items to get the third. This is often made as an analogy to a stack of plates. You can get the plate from the top of the stack only. The ones at the bottom are not accessible. This data structure holds on to data at the bottom for a very long time, while the items at the top are cycled through quickly.

A queue allows data to be entered at one end, and removed at the other. This is like a waiting list or line to an event, restaurant, or roller coaster. If you arrive late, you have to wait in line to get your turn. This data structure ensures each piece of data remains for a pretty consistent amount of time.

A flowing river is a queue, in some sense. The water near the front exits first. source

Trees & Graphs

Trees & Graphs are sort of variations on the Linked List. Imagine a linked list, but with more than one pointer, and more rules to limit itself from exploding. That pretty much sums up both of these concepts.