DS Module 1
DS Module 1
Syllabus
INTRODUCTION
Data
• It is a set of values.
• Information means meaningful data and processed data.
Instruction
• Commands given to the computer to perform an operation.
Program
• Set of instruction to perform an operation.
Algorithm
• Algorithm is a step by step procedure or method for solving a problem by a computer in a
finite number of steps.
• Steps of an algorithm may include branching or repetition depending upon what problem
the algorithm is being developed for.
1
SMPC DATA STRUCTURES MODULE 1
Efficiency of an algorithm
Complexity of an algorithm
Big O notation
Definition:
Let f(n) and g(n) be two functions, then f(n)=O(g(n)) or f = O(g) (read “ f of n is big oh of g of n
“ or “ f is big oh of g “) if there is a positive integer C such that f(n)<= C* g(n) for all positive
integers n.
2
SMPC DATA STRUCTURES MODULE 1
➢ Abstract data type is a mathematical model that contain specification of data object and the
operation that can be performed on the object.
Syntax
Abstract datatype
{
Instances;
}
{
Operations;
}
• Class: The building block of C++ that leads to Object Oriented programming is a Class.
• It is a user defined data type, which holds its own data members and member functions,
which can be accessed and used by creating an instance of that class..
Iterators
➢ Examples of data structures- Arrays, Linked lists, Queues, Stacks, binary trees and hash tables.
➢ Linear DS-if the elements of a data structure are stored sequentially, then it is a linear
data structure. In linear data structure, we can traverse either forward or backward from a
node. e.g. arrays, stacks, queues and linked lists.
➢ Nonlinear DS-if the elements of a data structure are not stored in a sequential order,
then it is a nonlinear data structure. It branches to more than one node and cannot be
traversed in a single run. e.g. trees and graphs.
4
SMPC DATA STRUCTURES MODULE 1
4. Queue-it is FIFO DS in which the element that was inserted first is first one to be taken
out. the elements in a queue are added at one end called the rear and removed from the
other end called the front.
5. Trees-it is a collection of elements called the nodes. Every node contains a left pointer,
a right pointer and a data element. Every binary tree has a root element pointed by a
‘root’ pointer.
6. Graphs-it is an abstract data structure that is used to implement the graph concept from
mathematics. It is basically a collection of vertices and edges that connect these
vertices.
Stacks
➢ It is a linear structure which can be implemented by either using an array or a linked list.
➢ The elements in a stack are added and removed only from one end, which is called the top.
➢ So stack is called LIFO data structure-i.e-the element that was inserted last is taken out first.
➢ Applications-
1) Reverse the order of data
2) Convert infix expression into postfix
3) Convert postfix expression into infix
4) Backtracking problem
5) System stack is used in every recursive function
6) Converting a decimal no. into its binary equivalent
Operations on stack
• Stack has 3 basic operations- push, pop and peep
Push operation
✓ It is used to insert an element into the stack
✓ The new element is added at the topmost position of the stack
✓ Before inserting the value, we must first check if top=max-1, i.e is the stack is full or
not. if the stack is already full, an overflow message is printed.
Algorithm-
6
SMPC DATA STRUCTURES MODULE 1
Pop operation
✓ It is used to delete the topmost element from the stack
✓ Before deleting the value, we must first check if top= NULL, i.e is the stack is empty or
not.
✓ If the stack is empty, an underflow message is printed
✓ If the stack is not empty, the value of the top is decrement
Algorithm-
Peep operation
✓ This returns the value of the topmost element of the stack without deleting it from the stack
✓ If top=NULL, then an appropriate message is printed
Algorithm-
Step 1: if top=NULL,then print”stack is empty” go to step 3
Step 2: return stack[top]
Step 3:end
Return 30
7
SMPC DATA STRUCTURES MODULE 1
Stack ADT
Abstract datatype stack
{
Instances
Linear list of elements;
Insertion and deletion takes place at one end called TOP;
}
{
Operations
stackEmpty() : return True if stack is empty, Return False otherwise
Stackfull() : : return True if stack is full, Return False otherwise.
8
SMPC DATA STRUCTURES MODULE 1
➢ In postfix notation (reverse polish notation or RPN)- the operator is placed after the
operands.
E.g. the expression A+B in infix notation can be written as AB+ in postfix notation. The
order of evaluation of a postfix expression is always from left to right, even brackets cannot
alter the order of evaluation.
➢ In prefix notation (polish notation), the operators are placed before the operands. E.g. the
expression A+B in infix notation can be written as +AB in prefix notation.
9
SMPC DATA STRUCTURES MODULE 1
Step 4: repeatedly pop from the stack and add it to the postfix expression until
the stack is empty
Step 5: exit
e.g.Convert the following infix expression into postfix expression using the algorithm.
(a) A – (B / C + (D % E * F) / G)* H
(b) A – (B / C + (D % E * F) / G)* H)
10
SMPC DATA STRUCTURES MODULE 1
➢ An algebraic expression written in infix notation, the computer first converts the
expression into the equivalent postfix notation and then evaluates the postfix
expression.
➢ Every character of the postfix expression is scanned from left to right. If the character
encountered is an operand, it is pushed on to the stack. However, if an operator is
encountered, then the top two values are popped from the stack and the operator is applied
on these values. The result is then pushed on to the stack.
11
SMPC DATA STRUCTURES MODULE 1
Example
12
SMPC DATA STRUCTURES MODULE 1
Queues
➢ It is a data structure which stores its elements in an ordered manner
➢ A queue is a FIFO data structure. i.e. the element that was 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
end called the front
➢ It can be implemented by either using arrays or linked lists
➢ MAX specifies the maximum no. of elements in the queue
➢ The queue is full, if rear=MAX-1 and the queue is empty when front=-1 and rear=-1
Applications:
1. Priority queues
2. Graph operations
3. Tree operations
4. simulations
✓ use queues in time sharing systems in which each process waits in a queue to get
CPU time
✓ use queues in computer networks. Data packets sent through network are put in
a queue
✓ operating systems often maintain a queue of processes
13
SMPC DATA STRUCTURES MODULE 1
Queue
Operations on queue
• A queue has two basic operations- insertion and deletion
• Apart from this , there is another operation peek-this returns the first element of the queue.
Insertion
✓ It adds an element to the rear of the queue.
Algorithm
14
SMPC DATA STRUCTURES MODULE 1
Deletion
✓ It removes the element from the front or the start of the queue.
Algorithm
15
SMPC DATA STRUCTURES MODULE 1
Circular queues
❖ In a linear queue, the overflow condition will occur when rear=MAX-1 and underflow
condition will occur when rear=-1 & front=-1.
• The circular queue will be full only when front=0 and rear=max-1
• For insertion, we have to check the following conditions
✓ If front=0 and rear=MAX-1, then print that circular queue is full
✓ If rear!=MAX-1, then the value will be inserted and rear will be incremented
✓ If front!=0 and rear=MAX-1,then the queue is not full.so set rear=0 and insert
the new element there.
Queue Full
16
SMPC DATA STRUCTURES MODULE 1
Empty Queue
Queue full
Empty Queue
Empty queue
18
SMPC DATA STRUCTURES MODULE 1
19
SMPC DATA STRUCTURES MODULE 1
Dequeues
0 1 2 3 4
Left right
Double-ended queues
20
SMPC DATA STRUCTURES MODULE 1
Priority queue
➢ Priority queues are widely used in operating systems to execute the highest priority
process first.
1. Use a sorted list: to store the elements so that when an element has to be taken out,
the queue will not have to be searched for the element with the highest priority.
2. Use an unsorted list: insertions are always done at the end of the list. Every time
when an element has to be removed from the list, the element with the highest priority
will be searched and removed.
21
SMPC DATA STRUCTURES MODULE 1
➢ FRONT[K] and REAR[K] contain the front and rear values of row K, where K is the
priority number.
➢ The row and column indices start from 1, not 0
22