What is a Data Structure?
We knowingly or unknowingly use data structures in our day-to-day life.
- We form a Queue to board public transport.
We will arrange the books on a shelf in a particular order like A-Z, by the height of the books.
We search in a particular way to find a number in a telephone directory.
- We may always tend to choose the shortest path, from one location to another location, in Maps.
And the list goes on....!
So essentially a data structure is something we rely on for storing and organizing the data.
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output.
Type of Data Structures
In general, we have two types of data structures, linear and non-linear.
Linear Data Structure.
In linear data structures, the elements are arranged in sequence one after the other. Since elements are arranged in a particular order, they are easy to implement.
However, when the complexity of the program increases, the linear data structures might not be the best choice because of operational complexities.
Examples of Linear Data Structures
Array
In an array, elements in memory are arranged in continuous memory. All the elements of an array are of the same type.
Stack
In the stack data structure, elements are stored in the LIFO principle. That is, the last element stored in a stack will be removed first.
Queue
Unlike stack, the queue data structure works in the FIFO principle where the first element stored in the queue will be removed first.
Linked List
In a linked list data structure, data elements are connected through a series of nodes. And, each node contains the data items and addresses to the next node.
Non-Linear Data Structures
Unlike linear data structures, elements in non-linear data structures are not in any sequence. Instead, they are arranged in a hierarchical manner where one element will be connected to one or more elements.
Non-linear data structures are further divided into the graph and tree-based data structures.
Examples of Non-Linear Data Structures
Graphs
In the graph data structure, each node is called a vertex and each vertex is connected to other vertices through edges.
Tree-Based Data Structure
Similar to a graph, a tree is also a collection of vertices and edges. However, in a tree data structure, there can only be one edge between two vertices.
Linear Vs. Non-Linear Data Structures
Finally
A warrior should not just possess a weapon, he must know when and how to use it. Thus one should know to use an appropriate data structure in an algorithm.