Data Structures

Yahjaira Vasquez
4 min readSep 1, 2020

--

Let us learn about data structures and the best use cases. First off, what is a data structure? Data structures are how we organize, store, and retrieve data. Knowing when and how to appropriately use each data structure is important for ensuring your code efficiently uses time and space. Other things to consider: some data structures may run quicker, but take up more space, while others may take up less space, but run a little slower. You must always consider the trade offs of each use case and decide which is more beneficial for the company/code base.

That being said, let’s jump into the different kind of data structures and what they are most commonly used for.

Linear Data Structures

Linear data structures are single leveled data structures, for which each element is connected to the previous and next element. Linear data structures include: arrays, stacks, queues, and linked lists.

Array

Arrays have a grid like structure. The elements of an array are stored next to each other in memory. When using an array, all of the elements must be of the same data type (i.e. integers, strings, or booleans etc.). When creating an array, you want to define the size of the array, so that this space in memory may be reserved. This can be a negative aspect of arrays because, you can be left reserving unused space in memory, also, if you want to add to the size of the array it is inefficient because then a new space in memory must be found to fit the size of your array because elements of an array are store contiguously.

Arrays are best for basic spread sheets or for creating linear data structures that you may know the exact size of. Arrays are also useful for instances where you will need access to elements and you know the index of the element. Arrays are also faster to iterate through, than say, linked lists. Arrays are also great for combining within other data structures.

Linked List

Link lists are similar to arrays, but the elements of the list are not stored contiguously. The elements, referred to as nodes, are spread out, with each individual node pointing to the next node in the list. Because each node points to the next node in the list, the nodes may be spread throughout memory. A negative to linked lists can be that it takes up a bit more space than arrays because the node must document the current element, referred to as data, as well as the next element in the list, referred to as the pointer.

Linked lists are especially useful for cases where you may not have an exact size for your list, therefore the expansion of the list is anticipated. Another great use case for linked lists would be when you need to add nodes to the beginning or middle of your list.

One thing to note with linked lists, this data structure is not necessarily included with all coding languages. For instance, in Ruby, there are no linked lists, arrays act as linked lists. There are also ways to create linked list like structures in languages such as Ruby and Javascript, but I will leave that to you to further research.

Queue

Queues can be thought of like a line at the grocery store. They have a FIFO method. This means, that items first placed in the list will be the first to be removed from the list. Back to the line at the grocery store, there are five people in the line. The first person who entered the line will be the first person attended to, as well as the first person to exit the line and will continue in this sequential order.

Queues are great for any algorithms that require FIFO methodology.

Stack

Stack is comparable to a queue, but it follows a LIFO method, meaning last item in is the first to be removed from the list. You can think of a stack like a can of Pringles. The last chip inserted to the stack is the first chip you get when you open the can. You can’t remove from the middle of the stack, unless you remove all the chips that were placed on top/after of the middle chip.

That being said, stacks are great for any algorithms that require LIFO methodology. Stack is also great for undoing, as well as reversing order.

Non-Linear Data Structures

Data structures that are not organized in a single level or linearly, are known as non-linear data structures. Non linear data structures are not the easiest to traverse, as they cannot be traversed with one run, making them not the best data structures to use when time complexity is the focus. Non-linear data structures are best used when space complexity is your focus. They tend to better utilize space in memory, making them great for AI and Machine Learning. The two non-linear data structures we will be discussing are graphs and trees.

Graph

Graphs are data structures that consist of edges and vertices. Vertices can be describes as specific points on the graph, where edges are the paths to connect two points. Graphs are commonly used for networks, such as social media friends lists, apps providing directions to and from locations, internet networks, etc. While graphs are great for maintaining connections, most graphs lack in scalability.

Tree

Trees are also used for representing relationships. They are mostly used for representing hierarchical relationships. They share similarity to linked lists, in that each node points to its child nodes, but each child node does not contain any information regarding its parent node. Trees are great for inserting and deleting nodes, but can be more challenging when trying to rearrange nodes. As stated before, best use case for trees would e for data that needs to be stored hierarchically.

--

--

Yahjaira Vasquez

Seeking and spreading knowledge within the world of tech!