Computers are all about Data and instructions to be performed on the data. A data structure is a way of organizing the collection of data. Later on, this data can be accessed, queried, or even updated easily and quickly. In other words, a data structure is any data representation and its associated operations. Even an integer or string stored on the computer can be viewed as a simple data structure.
Data Structures are of two types, Linear data structures, and Non-linear data structures. In Linear data structures, elements are arranged in a sequence i.e. one after the other. For example, array stack, queue, etc are the linear data structures.
In Non-Linear data structures elements are arranged in a hierarchical order i.e. one element can be connected to multiple elements. Examples of non-linear data structures are graphs, trees, etc.
Data structures are used almost everywhere in our lives. Say for example, while using the browser linked list is used to keep track of previous and next websites, for contacts on your phone HashMap is be used, the stack is used in a calculator to evaluate an expression, etc.
In different scenarios, different data structures are used as per the requirement of the operations to be performed on the data. Data is organized in a way that accessing time of data can be minimized and thus fast results can be obtained.
They are a crucial part of designing efficient software and algorithms. Some of the common operations to be performed on the data include sorting, data exchange, indexing, searching, etc.
There is no
the best data structure that can be used in every situation. Depending on the requirement of the situation whichever data structure gives an optimum solution is best and to be chosen. Data structures are chosen that uses less space, and less amount of time, and also, they must not be too complex to implement.
To provide a solution to a problem, it is first analyzed, then the performance goals are determined that must be achieved and then the solution can be presented by selecting the right data structure for the job.
Let us look at each of these data structures in detail:
An array is the simplest and most commonly used data structure. It is a fixed-length container containing n elements of the same type. The elements are kept in sequence even in memory. Elements can be accessed using its index starting from 0 to n-1. Thus, it is easier to insert and retrieve information using arrays.
Operations:
Use case:
The stack data structure uses the LIFO (Last In First Out) principle to insert and remove elements i.e. the elements entered last will be removed first. It is famous for its UNDO operation. The elements cannot be removed or inserted in the middle position. It maintains a top position variable at which elements can be removed or added.
Operations:
Use case:
Queue Data Structure follows the FIFO (First In First Out) principle. The elements that are added first are removed first as well. The elements can only be added at the rear end and can be removed from the front end.
Operations:
Use case:
LinkedList data structure not only stores the element but also stores the link to the next element in the list. The data value assigned to the element or node as well as the pointer in the memory address refers to the next node in the list is contained in a list.
Operations:
Use case:
The tree is a hierarchical data structure where its elements are organized hierarchically. It is a non-linear data structure. A tree node contains the element and the link to the child elements.
Operations:
Use case:
The graph is a non-linear data structure, in which elements are stored in form of a network. It is a set of Vertices and Edges. The pair (x,y) contains two vertices, it indicates the edge between two vertices. Edge may contain weight for weighted graph. Graphs are of two types directed and undirected. Graphs are also weighted and non-weighted.
Operations:
Add Vertex - to add a vertex to the graph.
Add Edge - to add an edge between the two vertices of the graph.
Display Vertex - to display vertices of the graph.
Use case:
Trie data structure is efficient for solving string-related problems. It helps in the fast retrieval of search results.
Use case:
Hashing is used to uniquely identify elements in a table. Thus, hashing makes storage, retrieval, and update easier for a database. Each object is uniquely identified by a unique index called the "key". The object can be searched using this key.
Operations:
Use case: