Data Structure
Data Structure
A data structure is not only used for organizing the data. It is also used for
processing, retrieving, and storing data. There are different basic and advanced
types of data structures that are used in almost every program or software system
that has been developed. So we must have good knowledge of data structures.
Data structures are an integral part of computers used for the arrangement of
data in memory. They are essential and responsible for organizing, processing,
accessing, and storing data efficiently. But this is not all. Various types of data
structures have their own characteristics, features, applications, advantages, and
disadvantages. So how do you identify a data structure that is suitable for a
particular task? What is meant by the term ‘Data Structure’? How many types of
data structures are there and what are they used for?
How Data Structure varies from Data Type:
We already have learned about data structure. Many times, what happens is that
people get confused between data type and data structure. So let’s see a few
differences between data type and data structure to make it clear.
Data Type Data Structure
• The data type is the form of a • Data structure is a collection of
variable to which a value can be different kinds of data. That
assigned. It defines that the entire data can be represented
particular variable will assign the using an object and can be used
values of the given data type throughout the program.
only.
• It can hold value but not data. • It can hold multiple types of data
Therefore, it is dataless. within a single object.
• The implementation of a data • Data structure implementation is
type is known as abstract known as concrete
implementation. implementation.
• There is no time complexity in • In data structure objects, time
the case of data types. complexity plays an important
role.
• In the case of data types, the • While in the case of data
value of data is not stored structures, the data and its value
because it only represents the acquire the space in the
type of data that can be stored. computer’s main memory. Also,
a data structure can hold
different kinds and types of data
within one single object.
• Data type examples are int, • Data structure examples are
float, double, etc. stack, queue, tree, etc.
Classification :
Linear data structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and next
adjacent elements is called a linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
Static data structure: Static data structure has a fixed memory size. It
is easier to access the elements in a static data structure.
An example of this data structure is an array.
Dynamic data structure: In the dynamic data structure, the size is not
fixed. It can be randomly updated during the runtime which may be
considered efficient concerning the memory (space) complexity of the
code.
Examples of this data structure are queue, stack, etc.
Non-linear data structure: Data structures where data elements are not placed
sequentially or linearly are called non-linear data structures. In a non-linear
data structure, we can’t traverse all the elements in a single run only.
Examples of non-linear data structures are trees and graphs.
Need Of Data structure :
The structure of the data and the synthesis of the algorithm are relative to each
other. Data presentation must be easy to understand so the developer, as well as
the user, can make an efficient implementation of the operation.
Data structures provide an easy way of organizing, retrieving, managing, and storing
data.
Here is a list of the needs for data.
• Data structure modification is easy.
• It requires less time.
• Save storage memory space.
• Data representation is easy.
• Easy access to the large database.
Arrays:
An array is a linear data structure and it is a collection of items stored at contiguous
memory locations. The idea is to store multiple items of the same type together in
one place. It allows the processing of a large amount of data in a relatively short
period. The first element of the array is indexed by a subscript of 0. There are
different operations possible in an array, like Searching, Sorting, Inserting,
Traversing, Reversing, and Deleting.
Characteristics of an Array:
An array has various characteristics which are as follows:
Arrays use an index-based data structure which helps to identify each of the
elements in an array easily using the index.
If a user wants to store multiple values of the same data type, then the array
can be utilized efficiently.
An array can also handle complex data structures by storing data in a two-
dimensional array.
An array is also used to implement other data structures like Stacks, Queues,
Heaps, Hash tables, etc.
The search process in an array can be done very easily.
Operations performed on array:
Initialization: An array can be initialized with values at the time of declaration
or later using an assignment statement.
Accessing elements: Elements in an array can be accessed by their index, which
starts from 0 and goes up to the size of the array minus one.
Searching for elements: Arrays can be searched for a specific element using
linear search or binary search algorithms.
Sorting elements: Elements in an array can be sorted in ascending or
descending order using algorithms like bubble sort, insertion sort, or quick sort.
Inserting elements: Elements can be inserted into an array at a specific
location, but this operation can be time-consuming because it requires shifting
existing elements in the array.
Deleting elements: Elements can be deleted from an array by shifting the
elements that come after it to fill the gap.
Updating elements: Elements in an array can be updated or modified by
assigning a new value to a specific index.
Traversing elements: The elements in an array can be traversed in order,
visiting each element once.
Applications of Array:
Different applications of an array are as follows:
An array is used in solving matrix problems.
Database records are also implemented by an array.
It helps in implementing a sorting algorithm.
It is also used to implement other data structures like Stacks, Queues, Heaps,
Hash tables, etc.
An array can be used for CPU scheduling.
Can be applied as a lookup table in computers.
Arrays can be used in speech processing where every speech signal is an array.
The screen of the computer is also displayed by an array. Here we use a
multidimensional array.
The array is used in many management systems like a library, students,
parliament, etc.
The array is used in the online ticket booking system. Contacts on a cell phone
are displayed by this array.
In games like online chess, where the player can store his past moves as well as
current moves. It indicates a hint of position.
To save images in a specific dimension in the android Like 360*1200
Linked list:
A linked list is a linear data structure in which elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers as shown
in the below image:
Types of linked lists:
• Singly-linked list
• Doubly linked list
• Circular linked list
• Doubly circular linked list
Stack
Stack is a linear data structure that follows a particular order in which the
operations are performed. The order is LIFO (Last in first out). Entering and
retrieving data is possible from only one end. The entering and retrieving of data is
also called push and pop operation in a stack. There are different operations
possible in a stack like reversing a stack using recursion, Sorting, Deleting the
middle element of a stack, etc.
LIFO is an abbreviation for last in, first out. It is a method for handling data
structures where the first element is processed last and the last element is
processed first.
There is a bucket that holds balls.
Different types of balls are entered into the bucket.
The ball to enter the bucket last will be taken out first.
The ball entering the bucket next to last will be taken out after the ball above it
(the newer one).
In this way, the ball entering the bucket first will leave the bucket last.
Therefore, the Last ball (Blue) to enter the bucket gets removed first and the
First ball (Red) to enter the bucket gets removed last.
Queue:
Queue is a linear data structure that follows a particular order in which the
operations are performed. The order is First In First Out (FIFO) i.e. the data item
stored first will be accessed first. In this, entering and retrieving data is not done
from only one end. An example of a queue is any queue of consumers for a
resource where the consumer that came first is served first. Different operations
are performed on a Queue like Reversing a Queue (with or without using recursion),
Reversing the first K elements of a Queue, etc. A few basic operations performed In
Queue are enqueue, dequeue, front, rear, etc.
FIFO is an abbreviation for first in, first out. It is a method for handling data
structures where the first element is processed first and the newest element is
processed last.
There is a ticket counter where people come, take tickets and go.
People enter a line (queue) to get to the Ticket Counter in an organized manner.
The person to enter the queue first will get the ticket first and leave the queue.
The person entering the queue next will get the ticket after the person in front
of him
In this way, the person entering the queue last will the tickets last
Therefore, the First person to enter the queue gets the ticket first and the Last
person to enter the queue gets the ticket last.
Characteristics of a Queue:
The queue has various different characteristics which are as follows:
The queue is a FIFO (First In First Out) structure.
To remove the last element of the Queue, all the elements inserted before the
new element in the queue must be removed.
A queue is an ordered list of elements of similar data types.
Applications of Queue:
Different applications of Queue are as follows:
Queue is used for handling website traffic.
It helps to maintain the playlist in media players.
Queue is used in operating systems for handling interrupts.
It helps in serving requests on a single shared resource, like a printer, CPU task
scheduling, etc.
It is used in the asynchronous transfer of data e.g. pipes, file IO, and sockets.
Queues are used for job scheduling in the operating system.
In social media to upload multiple photos or videos queue is used.
To send an e-mail queue data structure is used.
To handle website traffic at a time queues are used.
In Windows operating system, to switch multiple applications.
Operation performed on queue:
A queue is a linear data structure that implements the First-In-First-Out (FIFO)
principle. Here are some common operations performed on queues:
Enqueue: Elements can be added to the back of the queue, adding a new
element to the end of the queue.
Dequeue: The front element can be removed from the queue by performing a
dequeue operation, effectively removing the first element that was added to the
queue.
Peek: The front element can be inspected without removing it from the queue
using a peek operation.
IsEmpty: A check can be made to determine if the queue is empty.
Size: The number of elements in the queue can be determined using a size
operation.
Tree:
A tree is a non-linear and hierarchical data structure where the elements are
arranged in a tree-like structure. In a tree, the topmost node is called the root
node. Each node contains some data, and data can be of any type. It consists of a
central node, structural nodes, and sub-nodes which are connected via edges.
Different tree data structures allow quicker and easier access to the data as it is a
non-linear data structure. A tree has various terminologies like Node, Root, Edge,
Height of a tree, Degree of a tree, etc.
There are different types of Tree-like
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
Characteristics of a Tree:
The tree has various different characteristics which are as follows:
A tree is also known as a Recursive data structure.
In a tree, the Height of the root can be defined as the longest path from the root
node to the leaf node.
In a tree, one can also calculate the depth from the top to any node. The root
node has a depth of 0.
Applications of Tree:
Different applications of Tree are as follows:
Heap is a tree data structure that is implemented using arrays and used to
implement priority queues.
B-Tree and B+ Tree are used to implement indexing in databases.
Syntax Tree helps in scanning, parsing, generation of code, and evaluation of
arithmetic expressions in Compiler design.
K-D Tree is a space partitioning tree used to organize points in K-dimensional
space.
Spanning trees are used in routers in computer networks.
Characteristics of Graph:
The graph has various different characteristics which are as follows:
The maximum distance from a vertex to all the other vertices is considered the
Eccentricity of that vertex.
The vertex having minimum Eccentricity is considered the central point of the
graph.
The minimum value of Eccentricity from all vertices is considered the radius of a
connected graph.
Applications of Graph:
Different applications of Graphs are as follows:
The graph is used to represent the flow of computation.
It is used in modelling graphs.
The operating system uses Resource Allocation Graph.
Also used in the World Wide Web where the web pages represent the nodes.
The node contains a pointer to the next node means that the node stores the
address of the next node in the sequence. A single linked list allows the traversal of
data only in one way.
Doubly Linked List
A doubly linked list or a two-way linked list is a more complex type of linked list that
contains a pointer to the next as well as the previous node in sequence.
Therefore, it contains three parts of data, a pointer to the next node, and a pointer
to the previous node. This would enable us to traverse the list in the backward
direction as well. Below is the image for the same: E is being inserted at the
beginning of the doubly linked list.
Inserting a new node in a doubly linked list is very similar to inserting new node in
linked list. There is a little extra work required to maintain the link of the previous
node. A node can be inserted in a Doubly Linked List in four ways:
At the front of the DLL.
In between two nodes
After a given node.
Before a given node.
At the end of the DLL.
Add a node at the front in a Doubly Linked List:
The new node is always added before the head of the given Linked List. The task
can be performed by using the following 5 steps:
1. Firstly, allocate a new node (say new_node).
2. Now put the required data in the new node.
3. Make the next of new_node point to the current head of the doubly linked list.
4. Make the previous of the current head point to new_node.
5. Lastly, point head to new_node.
What is Stack
A stack is a linear data structure in which the insertion of a new element and
removal of an existing element takes place at the same end represented as the top
of the stack.
To implement the stack, it is required to maintain the pointer to the top of the
stack, which is the last element to be inserted because we can access the elements
only on the top of the stack.
Push:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
Algorithm for push:
Pop:
Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be
an Underflow condition.
Algorithm for pop:
Top
Returns the top element of the stack.
Algorithm for Top
isEmpty:
Returns true if the stack is empty, else false.
Algorithm for isEmpty:
Types of Stacks:
Fixed Size Stack: As the name suggests, a fixed size stack has a fixed size and
cannot grow or shrink dynamically. If the stack is full and an attempt is made to
add an element to it, an overflow error occurs. If the stack is empty and an
attempt is made to remove an element from it, an underflow error occurs.
Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When
the stack is full, it automatically increases its size to accommodate the new
element, and when the stack is empty, it decreases its size. This type of stack is
implemented using a linked list, as it allows for easy resizing of the stack.
In addition to these two main types, there are several other variations of Stacks,
including:
1. Infix to Postfix Stack: This type of stack is used to convert infix expressions to
postfix expressions.
2. Expression Evaluation Stack: This type of stack is used to evaluate postfix
expressions.
3. Recursion Stack: This type of stack is used to keep track of function calls in a
computer program and to return control to the correct function when a function
returns.
4. Memory Management Stack: This type of stack is used to store the values of the
program counter and the values of the registers in a computer program,
allowing the program to return to the previous state when a function returns.
5. Balanced Parenthesis Stack: This type of stack is used to check the balance of
parentheses in an expression.
6. Undo-Redo Stack: This type of stack is used in computer programs to allow users
to undo and redo actions.
Stack is a linear data structure that operates on the LIFO principle and can be
implemented using an array or a linked list. The basic operations that can be
performed on a stack include push, pop, and peek, and stacks are commonly used
in computer science for a variety of applications, including the evaluation of
expressions, function calls, and memory management. There are two ways to
implement a stack –
Using array
Using linked list
Characteristics of Queue
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the
Queue is implemented using the array. Variables used in this case are
Queue: the name of the array storing queue elements.
Front: the index where the first element is stored in the array representing the
queue.
Rear: the index where the last element is stored in an array representing the
queue.
Implement a Queue in Python
There are various ways to implement a queue in Python. This article covers the
implementation of queue using data structures and modules from Python library.
Python Queue can be implemented by the following ways:
Implementation using list
List is a Python’s built-in data structure that can be used as a queue. Instead of
enqueue () and dequeue(), append() and pop() function is used. However, lists are
quite slow for this purpose because inserting or deleting an element at the
beginning requires shifting all of the other elements by one, requiring O(n) time.
The code simulates a queue using a Python list. It adds elements ‘a’, ‘b’, and ‘c’ to
the queue and then dequeues them, resulting in an empty queue at the end. The
output shows the initial queue, elements dequeued (‘a’, ‘b’, ‘c’), and the queue’s
empty state.