DEV Community

Cover image for Data Structures 101: Introduction to Data Structures and Algorithms.
Brayan Kai
Brayan Kai

Posted on • Edited on

Data Structures 101: Introduction to Data Structures and Algorithms.

Introduction

Data is something that everyone is familiar with, but knowing how to organize, alter, manage, and retrieve data is even more crucial. Being a competent programmer isn't only about whether or not your code works; it's also about readability and scalability, which refers to how fast and memory-efficient your code is.

A thorough understanding of Data Structures and Algorithms will help you become a better programmer since you will be more concerned with developing good code that is legible by all and highly optimized in terms of time and space (memory).
This article, I believe, will teach you a lot.🎉

Prerequisite

To get the most out of this series, you should be familiar with the fundamentals of at least one programming language, preferably Python, because the course will focus on Python Data Structures and Algorithms.

Another requirement for learning and exploring Data Structures and Algorithms is an open heart.

Being willing to learn and believing in your ability to succeed. You don't have to put any pressure on yourself, but I believe that if you put forth consistent effort day after day, you will gain the most out of this article.

The goal of this article🎉

After reading this article to the end, every reader should be able to :

  • Understand what Data Structures is,

  • The different types of data structures, and

  • The reasons for studying Data Structures.

What are Data Structures🤔

In Simpler Terms

Let me begin by simplifying the entire concept of data structures and algorithms.

An algorithm is simply a fancy phrase for a software that solves a problem or a set of instructions for solving a problem.
Algorithms may appear to be a bit like a magic act at first glance. You can see the trick's input and output, but everything that happens in between appears to be a bit magical.
My goal is to walk you through the phases of the trick and explain what materials you'll need to pull it off.
The resources are now what we term data structures or a common pattern in the problem in this illustration.

However, I cannot guarantee that I will go over every last detail, but I believe that after this, you will be able to apply algorithms to problems you may encounter as a developer.

Let's take a closer look at Python Data Structures

Data Structures are data storage and organization approaches that make it easier to alter, navigate, and access data. They also decide how data is collected, what functions we may use to access it, and how data is related to one another.

The primary purpose of data structures is to provide a method for organizing data so that it may be used effectively. There are also several data structures suitable for various applications, some of which are highly specialized for certain purposes.

Python algorithms are a thorough series of instructions that aid in the data processing for a certain purpose.

Let's look at the different forms of data structures now.

Different Types of Data Structures

There are two types of data structures: primitive and non-primitive, with linear and non-linear data structures falling within the non-primitive category.

Linear Data Structures

A linear data structure is one in which data items are ordered consecutively or linearly, with each member attached to its previous and next neighboring elements.
A single level is involved in a linear data structure. As a result, we can explore all of the elements in only one run. Because computer memory is organized in a linear fashion, linear data structures are simple to build. Lists, Linked Lists, Stacks, and Queues are some examples.

Non-Linear Data Structures

Non-linear data structures are data structures in which data elements are not ordered sequentially or linearly. A single level is not involved in a non-linear data structure. As a result, we won't be able to traverse all of the elements in a single run. In comparison to linear data structures, non-linear data structures are more difficult to construct. In comparison to a linear data structure, it makes better use of computer memory. Trees and graphs are two examples.

A chart showing  th types of Data Structures we have

Types of Data Structures and Their Meanings

We saw that there are linear and non-linear data structures in the previous section, so in this section we'll pick several data structures and see what they signify and how to work with.
We are also going to discuss about Sets, Tuples and Dictionaries in Python I believe they will also be of great interest to us.

Lists

List is, in my opinion, the most popular and simple Data Structure, as well as the most commonly used Data Structure. A list is a data structure that stores data in memory for subsequent use. Each list contains a predetermined number of cells, and each cell has a matching numeric index that is used to access data. You only need the required index to retrieve any of the data in the list whenever you want to use it.

Multiple items can be stored in a single variable using lists. Square brackets [] are used to make a list, with commas separating elements. Lists can contain items of a single item type in most cases, but they can also contain things of several different types. To represent 2D and 3D grids like matrices, lists can be nested with other lists.
Example of a list

Linked Lists

Linked lists are a type of data structure that does not rely on physical data placement in memory. Connected lists employ a reference mechanism instead of indexes or positions: elements are stored in nodes that carry a pointer to the next node, and so on until all nodes are linked.

This approach enables for quick item placement and removal without the need for reorganization.
Linked Lists

Sets

A set is a group of elements that are not in any particular sequence. Every set element must be one-of-a-kind and unchangeable. Curly braces or the set function set() are used to make them. They share several features with lists, such as the ability to use in to see if they contain a specific item.

You must use set() to construct an empty set, as set{} creates an empty dictionary.

# empty set
print(set())
print(my_set:={})
Enter fullscreen mode Exit fullscreen mode

Dictionaries

Dictionaries are a collection of items that are not in any particular sequence. They're data structures that let you map any key to any value. When the key is known, dictionaries are optimized to retrieve values.

# empty dictionary
emoty_dict = {}

# dictionary with integer keys
num_dict = {1: "Vitalis", 2: "Kandie"}

Enter fullscreen mode Exit fullscreen mode

.get() is a helpful dictionary technique. It works in the same way as indexing, except if the key isn't found in the dictionary, it returns another value (by default, 'None').

bird = {'name': 'screech', 'color': 'blue'}

print('Name: ', person.get('name'))

print('color: ', bird.get('color'))

# value is not provided
print('origin: ', bird.get('origin')) 

# value is provided
print('origin: ', bird.get('origin', 'United Kingdom'))

Enter fullscreen mode Exit fullscreen mode

Tuples

Tuples are comparable to lists with the exception that they are immutable (they cannot be changed). They're made with parentheses (). We utilize indexing, much as in lists, to retrieve the values in the tuple. A TypeError occurs when a value in a tuple is reassigned. Tuples are quicker than lists, but they can't be modified.

# Empty tuple
empty_tuple = ()
print(empty_tuple)

# Tuple having integers
num_tuple = (2, 5, 4)
print(num_tuple)

# tuple with mixed datatypes
mix_tuple = (2, "Hiya", 5.4)
print(mix_tuple)

# nested tuple
nest_tuple = ("here", [1, 2, 3], (4, 5, 6))
print(nest_tuple)

#indexing
print(nest_tuple[0])
#here

Enter fullscreen mode Exit fullscreen mode

Stack

A stack is a linear data structure in which operations are carried out in a specific order. The sequence could be LIFO (Last In First Out) or FILO (First In Last Out) (First In Last Out).

There are numerous examples of stacks in the real world. Consider the canteen, where plates are heaped on top of one another. The plate at the top is the first to be removed, but the plate at the bottom is the one that stays in the stack the longest. As a result, it is easy to observe that it follows the LIFO (Last In First Out)/FILO (First In Last Out) order.

Stack Explanation

To implement stack as seen in the illustration above we need two simple operations:

  • push : It adds an element to the top of the stack.

  • pop: It takes an element from the top of the stack and removes it.

Operations:

  • Adding - This raises the size of the stack by adding items to it. The addition occurs at the very top of the stack.

  • Deletion consists of two conditions: first, if no element is present in the stack, the stack will underflow; second, if the stack contains items, the topmost element will be removed. It reduces the size of the stack.

  • Traversing entails going through each piece of the stack.

Queues

Queues and stacks are conceptually similar; both are sequential structures, but queues process elements in the order in which they were entered, rather than the most recent element.

As a result, queues are similar to FIFO (First In, First Out) versions of stacks. These serve as a request buffer, keeping requests in the order they were received until they can be processed.

Queues In Python

Trees

Trees are another type of relation-based data structure that is used to depict hierarchical hierarchies. Nodes, like linked lists, contain both data and pointers indicating their relationship to other nodes.

Every tree has a "root" node that all other nodes branch off of. All items right below it, referred to as "child nodes," are referenced from the root. This process repeats itself, with each child node branching out into further child nodes.

Trees illustration

Graphs

Graphs are a type of relational data structure that can be used to store web-like relationships. In graphs, each node, or vertex, has a title, a value, and a list of links (called edges) it has with other vertices.
Graph illustration

Graph Description 2

Various Operations that can be performed on Data Structures:

  1. Insertion: Add a new data item to the specified data item collection.

  2. Delete: Removes an existing data item from a collection of data items.

  3. Traversal: Only access each data item once in order to process it.

  4. Searching: If the data item exists in the given collection of data items, find its location.

  5. Sorting entails arranging data items in a specific order, such as ascending or descending for numerical data and dictionary order for alphanumeric data.

What are the Benefits of Data Structures?

You must understand Data Structures and how to execute various operations on them if you want to become a skilled programmer who creates good code😊.

Conclusion

Wow😍, I bet you gained a lot of knowledge from this article. In this fantastic series of Getting Started with Data Structures and Algorithms, keep an eye out for more from my blog.
You can also read this article to learn more about dictionaries, sets, tuples, and lists.
You can reach out to me on Twitter to learn more about any subject, and I will respond. Thank you for taking the time to read this😘.

Enjoy🎉

Top comments (18)

Collapse
 
flaviankyande profile image
Flavian Kyande

This is great 🌟

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very much @flaviankyande , Trusting you were able to get something from it 🤗.
I really appreciate your feedback.

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very much @oderofrancis , I very much appreciate your feedback

Collapse
 
apoorv14 profile image
apoorvwankar

Great Work.🙌🙌

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very much I really appreciate you for taking your time to read this 👏🤩

Collapse
 
smartjeff profile image
Jeff Odhiambo

A very great article, keep it up

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very @killallnano I really appreciate it 🤗. Will surely Keep moving🌟

Collapse
 
oderofrancis profile image
FRANCIS ODERO

Awesome work

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very @oderofrancis I greatly appreciate your feedback 🤗

Collapse
 
abbi_mckann profile image
Abbi_McKann

Nice work, questionable pancakes :p

Collapse
 
brayan_kai profile image
Brayan Kai

Thanks 👍 really glad you took Time to read 🤗

Collapse
 
dmaina5054 profile image
Daniel Maina

Great Job

Collapse
 
avizyt profile image
🌠Avijit Biswas🇮🇳

Very elaborative and good.

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very much @avizyt , Really appreciate it

Collapse
 
miss_bironga profile image
Bironga Empire

Great content, very helpful

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very much @miss_bironga , For taking your time to read this trusting you learnt a lot from it

Collapse
 
owino profile image
Owino

This is fantastic!

Collapse
 
brayan_kai profile image
Brayan Kai

Thank you very much @owino , hope the article was of great help to you🤗 , thank you very much for your feedback 😊