Introduction to data structure

Photo by Kaleidico on Unsplash

Introduction to data structure

Stephen Ilori, a software engineer from Lagos, Nigeria talks about the What, and why behind a lot of software developers nightmares(data structures!).

ยท

5 min read

We've all been in one interview or another where they asked us to "find x" and somehow, that didn't work out so well, either because you weren't expecting it or you don't even understand where to start. The good news is, we've all been there (I know I have).

So, in this lesson, we'd be talking about different data structures as a series so you could kick ass in your next technical interview. My name is Ilori Stephen, and I'm your street programmer from Lagos, Nigeria(Still waiting for the beat to drop ๐Ÿ˜).

The What?

It's okay to ask "What is a data structure?", and "why is it important?". A data structure can be defined as a specialized format for organizing, processing, retrieving, and storing data. Data structures are incredibly important because our regular data types "string, int, array, etc" can be very insufficient depending on the problem type. Data structures help us convert these data types into a logical unit that makes them relevant to the problem we are trying to solve.

How Is It Used?

Good question! If you've ever used the following data types "array, objects, maps, etc", then it's safe to say that you've used a data structure. They come into play in so many ways! For example, file systems, and music players amongst many others are implementations of data structures(brilliant, isn't it? ๐Ÿ˜Š).

As you can tell, data structures are a very important part of software engineering. They also play a key role in helping us write efficient algorithms which are used by tons of applications out there!

How Many Types Are There?

Several types of data structures are often decided by your problem statement or use case. If you were tasked with the responsibility of building a Job Queue, you might want to insert, remove, and update elements in a certain order. You could easily design this with an array, but using a variety has its trade-offs. For a task like this, you might want to employ the use of a Queue data structure.

Data structures can also be grouped as linear and non-linear. You guessed it! A linear data structure is a type of structure that has all of its elements in a linear order while a non-linear data structure is a direct opposite of what we just defined. The different types of data structures are;

  1. Array: An array is a linear data structure that consists of different elements, each identified by an index. An array can hold a collection of integers, strings, or even other arrays.

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ['Maria', 'John', 'Sam', 'Stephen', 'Rayo'] [1, 'Stephen', []] // These are all valid arrays and each of the elements in the array are associated with an index starting from 0.

  2. Stack: A stack can be defined as a linear data structure that follows the LIFO(Last In First Out) order with two main operations: Push, and Pop. This is a very useful data structure as a lot of solutions are built around them such as; the Javascript call stack, Backtracking, and Memory management.

  3. Queue: Similar to a stack, a queue is a linear data structure that follows the FIFO(First In First Out) order with two primary operations: Push, and Shift which is used for inserting, and removing elements from the last, and first positions. Queues are particularly useful for the following operations; Task Scheduling, Traffic Management, and Batch processing.

  4. Linked List: A linked list is a linear data structure that consists of nodes with each node pointing to the next or in some cases a previous node in the list. Unlike the Stack and Queue data structures, a linked list has several methods that make it possible for us to manipulate its data. There are different examples of a linked list with the most popular ones being the singly-linked list, and the doubly-linked list(more on this later). Linked lists are used for operations such as; Browser navigation, Music players, and Image viewers amongst a few. Attached below is an example of a singly-linked list.

  5. Tree: I know what you're thinking but I most definitely don't mean that tree ๐Ÿ˜‘. A tree is a non-linear data structure that consists of nodes where each node can be connected to one or more nodes but must be connected to exactly one parent. There are different types of trees, the most popular ones being the Binary and the Binary search trees. Trees are used in implementations such as the File system, Displaying/Formatting hierarchical data. Below is an example of a tree data structure.

  6. Graph: A graph is a non-linear data structure that is made up of nodes or vertices connected by edges or lines. Just like other data structures, there are different types of graphs such as; weighted graphs, unweighted graphs, distributed graphs, and undistributed graphs. Graphs are used in building digital maps, computer networks, and designing social network applications. Attached is an image of a graph.

  7. Hash Table: Also known as hash map, is a type of data structure that uses a hash function to generate hashes mapped to a value in an array-like storage format. Hash tables are used in database indexing, caches, sets, and associative arrays. Attached below is an image of a hash table.

  8. Heap: A heap is a tree-like data structure where we have a parent and children based on different rules such as the parent value being greater than the children or the parent value being less than the children. We have two types of heaps and they're max-binary heap and min-binary heap.

Choosing A Data Structure

Choosing a data structure really depends on what you are trying to achieve or solve. For instance, a linked list is really good/efficient for fast inserts and removals, Stacks are pretty useful if you're supporting a LIFO order, and Trees(again, not the other kind) are pretty good if you're working with a parent-child relationship.

Conclusion

I really hope you enjoyed reading this article as much as I enjoyed making it. Think of this article as a theoretical introduction to data structures. In my next articles, we'd talk about implementing each of the data structures listed above so you can have a more solid understanding of how they work(I guess this is where I say, don't forget to subscribe ๐Ÿ˜).

ย