Data Structures: An Intro…
Tags: javascript, algorithms, datastructures, programming
Published: 2022-10-22 09:11:37

---
title: Data Structures: An Intro…
published: true
date: 2022-10-03 20:03:49 UTC
tags: javascript,algorithms,datastructures,programming
canonical_url: https://medium.com/@ab17dz/data-structures-an-intro-c94b88a41757
---

### Data Structures and Algorithms: An Intro…

In this new series of stories, we will be covering different types of data structures and algorithms and their implementation in **_JavaScript_**.

By the end of this series, I hope that you’ll acquire the necessary tools that will help you to pick the right data structure to solve any specific problem. Moreover, I hope also that this content would help you to have an easier time answering future interview questions about data structures.

In this very first story, we will have a general overview of data structures. So without any further ado, let’s get into business…

#### Data Structures: Concepts

To explain what are data structures, let’s reverse engineer the expression ‘_data structure_’. We can clearly see that it is composed of two main terms, **data** and **structure.**

‘_data_’ can be generally defined as a _set of items_. Whereas, ‘_structure_’ can be defined as the _arrangement of something_. Now, if we combine the two definitions, we will have something like this: a data structure is _an arrangement of a set of items._

In the context of computer science, a data structure is a way of arranging data in a specialized format on a computer memory so that it can be efficiently managed i.e. _stored_, _processed_, and _retrieved_.

Now, to build a solid foundation in data structures, I would like to walk you through some important related concepts first.

**_# How Is Data Stored in Computers?_**

Data are stored in memory as numbers composed of 0 and 1 digits (based on the binary system). Each digit is called ‘ **_Bit_** ’. A bit is the smallest unit of memory. At the level of hardware, each bit is an electrical device called a ‘ **_transistor_** ’. When the transistor is OFF, it represents the value 0 in memory. On the other hand, when it is ON, it represents the value 1.

You can imagine memory in a computer as a stack of shelves. Each shelf is a succession of eight bits. Every eight bits (a shelf) is often called a ‘ **_byte_** ’. Each shelf is accessible via a unique identifier.

![](https://cdn-images-1.medium.com/max/609/1*hdi6Fc4CoKi_c6bhcJSiHA.png)

When you write ‘_var a = 1;’_ in your code_,_ your machine would allocate 8 shelves (8 bytes) starting at the memory address ‘0’ (The memory address could be any other unique identifier, we’re assuming it is 0 just for the sake of this example). The binary representation of the number 1 would look something like _00000000.00000000.00000000.00000000.00000000.00000000.00000000.0000000_ **_1_**.

If you would like, another day, access the value of the variable _a,_ your machine knows that it is at the memory address ‘0’ and it is a number. Therefore, it is going to count 8 bytes (case of JavaScript).

Different types of data have different representations in your computer’s memory. This is what leads us to talk about the next concept…

**_# What Are Data Types?_**

Simply put, a data type identifies what type of value a variable holds and what set of operations are allowed on it. A data type gives an insight to the compiler (or the interpreter) on how would you (as a programmer) use a variable.

Different programming languages have different types of data. But they can be mainly classified into two categories. There are **primitive** data types that are predefined by the language such as integers, floats, booleans, strings, chars…etc. They represent the most basic immutable types of values that a variable can accept and are implemented differently in different languages. On the other side, there are **composite** data types ( native to the language or defined by the user). These are constructed using a combination of primitives such as objects, arrays, maps, lists, stacks…etc

Since we are concerned with JavaScript. It has seven (07) primitives: Number, String, Undefined, Null, Boolean, BigInt, and Symbol. And many composite data types: Object, Function, Array, Set, Map, Date,…etc. You can find an exhaustive list of built-in data types in JavaScript [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects).

**_# How do Data Structures Relate to Data Types?_**

Actually, when we’ve been talking about composite (compound) data types, we were referring at the same time to data structures. If we would like to define data structures once again, we could say that a data structure is a collection of data types. However, they differ from primitive data types in the way they are implemented. What do we mean by that?

The implementation of primitive data types is abstract (hidden) and language-specific. For example, a value is directly assigned to a primitive data type, and most likely you don’t know how it is done.

On the contrary, data structures (composite data types) are concretely implemented. It means that they concretely define the way to deal with the data. For example, to add an element to a ‘_stack_’, you would use the _push() _method.

#### Data Structures: The Classification

Despite the choice of the programming language, data structures can be categorized in different ways. Though, generally, they can be classified as Linear and Non-Linear data structures.

![](https://cdn-images-1.medium.com/max/667/1*w__qX4VVxIgaFd-QNcSYAQ.png)

A data structure is said to be linear if the data is arranged in a straight sequential order one after the other. Good examples of that are arrays, stacks, queues, and linked lists as shown in the diagram above.

On the other hand, a data structure is said to be non-linear when the data is arranged in non-sequential order, or in a hierarchical manner instead. Examples of that are graphs and trees.

#### Data Structures: Basic Operations

The data within a data structure is processed by means of a set of different methods. However, it is common that the following seven(07) basic operations are used for data management in data structures.

- **Selecting:** accessing a particular item within a data structure.
- **Traversing** : visiting each item within a data structure at least once.
- **Inserting** : adding a new item to a data structure.
- **Deleting** : removing an item from a data structure.
- **Searching** : finding a specific item within a data structure that satisfies one or more conditions.
- **Sorting:** arranging the items within a data structure in a specific logical order.
- **Merging:** Combining two data structures into a single one.

#### Data Structures: The Right Choice

Choosing the right data structure to solve a particular problem would depend on many factors like,

- **Operations frequency** : you should take into consideration the frequency with which operations are performed since it is going to help you quantify the resource constraints (mainly time and memory).
- **Time complexity** : same operations have different time execution in different data structures. Therefore, if you think time is crucial for you to solve a problem, you need then to take time complexity into consideration for your data structure choice.
- **Space complexity** : same as in time complexity, if the amount of memory space is a crucial constraint for you to solve a problem, then you should choose the data structure that fits this requirement.

Time complexity and space complexity are often expressed in **_big O notation_** _ — If you don’t know what is big O notation, don’t worry. This is going to be the object of our next story in this series._

#### Data Structures: Benefits

We can sum up the need for data structures in two main points,

**_# Effeciency_**

We have already mentioned that choosing the right data structure to solve a problem helps you to increase the efficiency of your solution. Therefore, data is processed in the most appropriate way possible.

**_# Abstraction_**

Through the use of data structures, you ensure a high level of readability and simplicity by hiding the low-level details of the associated operations. The only thing a data structure provides is an interface to manipulate the data.

That’s it for this first read in this series.

**Edit** : the second part of this tutorial is ready…Here is it…

[Data Structures and Algorithms: Big O Notation](https://medium.com/@ab17dz/in-the-last-story-of-the-data-structures-and-algorithms-series-weve-briefly-mentioned-some-45ebddbbae8c)

If you have any questions or feedback, please, use the comment section. And stay tuned for upcoming stories…

**_Wait for a second, please! Before we leave, if you want, let’s connect…_**

- On [YouTube](https://www.youtube.com/channel/UC120v1rSlcmCkeeU7i5wHDw?sub_confirmation=1)
- On [Linkedin](https://www.linkedin.com/in/abdelkader-guendouz-490818187/)
- On [Twitter](https://twitter.com/Abdzg12)