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.
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.
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 data types store decimal values, so as to double. The difference is that double is more precise than float.
Character data types store a single character in memory.
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 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 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.
Linear lists are better than array because we can easily a new node with data. The length can change with new items.
It is LIFO (last in first out) data structure. The last element that enters in the stack Is the one which comes out first.
Stack have various uses like in recursion, evaluate prefix, postfix and infix notation.
Array and Linked list can be used to implement a stack.
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).
It has various uses like to queue tasks when the CPU or memory resources are full and not available to use.
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.
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.
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.
Here is a summary organisation.
- Primitive Data Structures
- Non Primitive Data Structures
- Linked List
- Stack Queue
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.