Introduction to Data Structures
Published: 2021-12-07 00:49:56

---
title: Introduction to Data Structures
published: true
date: 2021-11-22 12:23:01 UTC
tags: 
canonical_url: https://www.kaumnen.com/p/introduction-to-data-structures
cover_image: https://www.niit.com/india/sites/default/files/2020-01/thumbnail%20image-06.jpg
---

### **Data Structures**

You will hear about them no matter in what stage of SE career you are at. Understanding them, how they work, is essential if you want to become a good programmer.

When you get a good grasp of them, it will help you with either your career or coding interviews. If you want to become well-versed, successful, you need to understand **data structures.**

> [Bad programmers worry about the code. Good programmers worry about data structures and their relationships. — Linus Torvalds](https://www.azquotes.com/quotes/topics/data-structures.html#:~:text=Data%20Structures%20Quotes-,Bad%20programmers%20worry%20about%20the%20code.%20Good%20programmers%20worry%20about%20data%20structures%20and%20their%20relationships.,-Linus%20Torvalds)

**What are they? Which ones exist? Pros and cons of each one?**  
If this is looking **_hmm_** to you, well all of us were in that phase. But don’t worry! I will try to explain all of those questions in this introductory blog post. You got it!

### **What are Data Structures?**

> [_In computer science, a_ **_data structure_** _is a data organization, management, and storage format that enables efficient access and modification._](https://en.wikipedia.org/wiki/Data_structure)

So essentially, the data structure is a way to **efficiently manipulate and organize data.**

We are using data structures every day! For example:

- In Microsoft Word, undo and redo operations? —  **Stacks!**
- Social networks feeds? — **Linked list or Hash table!**
- Storing an image? —  **Arrays!**
- Go back functionality in browsers? —  **Stacks!**
- Friend list, connections, who is a friend with who? —  **Graph!**
- etc.

**Data structures are everywhere!**

They are the backbone of every good and quality software and software engineer!

### Which Data Structures exist?

I will cover the following Data Structures.

#### Array

> [_In computer science, an_ **_array data structure\*_** , or simply an _\*\*_array_\*\*_, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.\*](https://en.wikipedia.org/wiki/Array_data_structure)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1637584937151/VDKF0YX1lh.png)

Linear array or one-dimensional array is **the simplest** type of data structure. **Arrays** are present everywhere! Basically, it is just a continuous stream of cells.

**Array** pros:

- They are quick! If you know the exact number, that data is retrieved almost instantly.

**Array** cons:

- Arrays have to be **continuous**. Before, it was quite common to run into memory problems as they grow in size. **Memory management** is nowadays a lot more automated/secure (Python, JavaScript), but you still have to keep in mind this, in order to either **write or troubleshoot code** well (C, C++).

We can find the **Array** data structure here:

- **Sorted** leaderboards
- 2D arrays, **matrices** , are used in **image** processing and manipulation

#### Linked List

> _A_ **_linked list_** _is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next._

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1637584938876/ZjvH_YdFFO.jpeg)

A linked list is **not continuous,** or at least it **doesn't have** to be.

It consists of a **collection of nodes.** Basically, each node contains **data** and a **pointer** to the **next node** in the sequence. The **last node** contains a pointer to _nothing_ (it has a value of **Null/None** ).

The **first and last nodes** are known as **HEAD** and **TAIL** ones.

**Linked List** pros:

- You can easily **add or delete** a node. All you need is to **change the pointer** , as each node is aware only of the node next to it (or node before it, if it’s **double-linked** )

**Linked List** cons:

- They can be slow because, in order **to find** a node, you have to (in the **worst** case, **TAIL** has the **data** you are looking for) traverse the whole **Linked List**.

We can find the **Linked List** data structure here:

- **Previous** web pages in browsers
- **Switching** music in music players
- **Turns** in mobile games

#### Hash Table

> [**_Hash Table_** _is a data structure which stores data in an_ **_associative_** _manner. In a_ **_hash table\*_** , data is stored in an _\*\*_array **format, where each data value has its own** unique index **value.** Access **of data becomes very fast if we know the** index_\*\*_ of the desired data.\*](https://www.tutorialspoint.com/data_structures_algorithms/hash_data_structure.htm)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1637584940611/dCmwfHuNE.jpeg)

So essentially, it uses **arrays** as an **underlying** data structure. **Hash Table** functions in a **key-value** manner. It has **a function** , which computes an **index** (hash node). That **index** is used for **data** (value) to be stored.

**Hash Table** pros:

- As per the nature of Hash Table functioning, they are **fast** when it comes to the **addition** or **retrieval** of data

**Hash Table** cons:

- The hash function can cause a collision! There are workarounds, but you need to keep in mind that two different inputs can cause the same output, which could make a problem in memory and potential data loss.

_off-topic: Recently, there was_ **_a collision_** _problem in_ **_Apple’s_** _security system. Read more about it_ [_here_](https://www.theverge.com/2021/8/18/22630439/apple-csam-neuralhash-collision-vulnerability-flaw-cryptography)_._

We can find the **Hash Table** data structure here:

- Databases, passwords
- **Searching** in web-browsers
- **Files** on devices, filename, and file path

#### Stack and Queue

> _Stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle._

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1637584942404/FT-SNEPm5.jpeg)

You can think of a stack as a **pile of plates**. You clean them, and the **last one** you place on top, you will use it **first**. So, **last-in** -> **first-out**.

> _Queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle._

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1637584943820/i-v4QfEKL.jpeg)

You can abstract queue as people waiting to buy something in store. The first one who came will be the first one who will get to buy and go. So, **first-in** -> **first-out**.

**Stack and Queue** pros:

- They are **efficient** in **addition** or **removal** of data

**Stack and Queue** cons:

- They don't have **many** use cases. That doesn't mean you shouldn't learn them!

We can find the **Stack** data structure here:

- **Undo** operation
- **Syntaxes** in programming languages
- **Virtual** Machines
- **Browser** history
- Call logs, **e-mails** , gallery, **notifications**

We can find the **Queue** data structure here:

- **Operating** system, job **scheduling**
- **Sending** e-mail, it’s **queued** and **sent** in blocks
- Server **responding** to request
- **Uploading** and **downloading** files

#### Graphs and Trees

> [**_Tree_** _and_ **_graph_** _come under the category of_ **_non-linear_** _data structures where tree offers a very useful way of representing a relationship between the nodes in a hierarchical structure and graph follows a network model._](https://techdifferences.com/difference-between-tree-and-graph.html#:~:text=Tree%20and%20graph%20come%20under%20the%20category%20of%20non%2Dlinear%20data%20structure%20where%20tree%20offers%20a%20very%20useful%20way%20of%20representing%20a%20relationship%20between%20the%20nodes%20in%20a%20hierarchical%20structure%20and%20graph%20follows%20a%20network%20model.)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1637584945250/i1ZjtHaKmB.jpeg)

**How to differentiate them?**

**Tree** data structure **has to** be connected, and **can not** have loops.

**Graphs** data structures **don't have** such restrictions.

They are both **hierarchical** data structures. You can abstract that as **a business** , with **CEO** being on **top** with his associates beneath, then the people below him, who have their own associates, and so on.

Each node is **aware** of the **next node** (kind of like **a linked list** ). It contains **data** and the **pointer** to the next one.

**Graphs and Trees** pros:

- They provide a quick **addition** of data, as well as **searching** e.g. binary search.

**Graphs and Trees** cons:

- **Modifying** data can be tricky **sometimes** , as you don't have all node indexes at once and you maybe have to traverse them.

We can find the **Graph** data structure here:

- Facebook **Graph** API
- **Dijkstra** algorithm which finds **the smallest** path between two nodes, used in **maps** for example
- Facebook, Instagram, social media **networking**

We can find the **Tree** data structure here:

- XML **parser**
- Domain Name Server —  **DNS**
- File **explorer** of any device
- **Posting** questions on Quora, Reddit, etc.

### Conclusion

These **data structures** will give you a **good foundation** for algorithms.

But don’t forget: You will become **an expert** at them **only** if you start **applying** them!

It can be frustrating from time to time, but solving those problems will give you **an experience** , something you **can not** learn.

**I wish you the best of luck, you got it!**

**_\_ktb_**