Common Data Structures and How to Use Them

Vladyslav Nykoliuk
3 min readMar 8, 2021

Data Structures are fundamental to the core of Computer Science and are used in our everyday life more often than we think. When you’re scrolling your photo gallery, you’re using a linked list data structure to filter through your saved images, or when you’re checking your email, updating a YouTube playlist, and visiting your call logs, you’re using a real-life implementation of Queue to organize these lists. It is no wonder we use data structures all the time as they provide a dependable and secure method of utilizing data. Let’s dive into some common data structures and how to actually use them.

Linked Lists

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations, and instead, continue the sequence to the next node.

Linked List Example

The elements in the list are known as nodes. The head defines the first element of the array whereas the tail defines the last.

There are three types of linked lists:

Singly Linked List — Transversal can only go in one direction, from head to tail.

Doubly Linked List — Transversal can be done in both directions.

Circular Linked List — Lists where the previous head directs to the tail or vise versa.

Transversing a linked list: (Pseudocode)

# Given an array and its head
# sort through each node of the array following the head node
# set data you want to find and use in a while loop
# when the data is found the node index will be returned

Code: (Python)

def search(self, data):     
current = self.head
found = False
while current and found is False:
if current.get_data() == data:
found = True
else:
current = current.get_next()
if current is None:
raise ValueError("Data not in list")
return current

Arrays

Arrays are a collection of data with a fixed size depending on the size of the elements. Any integers, floats, and strings can be stored in arrays.

Array Example

Array nodes are referenced through their index and all possible array operations are get, insert, search, update and delete.

Array: (Pseudocode)

# Get array
# run .index() on element you want index of
# index of node is returned

Code: (Python)

list = ["One", "Two", "Three"]
print(list.index("Two"))
nums = [4, 3, 5, 9, 0, 2]
print(nums.index(3))

Stacks

Stacks are structures that store elements based on when they are inserted. The main thing to remember is LIFO- Last In First Out which means the most recently inserted element will be accessed first. Think of it as you typing out a draft. Every time you make a change, it gets added to the stack, with the most recent change being the furthest at the top and your start will be at the bottom.

Stack Example

Stack Implementation: (Pseudocode)

# Initiate Stack
# append several elements
# get index of a specific element
# remove a certain element

Stack Implementation: (Python)

stack = []
stack.append('a')stack.append('b')stack.append('c')print(stack)
print(stack.get('a'))
stack.pop('b')stack.pop('c')

Resources:

https://www.freecodecamp.org/news/the-top-data-structures-you-should-know-for-your-next-coding-interview-36af0831f5e3/

--

--