Data Structure Types, Classifications and Applications
Data Structure Types, Classifications and Applications
A data structure is not only used for organizing the data. It is also used
for processing, retrieving, and storing data. Different basic and advanced
types of data structures are used in almost every program or software
system that has been developed. So we must have good knowledge of
data structures.
Data structure has many different uses in our daily life. There are many
different data structures that are used to solve different mathematical and
logical problems. By using data structure, one can organize and process
a very large amount of data in a relatively short period. Let’s look at
different data structures that are used in different situations.
Arrays:
Array
Characteristics of an Array:
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.
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:
Singly-linked list
Linked List
Stack:
Stack
Characteristics of a Stack:
The insertion and deletion are performed at one end i.e. from the
top of the stack.
In stack, if the allocated space for the stack is full, and still anyone
attempts to add more elements, it will lead to stack overflow.
Queue:
Queue
Characteristics of a Queue:
To remove the last element of the Queue, all the elements inserted
before the new element in the queue must be removed.
Tree
Characteristics of a Tree:
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.
Graph:
Characteristics of Graph:
Many times there are more than one ways to solve a problem with
different algorithms and we need a way to compare multiple ways. Also,
there are situations where we would like to know how much time and
resources an algorithm might take when implemented. To measure
performance of algorithms, we typically use time and space complexity
analysis. The idea is to measure order of growths in terms of input size.
The valid algorithm takes a finite amount of time for execution. The
time required by the algorithm to solve given problem is called time
complexity of the algorithm. Time complexity is very useful measure in
algorithm analysis.
Space Complexity:
Definition –
VIEW OF LIST
2. StackADT
Viewofstack
InStackADTImplementationinsteadofdatabeingstoredineach
node, the pointer to data is stored.
The program allocatesmemory for the data and address is
passedto the stack ADT.
The head node and the data nodes are encapsulated in the
ADT. Thecalling function can only see the pointer to the
stack.
Thestackheadstructurealsocontainsapointerto top and count of
number of entries currently in stack.
push()–Insertanelementatoneendofthestackcalledtop.
pop () – Remove and return the element at the top of the
stack, if it is not empty.
peek () – Return the element at the top of the stack without
removing it, if the stack is not empty.
size()–Returnthenumberofelementsinthestack.
isEmpty()–Returntrueifthestackisempty,otherwisereturnfalse.
isFull()–Returntrueifthestackisfull,otherwisereturnfalse.
3. QueueADT
View of Queue
The queue abstract data type (ADT) follows the basic design
of the stack abstract data type.
Each node contains a void pointer to the data and the link
pointer to the next element in the queue. The program’s
responsibility is to allocate memory for storing the data.
enqueue ()–Insert an element at the end of the queue.
dequeue () – Remove and return the first element of the
queue, if thequeue is not empty.
peek () – Return the element of the queue without removing
it, if thequeue is not empty.
size()– Return the number of elements in the queue.
isEmpty()– Return true if the queue is empty, other wise return
false.
is Full () – Return true if the queue is full, otherwise
return false. Features of ADT:
Abstract data types (ADTs) areaway of encapsulating data and
operations on that data into a single unit. Some of the key features
of ADTs include:
Abstraction: The user does not need to know the
implementation of the data structure only essentials are
provided.
Better Conceptualization: ADT gives us a better
conceptualization of the real world.
Robust: The program is robust and has the ability to catch
errors.
Encapsulation: ADTs hide the internal details of the data
and provide a public interface for users to interact with the
data. This allows for easier maintenance and modification of
the data structure.
Data Abstraction: ADTs provide a level of abstraction from
the implementation details of the data. Users only need to
know the operations that can be performed on the data, not
how those operations are implemented.
Data Structure Independence: ADTs can be implemented
using different data structures, such as arrays or linked lists,
without affecting the functionality of the ADT.
Information Hiding: ADTs can protect the integrity of the
data by allowing access only to authorized users and
operations. This helps prevent errors and misuse of the data.
Modularity: ADTs can be combined with other ADTs to
form larger, more complex data structures. This allows for
greater flexibility and modularity in programming.
Overall, ADTs provide a powerful tool for organizing and
manipulating data ina structured and efficient manner.
Implementation of a
4 Implementationofahighlevelconce simple concept
pt
It is rarely reusable
5 Itisusablebeyonditsoriginaluse. beyond its
original use.
6 Ithidestheinternaldetails. Itdoesn’thideanything.
7 Itusesclass. Itusesstructure.
Examples-Arrays,
8 Examples-lists,sets,stacks. linked lists, trees,
graphs.
SEARCHING
Searching is the fundamental process of locating a specific element or item within a collection of
data. This collection of data can be arrays, lists, trees, or other structured representations. Data
structures are complex systems designed to organize vast amounts of information. Searching
within a data structure is one of the most critical operations performed on stored data.
The goal is to find the desired information with its precise location quickly and with minimal
computational resources. It plays an important role in various computational tasks and real-world
applications, including information retrieval, data analysis, decision-making processes, etc.
Binary Search
1. Start with a sorted array or list. For binary search to work correctly, the elements must be in
ascending or descending order.
2. Set two pointers, low and high, to the beginning and end of the search space, respectively.
Initially, low = 0 and high = length of the array - 1.
3. Calculate the middle index using the formula: mid = (low + high) / 2. This will give you the index
of the element in the middle of the current search space.
4. Compare the target value with the element at the middle index:
o If they are equal, the target value has been found. Return the index of the middle
element.
o If the target value is less than the middle element, set high = mid - 1 and go to step 3.
o If the target value is greater than the middle element, set low = mid + 1 and go to step
3.
5. Repeat steps 3-4 until the target value is found or low > high. If low becomes greater than high,
it means the target value is not present in the array.
SORTING
Sorting in data structures helps arrange elements in a specific order, making
it easier to search, analyze, and visualize information. Let’s learn about
the various types of sorting algorithms, their workings, and their importance
in solving real-world problems.
For example, finding a name in a sorted list is quicker than finding it in an unsorted list.
Sorting is also important because it is often a first step in many other algorithms and data
operations.
Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted,
compares adjacent elements, and swaps them if they are in the wrong order. The process is
repeated until the list is sorted. It is called Bubble Sort because smaller elements "bubble"
to the top of the list.
Example:
Consider the list [4, 2, 7, 1].
First pass:
Compare 4 and 2, swap to get [2, 4, 7, 1]
Compare 4 and 7, no swap
Compare 7 and 1, swap to get [2, 4, 1, 7]
Second pass:
Compare 2 and 4, no swap
Compare 4 and 1, swap to get [2, 1, 4, 7]
Third pass:
Compare 2 and 1, swap to get [1, 2, 4, 7]
Fourth pass:
List is already sorted, no swaps needed
Final sorted list: [1, 2, 4, 7].
Selection Sort
Selection Sort is a simple comparison-based sorting algorithm. It divides the list into two
parts: the sorted part at the beginning and the unsorted part at the end.
The algorithm repeatedly selects the smallest (or largest, depending on sorting order)
element from the unsorted part and swaps it with the first element of the unsorted part,
effectively growing the sorted part by one element with each iteration.
Example:
Consider the list [4, 2, 7, 1].
First pass:
Find the minimum element in [4, 2, 7, 1], which is 1
Swap 1 with 4 to get [1, 2, 7, 4]
Second pass:
Find the minimum element in [2, 7, 4], which is 2
Swap 2 with 2 to get [1, 2, 7, 4] (no change)
Third pass:
Find the minimum element in [7, 4], which is 4
Swap 4 with 7 to get [1, 2, 4, 7]
Fourth pass:
The last element 7 is already in place, no need to swap
Final sorted list: [1, 2, 4, 7].
Insertion Sort
Insertion Sort is a simple comparison-based sorting algorithm that builds the final sorted
list one element at a time. It works similarly to the way you might sort playing cards in your
hands.
The algorithm takes one element from the unsorted portion of the list and inserts it into its
correct position in the sorted portion. This process is repeated until the entire list is sorted.
Example:
Consider the list [4, 2, 7, 1].
First pass (i = 1):
Key is 2. Compare 2 with 4.
2 is less than 4, so move 4 to the right and insert 2 at the beginning to get [2, 4, 7, 1].