0% found this document useful (0 votes)
35 views79 pages

Module 2 DSA 24

Uploaded by

pratiknagre34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views79 pages

Module 2 DSA 24

Uploaded by

pratiknagre34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

INTRODUCTION TO DATA STRUCTURE

DEPARTMENT OF AIDS/AIML/CSD/CSE/CSME

FACULTY OF ENGINEERING
Course outcomes
Topic CO
Data types – primitive and non-primitive, 1
Types of Data Structures 2
Linear & Non- Linear Data Structures, 3
Array, List, Stack, Queue, 3
Comparison of Basic Data Structures 3
Advanced Data Structures (Overview) 3
Choosing right data structure 4
SPECIFIC LEARNING OBJECTIVES

S/No Learning objectives Level Criteria Condition


Understand the basic concepts of data At least
1 Must know -
structure one

At least
2 Learn the types of data structures Must know -
two

Understand various operations performed


3 Must know all -
on data structure
Data :

•Data is a collection of numbers, alphabets and symbols combined to represent


information

•A computer takes a raw data as input and after processing of data it produces refined
data as output

•There is a Binary system of representation in the Computer for data

•Usually an integer number is represented by its binary equivalent


What is Data Structure?

⮚ Data Structure is a branch of Computer Science. The study of data structure


allows us to understand the organization of data and the management of the
data flow in order to increase the efficiency of any process or program.
⮚ Data Structure is a particular way of storing and organizing data in the memory
of the computer so that these data can easily be retrieved and efficiently
utilized in the future when required.
⮚ The data can be managed in various ways, like the logical or mathematical
model for a specific organization of data is known as a data structure.
Basic Terminologies related to Data Structures
1.Data: We can define data as an elementary value or a collection of values. For example, the
Employee's name and ID are the data related to the Employee.
2.Data Items: A Single unit of value is known as Data Item.
3.Group Items: Data Items that have subordinate data items are known as Group Items. For example,
an employee's name can have a first, middle, and last name.
4.Elementary Items: Data Items that are unable to divide into sub-items are known as Elementary
Items. For example, the ID of an Employee.
5.Entity and Attribute: A class of certain objects is represented by an Entity. It consists of different
Attributes. Each Attribute symbolizes the specific property of that Entity. For example,
The term "information" is sometimes utilized for data with given attributes
of meaningful or processed data.

1.Field: A single elementary unit of information symbolizing the Attribute of an Entity is


known as Field.

2.Record: A collection of different data items are known as a Record. For example, if we
talk about the employee entity, then its name, id, address, and job title can be grouped
to form the record for the employee.

3.File: A collection of different Records of one entity type is known as a File. For
example, if there are 100 employees, there will be 25 records in the related file
containing data about each employee.
Why should we learn Data Structures?

1.Data Structures and Algorithms are two of the key aspects of Computer Science.
2.Data Structures allow us to organize and store data, whereas Algorithms allow us
to process that data meaningfully.
3.Learning Data Structures and Algorithms will help us become better Programmers.
4.We will be able to write code that is more effective and reliable.
5.We will also be able to solve problems more quickly and efficiently.
Objectives of Data Structures

1.Correctness: Data Structures are designed to operate correctly for all kinds of inputs
based on the domain of interest. In order words, correctness forms the primary objective
of Data Structure, which always depends upon the problems that the Data Structure is
meant to solve.

2.Efficiency: Data Structures also requires to be efficient. It should process the data
quickly without utilizing many computer resources like memory space. In a real-time
state, the efficiency of a data structure is a key factor in determining the success and
failure of the process.
Key Features of Data Structures

1.Robustness: Generally, all computer programmers aim to produce software that yields correct output
for every possible input, along with efficient execution on all hardware platforms. This type of robust
software must manage both valid and invalid inputs.

2.Adaptability: Building software applications like Web Browsers, Word Processors, and Internet Search
Engine include huge software systems that require correct and efficient working or execution for many
years. Moreover, software evolves due to emerging technologies or ever-changing market conditions.

3.Reusability: The features like Reusability and Adaptability go hand in hand. It is known that the
programmer needs many resources to build any software, making it a costly enterprise. However, if the
software is developed in a reusable and adaptable way, then it can be applied in most future applications.
Thus, by executing quality data structures, it is possible to build reusable software, which appears to be
cost-effective and timesaving.
Data Management:
•Data management is the practice of collecting, keeping, and using data securely, efficiently,
and cost-effectively.
•The goal of data management is to help people, organizations, and connected things
optimize the use of data within the bounds of policy and regulation so that they can make
decisions and take actions that maximize the benefit to the organization.
•A robust data management strategy is becoming more important than ever as organizations
increasingly rely on intangible assets to create value.
•The data management process includes a combination of different functions that collectively
aim to make sure that the data in corporate systems is accurate, available and accessible.
Importance of Data management :
•With effective data management, people across an organization can find and access trusted data for their queries.
Some benefits of an effective data management solution include:

Visibility
•Data management can increase the visibility of your organization’s data assets, making it easier for people to
quickly and confidently find the right data for their analysis.
•Data visibility allows your company to be more organized and productive, allowing employees to find the data
they need to better do their jobs.

Reliability
•Data management helps minimize potential errors by establishing processes and policies for usage and building
trust in the data being used to make decisions across your organization.
•With reliable, up-to-date data, companies can respond more efficiently to market changes and customer needs.
Security
•Data management protects your organization and its employees from data losses, thefts, and breaches
with authentication and encryption tools.
•Strong data security ensures that vital company information is backed up and retrievable should the
primary source become unavailable.
•Additionally, security becomes more and more important if your data contains any personally identifiable
information that needs to be carefully managed to comply with consumer protection laws.

Scalability
•Data management allows organizations to effectively scale data and usage occasions with repeatable
processes to keep data and metadata up to date.
•When processes are easy to repeat, your organization can avoid the unnecessary costs of duplication, such
as employees conducting the same research over and over again or re-running costly queries
unnecessarily.
Data Types

Data Types :

•A data type is a classification of data which tells the compiler or interpreter how the programmer intends
to use the data.
•Most programming languages support various types of data, including integer, real, character or string,
and Boolean
•A major part of understanding how to design and code programs is centred in understanding the types
•of data that to be manipulate and how to manipulate that data.
•The common data types usually exist in most programming languages and act or behave similarly from
language to language.
•Additional complex and/or composite data types may exist and vary from language to language.
Primitive Data Structures

⮚ Primitive Data Structures are the data structures consisting of the numbers and the
characters that come in-built into programs.

⮚ These data structures can be manipulated or operated directly by machine-level instructions.

⮚ Basic data types like Integer, Float, Character, and Boolean come under the Primitive Data
Structures.

⮚ These data types are also called Simple data types, as they contain characters that can't be
divided further
Non-Primitive Data Structures
⮚ Non-Primitive Data Structures are those data structures derived from Primitive Data Structures.

⮚ These data structures can't be manipulated or operated directly by machine-level instructions.

⮚ The focus of these data structures is on forming a set of data elements that is
either homogeneous (same data type) or heterogeneous (different data types).

⮚ Based on the structure and arrangement of data, we can divide these data structures into two
sub-categories -
1. Linear Data Structures
2. Non-Linear Data Structures
Linear Data Structures

⮚ A data structure that preserves a linear connection among its data


elements is known as a Linear Data Structure.

⮚ The arrangement of the data is done linearly, where each element


consists of the successors and predecessors except the first and the
last data element.

⮚ However, it is not necessarily true in the case of memory, as the


arrangement may not be sequential.
Based on memory allocation, the Linear Data Structures are further classified into
two types:
Static Data Structures: The data structures having a fixed size are known as Static Data Structures. The
memory for these data structures is allocated at the compiler time, and their size cannot be changed by
the user after being compiled; however, the data stored in them can be altered.
The Array is the best example of the Static Data Structure as they have a fixed size, and its data can be
modified later.

Dynamic Data Structures: The data structures having a dynamic size are known as Dynamic Data
Structures. The memory of these data structures is allocated at the run time, and their size varies during
the run time of the code. Moreover, the user can change the size as well as the data elements stored in
these data structures at the run time of the code.
Linked Lists, Stacks, and Queues are common examples of dynamic data structures
Types of Linear Data Structures
1. Arrays
⮚ An Array is a data structure used to collect multiple data elements of the same data type into one
variable.

⮚ Instead of storing multiple values of the same data types in separate variable names, we could store
all of them together into one variable.

⮚ This statement doesn't imply that we will have to unite all the values of the same data type in any
program into one array of that data type. But there will often be times when some specific variables
of the same data types are all related to one another in a way appropriate for an array.
Types of Arrays

1.One-Dimensional Array: An Array with only one row of data elements is known as a One-
Dimensional Array. It is stored in ascending storage location.

2.Two-Dimensional Array: An Array consisting of multiple rows and columns of data elements is
called a Two-Dimensional Array. It is also known as a Matrix.

3.Multidimensional Array: We can define Multidimensional Array as an Array of Arrays.


Multidimensional Arrays are not bounded to two indices or two dimensions as they can include
as many indices are per the need.
Applications of Array

⮚We can store a list of data elements belonging to the same data type.
⮚Array acts as an auxiliary storage for other data structures.
⮚The array also helps store data elements of a binary tree of the fixed
count.
⮚Array also acts as a storage of matrices.
Operations on Array

• Array Traversal:
Array traversal refers to the process of visiting, checking, and/or updating each element in an array. This
is a fundamental operation in programming and is often used for tasks such as searching for an
element, modifying elements, and performing calculations on array elements.

arr = [1, 2, 3, 4, 5]
for i in range(len(arr)):
print(arr[i])
• Insertion in Array:

• Insertion in an array involves adding a new element at a specific position, shifting subsequent
elements to the right to make room. Depending on the position where the new element is to be
inserted, the process can vary slightly. Below are some common scenarios for inserting an element
into an array:

• Insertion at the End


• Insertion at the Beginning
• Insertion at a Specific Position
Insertion at the End
Insertion at the Beginning
Insertion at a Specific Position
Deletion in Array

• Deletion in an array involves removing an element from a specific position and


shifting the subsequent elements to fill the gap. Depending on the position of the
element to be deleted, the process can vary slightly. Here are some common
scenarios for deleting an element from an array:

• Deletion at the End


• Deletion at the Beginning
• Deletion at a Specific Position
1. Deletion from the End
Deletion from the Beginning
Deletion from a Specific Position
Searching in Array:

• Searching in an array involves finding the position of a specific element or verifying its presence. There
are two primary search techniques: linear search and binary search.
1.Linear Search: Linear search is the simplest search algorithm. It checks each element of the array
sequentially until the target element is found or the end of the array is reached. This method works on
both sorted and unsorted arrays.
2.Binary search is a more efficient algorithm but requires the array to be sorted. It works by repeatedly
dividing the search interval in half. If the target value is less than the middle element, the search
continues in the lower half, otherwise in the upper half.
2. Linked Lists
⮚ A Linked List is another example of a linear data structure used to store a collection of data elements
dynamically.

⮚ Data elements in this data structure are represented by the Nodes, connected using links or pointers.

⮚ Each node contains two fields, the information field consists of the actual data, and the pointer field
consists of the address of the subsequent nodes in the list.

⮚ The pointer of the last node of the linked list consists of a null pointer, as it points to nothing. Unlike
the Arrays, the user can dynamically adjust the size of a Linked List as per the requirements.
Linked Lists

• A linked list is a fundamental data structure in computer science. It consists of nodes


where each node contains data and a reference (link) to the next node in the
sequence. This allows for dynamic memory allocation and efficient insertion and
deletion operations compared to arrays.
comparison of Linked List vs Arrays

Linked List:
•Data Structure: Non-contiguous
•Memory Allocation: Dynamic
•Insertion/Deletion: Efficient
•Access: Sequential

Array:
•Data Structure: Contiguous
•Memory Allocation: Static
•Insertion/Deletion: Inefficient
•Access: Random
Types of Linked List

• Singly Linked List


• Doubly Linked List
• Circular Linked List
• Circular Doubly Linked List
• Header Linked List
Singly Linked List

• A singly linked list is a linear data structure in which the elements are not
stored in contiguous memory locations and each element is connected only
to its next element using a pointer.
Doubly Linked List

A doubly linked list is a data structure consisting of nodes, where each node contains
three components
•Data or value
•A pointer/reference to the next node in the sequence
•A pointer/reference to the previous node in the sequenceThis bidirectional nature of
doubly linked lists allows for more efficient traversal and manipulation of the list
compared to singly linked lists.
Circular Linked List

• The circular linked list is a linked list where all nodes are
connected to form a circle. In a circular linked list, the first
node and the last node are connected to each other which
forms a circle. There is no NULL at the end.
Circular Doubly Linked List

• A circular doubly linked list is defined as a circular linked list in which


each node has two links connecting it to the previous node and the next
node.
Applications of Circular Doubly Linked List

• Implementation of Circular Data Structures: Circular doubly linked lists are extremely helpful in the construction of
circular data structures like circular queues and circular buffers, which are both circular in nature.
• Implementing Undo-Redo Operations: Text editors and other software programs can use circular doubly linked
lists to implement undo-redo operations.
• Music Player Playlist: Playlists in music players are frequently implemented using circular doubly linked lists. Each
song is kept as a node in the list in this scenario, and the list can be circled to play the songs in the order they are
listed.
• Cache Memory Management: To maintain track of the most recently used cache blocks, circular doubly linked lists
are employed in cache memory management.
Applications of Linked Lists

⮚ The Linked Lists help us implement stacks, queues, binary trees, and graphs of
predefined size.
⮚ We can also implement Operating System's function for dynamic memory
management.
⮚ Linked Lists also allow polynomial implementation for mathematical operations.
⮚ We can use Circular Linked List to implement Operating Systems
3. Stacks
⮚ A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out)
principle that allows operations like insertion and deletion from one end of
the Stack, i.e., Top.

⮚ Stacks can be implemented with the help of contiguous memory, an Array,


and non-contiguous memory, a Linked List.

⮚ Real-life examples of Stacks are piles of books, a deck of cards, piles of


money, and many more.
The primary operations in the Stack are as follows:

1.Push: Operation to insert a new element in the Stack is termed as Push


Operation.

2.Pop: Operation to remove or delete elements from the Stack is termed


as Pop Operation.
Applications of Stacks
⮚ The Stack is used as a Temporary Storage Structure for recursive operations.
⮚ We can manage function calls using Stacks.
⮚ Stacks are also utilized to evaluate the arithmetic expressions in different programming languages.
⮚ Stacks are also helpful in converting infix expressions to postfix expressions.
⮚ Stacks allow us to check the expression's syntax in the programming environment.
⮚ We can match parenthesis using Stacks.
⮚ Stacks can be used to reverse a String.
4. Queues
⮚ A Queue is a linear data structure similar to a Stack with some limitations
on the insertion and deletion of the elements.
⮚ The insertion of an element in a Queue is done at one end, and the removal
is done at another or opposite end.
⮚ Thus, we can conclude that the Queue data structure follows FIFO (First In,
First Out) principle to manipulate the data elements.
⮚ Implementation of Queues can be done using Arrays, Linked Lists, or Stacks.
Some real-life examples of Queues are a line at the ticket counter, an
escalator, a car wash, and many more.
The following are the primary operations of the Queue:

⮚ Enqueue: The insertion or Addition of some data elements to the Queue is called Enqueue. The
element insertion is always done with the help of the rear pointer.

⮚ Dequeue: Deleting or removing data elements from the Queue is termed Dequeue. The deletion of
the element is always done with the help of the front pointer.
Applications of Queues

⮚ Queues are also used in Job Scheduler Operations of Operating Systems, like a keyboard buffer queue
to store the keys pressed by users and a print buffer queue to store the documents printed by the
printer.

⮚ Queues are responsible for CPU scheduling, Job scheduling, and Disk Scheduling.

⮚ Priority Queues are utilized in file-downloading operations in a browser.

⮚ Queues are also used to transfer data between peripheral devices and the CPU.
Non-Linear Data Structures
Non-Linear Data Structures are data structures where the data elements are
not arranged in sequential order. Here, the insertion and removal of data are
not feasible in a linear manner. There exists a hierarchical relationship
between the individual data items.

Types of Non-Linear Data Structures

1. Tree
2. Graph
1. Trees
⮚ A Tree is a Non-Linear Data Structure and a hierarchy containing a collection of
nodes such that each node of the tree stores a value and a list of references to
other nodes (the "children").

⮚ The Tree data structure is a specialized method to arrange and collect data in
the computer to be utilized more effectively. It contains a central node,
structural nodes, and sub-nodes connected via edges. We can also say that the
tree data structure consists of roots, branches, and leaves connected.
Applications of Trees

⮚ Trees implement hierarchical structures in computer systems like directories and file systems.
⮚ Trees are also used to implement the navigation structure of a website.
⮚ Trees are also helpful in decision-making in Gaming applications.
⮚ Trees are responsible for implementing priority queues for priority-based OS scheduling functions.
⮚ Trees are also responsible for parsing expressions and statements in the compilers of different
programming languages.
2. Graphs
⮚ Graph Data Structure is a collection of nodes connected by edges.
⮚ It’s used to represent relationships between different entities.
⮚ Graph algorithms are methods used to manipulate and analyze graphs, solving
various problems like finding the shortest path or detecting cycles.

Graph is composed of a set of


vertices( V ) and a set of
edges( E ). The graph is denoted
by G(V, E).
Components of a Graph

• Vertices: Vertices are the fundamental units of the graph.


Sometimes, vertices are also known as vertex or nodes. Every
node/vertex can be labeled or unlabeled.
• Edges: Edges are drawn or used to connect two nodes of the
graph. It can be ordered pair of nodes in a directed graph. Edges
can connect any two nodes in any possible way. There are no
rules. Sometimes, edges are also known as arcs. Every edge can
be labelled/unlabeled.
Basic Operations on Graphs:

• Insertion of Nodes/Edges in the graph – Insert a node into the graph.


• Deletion of Nodes/Edges in the graph – Delete a node from the graph.
• Searching on Graphs – Search an entity in the graph.
• Traversal of Graphs – Traversing all the nodes in the graph.
Applications of Graph

• Graph data structures can be used to represent the interactions between players on a team, such as
passes, shots, and tackles. Analyzing these interactions can provide insights into team dynamics and
areas for improvement.
• Commonly used to represent social networks, such as networks of friends on social media.
• Graphs can be used to represent the topology of computer networks, such as the connections between
routers and switches.
• Graphs are used to represent the connections between different places in a transportation network, such
as roads and airports.
Basic Operations of Data Structures
⮚ Traversal: Traversing a data structure means accessing each data element exactly once so it can be
administered. For example, traversing is required while printing the names of all the employees in a
department.
⮚ Search: Search is another data structure operation which means to find the location of one or more
data elements that meet certain constraints. Such a data element may or may not be present in the
given set of data elements. For example, we can use the search operation to find the names of all the
employees who have the experience of more than 5 years.
⮚ Insertion: Insertion means inserting or adding new data elements to the collection. For example, we
can use the insertion operation to add the details of a new employee the company has recently
hired.
⮚ Deletion: Deletion means to remove or delete a specific data element from the given list of data
elements. For example, we can use the deleting operation to delete the name of an employee who
has left the job.
⮚ Sorting: Sorting means to arrange the data elements in either Ascending or Descending order depending on
the type of application. For example, we can use the sorting operation to arrange the names of employees in
a department in alphabetical order or estimate the top three performers of the month by arranging the
performance of the employees in descending order and extracting the details of the top three.
⮚ Merge: Merge means to combine data elements of two sorted lists in order to form a single list of sorted data
elements.
⮚ Create: Create is an operation used to reserve memory for the data elements of the program. We can
perform this operation using a declaration statement. The creation of data structure can take place either
during the following:
– Compile-time
– Run-time
For example, the malloc() function is used in C Language to create data structure.
Difference between Primitive & Non-Primitive Data Types
Abstract Data Types

• Data types such as int, float, double, long, etc. are considered to be in-built data types and we can
perform basic operations with them such as addition, subtraction, division, multiplication, etc.
• Now there might be a situation when we need operations for our user-defined data type which
have to be defined. These operations can be defined only as and when we require them.
• In order to simplify the process of solving problems, we can create data structures along with their
operations, and such data structures that are not in-built are known as Abstract Data Type (ADT).
• The definition of ADT only mentions what operations are to be performed but not how these
operations will be implemented.
The process of providing only the essentials and hiding the details is
known as abstraction.
1. List ADT
•The data is generally stored in key sequence in a list which has a head
structure consisting of count and pointers to compare the data in the list.

•get() – Return an element from the list at any given position.


•insert() – Insert an element at any position of the list.
•remove() – Remove the first occurrence of any element from a non-empty list.
•removeAt() – Remove the element at a specified location from a non-empty
list.
•replace() – Replace an element at any position by another element.
•size() – Return the number of elements in the list.
2. Stack ADT
In Stack ADT Implementation instead of data being stored in each node,
the pointer to data is stored.
•push() – Insert an element at one end of the stack called top.
•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() – Return the number of elements in the stack.
•isEmpty() – Return true if the stack is empty, otherwise return false.
•isFull() – Return true if the stack is full, otherwise return false.
3. Queue ADT

Each node contains a void pointer to the data and the link
pointer to the next element in the queue.

•enqueue() – Insert an element at the end of the queue.


•dequeue() – Remove and return the first element of the queue, if
the queue is not empty.
•peek() – Return the element of the queue without removing it, if
the queue is not empty.
•size() – Return the number of elements in the queue.
Summary
• Data Management concepts, Data types – primitive and non-
primitive,
• Types of Data Structures- Linear and Non- Linear Data Structures,
Array, List, Stack, Queue, Comparison of Basic Data Structures,
• Advanced Data Structures (Overview), Choosing right data structure .
References
1. Data structure using C and C++-AM Tanenbaum, Y Langsam& MJ Augustein,Prentice Hall India.
2. Data structures & Program Design in C -Robert Kruse, Bruse Leung,Pearson Education.
Expected Questions

BAQ
1.Define data structure.
2.Enlist types of data structure.
SAQ
3.Illustrate the linear data structure.
4.Illustrate types of arrays.
LAQ
5.Explain operations on array.
6.Explain complexities associated with data structure.
7.Explain types of analysis.

You might also like