Rafael Maia
1 supporter
Introduction to Data Structures

Introduction to Data Structures

May 03, 2024

Data Structures are an extremely important concept for anyone who wants to be a great programmer, as such knowledge allows you to write more efficient code.

This article walks you through the concept of data structures at an introductory level, so you can start to understand that concept.

What is a Data Structure?

You can guess it from the name, it's a structure of data, which means that it is a way of organizing data on a computer so we can efficiently store, modify and access it later. It defines how each piece of data relates to each other, so we can perform specific operations on them.

Classifying Data Structures

There are two ways to classify data structures:

  • Linear data structures

  • Non-linear data structures

Linear Data Structures

In linear data structures, the pieces of information (called elements) are stored sequentially one after the other. Because of that, linear data structures are easy to implement. However, when the data grows too much, linear data structures are not a good choice due to operational costs.

Linear Data Structure Examples

1. Arrays

In an array, elements are stored sequentially in memory, i.e. the locations where the elements are stored are close to each other in the computer's memory.

Each element is represented by an index, which is a number that identifies the location where an element is stored within the array. For example, the letter "D" in the above array is at index 3.

2. Stacks

Elements in a stack are stored in a way that the last element added is removed first.

It's like a pile of books where the book on the top is removed first.

This idea is called the LIFO principle. LIFO stands for Last-In-First-Out, i.e. the last element added is the first one to be removed.

3. Queues

Unlike stacks, queues are data structures where the first element that was stored is removed first.

It's just like a real-life queue where the person who gets into the queue first in a box office will buy the ticket first.

This idea follows the FIFO principle. FIFO stands for First-In-First-Out, i.e. the first element stored is removed first.

Non-Linear Data Structures

Elements in non-linear data structures are stored in a hierarchical manner where elements can be interconnected in various ways to one or more other elements forming more complex relationships.

Non-Linear Data Structure Examples

1. Graph Data Structure

In graph data structures, elements are called vertices and each vertex is connected to other vertices through edges.

Elements (called nodes or vertices) are connected by edges and there may be multiple edges between two nodes.

2. Tree Data Structure

It's somewhat similar to graphs, with the differences that it only goes down, i.e. it only goes from elements higher in the hierarchy to elements that are lower, and there cannot be multiple edges between two vertices.

Elements lower in the hierarchy are called children, while elements higher in the hierarchy are called parents.

Do you see why it's called a tree? It's because it forms branches as new elements are added. The first node, i.e. the node that is the parent of all other nodes, is called the root of the tree.

In fact, you could even make some kind of art with this data structure:

This one I'll name "Computational Tree".

Differences between Linear and Non-Linear Data Structures

Why are data structures important?

By knowing the workings of data structures, programmers can write code that is efficient in terms of both time and memory.

Apart from efficiency, data structures can also make it easier to understand the information you're working with by organizing it in a logical way.

Data structures also hide the implementation details of data storage and retrieval, allowing programmers to work at a higher level of abstraction.

Sources

Enjoy this post?

Buy Rafael Maia a coffee

More from Rafael Maia