Introduction to Data Structures (Part 1)

Introduction to Data Structures (Part 1)

Did you ever get the chance to look at your class's attendance register in school? It contained all the details about each student, systematically documented and neatly organized. Remember how easy it was for your teacher to find out your dad's phone number if you were caught misbehaving😉 Now, consider a situation where the data in the attendance register was not organized - it would have made it difficult for your teacher to retrieve any relevant information about the class. This is just one example of how organizing data can make our lives simpler. There are countless other instances where proper data organization can help us easily access and retrieve important information

A data structure, as the name suggests is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently. Data structures are used extensively in industry, one such example being social networks like Instagram, Facebook, etc. They may use a graph data structure to represent relationships between users, with each user represented as a node and their connections represented as edges. Another example is your phone’s contact list which may be implemented as an array, a linked list, or a hash table, depending on the specific implementation. Each contact is stored as an object that contains information such as the person's name, phone number, email address, and other relevant details.

Don’t worry about the technical terms, all these examples are given so that you understand that organizing data is important and we’ve been doing it in our day-to-day tasks. To do the same for our computer’s data, we use data structures.

Classification of data structures:

Linear data structure -> Here data elements are stored sequentially, one after the other. They allow data elements to be added or removed in a specific order.

Non-Linear data structures -> Here data elements are not stored in a sequence but in a hierarchical manner where one element is connected to one or more data elements.

In this blog, we'll learn about 3 of my favourites-Array, Linked list and Stack and in Part-2 of this blog, we'll cover the other 3.


Array

It is a type of linear DS, elements of the same data type are stored in continuous memory locations. Each array element is identified by a unique index, which usually starts from 0. The size of the array is defined while declaring it, so we should know how much data is to be stored beforehand.

It’s like keeping a bunch of boxes in a row, where you can put different things in each box. And the boxes are numbered, so if you want to access a particular box (say number 5), you can directly go to the box marked 5.

To learn more about arrays, you can watch this lecture


Linked Lists

Like in our previous example, if we want to add or remove a new box that is in between other boxes, then we will have to move all other boxes in the array. That is when linked lists kick in. In this system, each box has a tag attached to it, which tells us the location of the next box. So, if we add another box, all we have to do is to change the tag to a new location and voila, a new box is created. Similarly, we can also remove boxes. Hence, memory allocation is not contiguous but dynamic.

In technical terms, each box is called a node and the tag is called a pointer, which points to the location of the next node. And sequences of both are called linked lists.

There are three types of linked lists:

Singly Linked List –> In this, each node of the linked list points only to its next node.

Doubly Linked List –> In this case, each node of the linked list holds two pointers, one points to the next node and the other points to the previous node

Circular Linked List –> Here the last node points back to the head of the linked list.

To learn more about linked lists, you can watch this lecture


Stack

Stack is a linear data structure where data is stored by stacking it on top of each other. Stack follows the LIFO policy. Last in first out. What is it? It essentially means that data that is inserted last in the stack will be the first one to be popped out.

For example, here we are stacking the boxes on top of each other rather than arranging them side by side like in arrays or linked lists. Logically, to obtain the nth box from the top, we will have to remove the n-1 boxes that are stacked on it. This is what LIFO means.

There are other operations as well that we can perform on stacks:

  • Push(): inserts data in stack

  • Pop(): removes the topmost data element in the stack

  • isEmpty(): returns true if the stack is empty

  • size(): returns the size of the stack.

  • top(): returns the topmost data element from the stack

To learn more about Stack, you can watch this lecture


Thats it for this blog guys, make sure to practice questions on Leetcode after learning the theory. Stay tuned for part 2 and please leave feedback for this blog as well !!!