# Types of Data structures with examples

Summary:

Data structures are one of the most important things to optimize the performance of any program. There are different types of data structures which are suitable for a specific purpose. It is important to choose the data structure type with the purpose.

In order to make better decisions on which data structures to use we need to know about them in detail.

## What are Data Structures?

Data structures are a way to store data in a specific manner inside computer memory.

Different data structures have a different way to store and organise data, depending on the situation one may be efficient and others may not.

For example:

The array data structure may be efficient to store data when we already know the length of data or we can use Linked lists that adds a new node with each new data.

## Types of Data Structures

### Primitive Data Structures

Primitive data structures are fundamental data structures that are already defined by the language. They are single values whose size and type are fix and do not have any special methods.

Some examples of primitive data structures will be integers, float, double, char, pointers.

Primitive data structures are the building blocks of non-primitive data structures.

#### Integer

The integers data type is one of the most used data structures which is used to store numbers from negative infinity, 0 and up to positive infinity `(...-4, -3, -2, -1, 0, 1, 2, 3, 4, ......)` except for fractions.

#### Float

Float data types store decimal values, so as to double. The difference is that double is more precise than float.

#### Character

Character data types store a single character in memory.

#### Boolean

The most used data type for decision making. They hold true or false value depending on any condition.

### Non-primitive Data Structures Types

Non-primitive data structures are those which are dependent on primitive data structures. They hold a reference to a memory location which hold the data.

#### Linear

Linear data structures are those which are linear in nature. Traversal of linear data structures can be don’t in one iteration.

Each element is attached to its previous and next element only (only one level of data and no hierarchy).

Computer memory is also linear in nature so it is easy to implement linear data structures.

##### Array

Array is the most used data structure and is very useful when we already know the length of data that we want to store.

It is a linear data structure, we can get individual values through the index position.

It has a limitation that it can store only one data type.

It is the collection of nodes in which each node consisting of a data value and reference to the next node.

Open Image

Linear lists are better than array because we can easily a new node with data. The length can change with new items.

##### Stack

It is LIFO (last in first out) data structure. The last element that enters in the stack Is the one which comes out first.

Open Image

Stack have various uses like in recursion, evaluate prefix, postfix and infix notation.

Array and Linked list can be used to implement a stack.

##### Queue

The queue is FIFO (first in first out) linear data structures. The element that enters first in the queue is the one who leaves first (just like an ordinary queue in real life).

Open Image

It has various uses like to queue tasks when the CPU or memory resources are full and not available to use.

#### Non-linear

Non-linear data structures contain multiples levels of data. Each element may be connected to two or more items.

Traversal of non-linear data structures is not possible in one traversal because of multiple levels of data (child-parent hierarchy).

Non-linear data structures are more memory efficient than linear data structures.

##### Tree

The tree is one of the most used types of data structures. It is non-linear and can consist of no nodes or a structure of a node as root and multiple subtrees connected to it.

##### Graph

A graph data structure type that contains nodes and lines that may connect them.

Graphs can be of two types:

Connected Graph: Graph in which all nodes are connected via lines.

Disconnected Graph: Graph in which all nodes are not connected.

The difference between tree and graph is that a graph can form a closed loop but a tree cannot.

## Summary

Here is a summary organisation.

1. Primitive Data Structures
1. Integer
2. Float
3. Character
4. Boolean
2. Non Primitive Data Structures
1. Linear
1. Array
3. Stack Queue
2. Non-Linear
1. Tree
2. Graph

I have used words like data structures and data types. Both have some difference between them.

Data types are the smallest unit to store data. They cannot break down into subparts. Like we cannot break integer type in any subpart.

Data structures can be complex in nature they contain several data types within them. Like an array may contain several integer values.

I hope it is relatable. If you have any problem let me know in the comments.