DSA - Data Structure and Algorithms
DSA - Data Structure and Algorithms
What is DS?
A data structure is basically a
group of data elements that
are put together under one
name, and which defines a
particular way of storing and
organizing data in a
computer so that it can be
used efficiently.
Introduction to data structures
BASIC TERMINOLOGY
Our aim has been to design good programs, where a good program is defined
as a program that runs correctly
• is easy to read and understand
• is easy to debug and
• is easy to modify.
A program should undoubtedly give correct results, but along with that it
should also run efficiently.
A program is said to be efficient when it executes in minimum time and with
minimum memory space.
In order to write efficient programs we need to apply certain data
management concepts.
The concept of data management is a complex task that includes activities
like data collection, organization of data into appropriate structures.
Data structure is a crucial part of data management.
Data structures are used in almost every program .
Some common examples of data structures
arrays, linked lists, queues, stacks, binary trees, and hash tables.
Data structures are widely applied in the following areas:
Compiler design , Operating system ,Statistical analysis package ,
DBMS
• The primary goal of a program or software is not to perform
calculations or operations but to store and retrieve information as
fast as possible.
• A solution is said to be efficient if it solves the problem within the
required resource constraints like the total space available to store
the data and the time allowed to perform each subtask. And the
best solution is the one that requires fewer resources than known
alternatives.
• Moreover, the cost of a solution is the amount of resources it
consumes. The cost of a solution is basically measured in terms of
one key resource such as time, with the implied assumption that
the solution meets the other resource constraints.
When selecting a data structure to solve a problem, the following
steps must be performed.
1. Analysis of the problem to determine the basic operations that
must be supported. For example, basic operation may include
inserting/deleting/searching a data item from the data structure.
2. Quantify the resource constraints for each operation.
3. Select the data structure that best meets these requirements.
Note: While one type of data structure may permit adding of new
data items only at the beginning, the other may allow it to be
added at any position.
While one data structure may allow accessing data items
sequentially, the other may allow random access of data. So,
selection of an appropriate data structure for the problem is a
crucial decision and may have a major impact on the
performance of the program.
Elementary Data Structure Organization
DS –building block of a program
• A program built using improper data structures may not work as
expected.
• So as a programmer it is mandatory to choose most appropriate
data structures for a program.
DATA - a value or set of values.
It specifies either the value of a variable or a constant
(e.g., marks of students, name of an employee, address of a customer,
value of pi, etc.).
Data item – No subordinate data items is categorized as an
elementary item
Data item - one or more subordinate data items is called a group
item.
EG, a student’s name may be divided into three sub-items—first
name, middle name, and last name—but his roll number would
normally be treated as a single item.
Record - collection of data items.
EG, the name, address, course, and marks obtained are individual data
items. But all these data items can be grouped together to form a record.
FILE is a collection of related records.
EG, if there are 60 students in a class, then there are 60 records of the
students.
All these related records are stored in a file.
A record in a file may consist of multiple data items but the value of a
certain data item uniquely identifies the record in the file. Such a data
item K is called a primary key, and the values K1 , K2 ... in such field are
called keys or key values.
EG, in a student’s record that contains roll number, name, address, course,
and marks obtained, the field roll number is a primary key. Rest of the
fields (name, address, course, and marks) cannot serve as primary keys,
since two or more students may have the same name, or may have the
same address (as they might be staying at the same place), or may be
enrolled in the same course, or have obtained same marks.
Classification of Data Structures
• Primitive data structures are the fundamental data types which are
supported by a programming language.
Some basic data types are integer, real, and boolean. The
terms ‘data type’, ‘basic data type’, and ‘primitive data type’
are often used interchangeably.
• Non-primitive data structures are those data structures which are
created using primitive data structures.
Examples : linked lists, stacks, trees, and graphs.
• Non-primitive data structures can further be classified into two
categories: linear and non-linear data structures.
Non - primitive Data Structures
• If the elements of a data structure are stored in a linear or
sequential order, then it is a linear data structure.
Examples are arrays, linked lists, stacks, and queues.
• If the elements of a data structure are not stored in a sequential
order, then it is a non-linear data structure.
Examples are trees and graphs.
CLASSIFICATION OF DATA STRUCTURES
Arrays
• An array is a collection of similar data elements.
• The elements of an array are stored in consecutive memory
locations and are referenced by an index (also known as the
subscript).
• Arrays are declared using the following syntax:
type name[size];
1st element 2nd 3rd 4th 5th element 6th 7th 8th element 9th 10th
element element element element element element element
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9 ]
Linked Lists
• A linked list is a very flexible dynamic data structure in which
elements can be added to or deleted from anywhere.
• In a linked list, each element (called a node) is allocated space as it is
added to the list.
• Every node in the list points to the next node in the list. Therefore, in
a linked list every node contains two types of information:
The data stored in the node
A pointer or link to the next node in the list
1 2 3 4 5 6 7 X
Eg: Linked List
Stack
• A stack is a last-in, first-out (LIFO) data structure in which insertion
and deletion of elements are done only at one end, known as TOP
of the stack.
• Every stack has a variable TOP associated with it, which is used to
store the address of the topmost element of the stack.
• If TOP = NULL, then it indicates that the stack is empty.
• If TOP = MAX-1, then the stack is full.
0 1 2 3 TOP = 4 5 6 7 8 9
Examples of stack
Queue
• A queue is a FIFO (first-in, first-out) data structure in which the
element that is inserted first is the first one to be taken out.
• The elements in a queue are added at one end called the REAR
and removed from the other one end called FRONT.
• When REAR = MAX – 1, then the queue is full.
• If FRONT = NULL and Rear = NULL, this means there is no element
in the queue.
9 7 18 14 36 45
0 FRONT = 1 2 3 4 5 REAR = 6 7 8 9
Eg: Queue
Tree
• A tree is a non-linear data structure which consists of a collection
of nodes arranged in a hierarchical order.
• One of the nodes is designated as the root node, and the
remaining nodes can be partitioned into disjoint sets such that
each set is the sub-tree of the root.
ROOT NODE
• When two nodes are connected via an edge, the two nodes are
D B C
known as neighbors.
A E
Road map eg of graph
OPERATIONS ON DATA STRUCTURES
• Traversing It means to access each data item exactly once so that it can be
processed. For example, to print the names of all the students in a class.
• Searching It is used to find the location of one or more data items that satisfy the
given constraint. Such a data item may or may not be present in the given
collection of data items. For example, to find the names of all the students who
secured 100 marks in mathematics.
• Inserting It is used to add new data items to the given list of data items. For
example, to add the details of a new student who has recently joined the course.
• Deleting It means to remove (delete) a particular data item from the given
collection of data items. For example, to delete the name of a student who has
left the course.
• Sorting Data items can be arranged in some order like ascending order or
descending order depending on the type of application. For example, arranging
the names of students in a class in an alphabetical order, or calculating the top
three winners by arranging the participants’ scores in descending order and then
extracting the top three.
• Merging Lists of two sorted data items can be combined to form a single list of
sorted data items.
ABSTRACT DATA TYPE
• Example, stacks and queues are perfect examples of an ADT.
• We can implement both these ADTs using an array or a linked list.
• This demonstrates the ‘abstract’ nature of stacks and queues.
• To further understand the meaning of an abstract data type, we will break the term into
‘data type’ and ‘abstract’, and then discuss their meanings.
• Data types in C include int, char, float, and double.
• In other words, the operations that can be performed on a data type are an inseparable
part of its identity.
• Therefore, when we declare a variable of an abstract data type (e.g., stack or a queue), we
also need to specify the operations that can be performed on it.
• Abstract : The word ‘abstract’ in the context of data structures means considered apart
from the detailed specifications or implementation.
What is ADT?
An abstract data type can be a structure considered without regard to its implementation. It
stores the data and perform various operations.
• The end-user is not concerned about the details of how the methods carry out their tasks.
• They are only aware of the methods that are available to them and are only concerned
about calling those methods and getting the results
EG of ADT
• For example, when we use a stack or a
queue, the user is concerned only with the
type of data and the operations that can
be performed on it.
• Therefore, the fundamentals of how the
data is stored should be invisible to the
user.
• They should not be concerned with how
the methods work or what structures are
being used to store the data.
• They should just know that to work with
stacks, they have push() and pop()
functions available to them. Using these
functions, they can manipulate the data
(insertion or deletion) stored in the stack.
• Eg of ADT – CAR
• Put the Key , turn the key , Shift gear