DS Unit 1
DS Unit 1
Example:
1. Our Name is a Data Structures which comprises of First Name,
Middle Name and Last Name
2. The Date is a Data Structures DD-MM-YYYY
3. IFSC code of Bank
4. Passport Number
Factors that are needed in the selection of Data
Structures
11
Fundamental Concepts:
Algorithm:
A Logical Module design
Handle specific problem
Relative to particular data structure
Properties Of An Effective Algorithm:
Algorithm should be
Concise and Compact
Effective Memory Utilization
Less Time Consuming
Clear and Definite
Efficient
12
Data Structures
13
Applications Of Data Structures
Operating System
Compiler Design
Database Management System
Expert Systems
Network Analysis
14
Dynamic aspects of operations on data
15
Dynamic aspects of operations on data
16
Dynamic aspects of operations on data
Static Data
Aspect Dynamic Data Structure
Structure
Memory is
Memory allocation allocated at Memory is allocated at run-time
compile-time
Size is fixed and
Size cannot be modified Size can be modified during runtime
Memory utilization Memory utilization is efficient as
Memory utilization
may be inefficient memory can be reused
Access time is Access time may be slower due to
Access
faster as it is fixed indexing and pointer usage
Arrays, Stacks,
Lists, Trees (with variable size), Hash
Examples Queues, Trees
tables 17
(with fixed size)
Characteristics of Data
Volume:
The amount of data.
Impacts storage and processing requirements.
Velocity:
The speed at which data is generated and processed.
High velocity data requires real-time processing capabilities.
Variety:
The different types and sources of data.
Includes structured, semi-structured, and unstructured data.
Veracity:
The quality and accuracy of data.
Value:
The usefulness of the data for decision-making.
Data should be relevant and actionable.
18
Creation, Manipulation and Operations on data
• Traversing
• Insertion
• Deletion
• Search
• Sorting
• Merging
Traversing
Every data structure contains the set of data elements.
Traversing operation on Data Structure means visiting each element of the
data structure.
After traversing the data structure we can perform a specific task on those
data elements.
For Example If we need to calculate the average of the marks obtained by
a student in 6 different subject, we need to traverse the complete array of
marks and calculate the total sum, then we will divide that sum by the
number of subjects i.e. 6, in order to find the average.
19
Creation, Manipulation and Operations on data
Insertion
•Insertion operation in Data Structure means represents the
adding new elements to the existing data structure at a specific
location.
•If the size of data structure is n then we can only insert (n-1)
data elements into it.
Deletion
•The process of removing an element from the existing data
structure is called Deletion.
•We can delete an element from the data structure at any
random location.
•If we try to delete an element from an empty data structure
then underflow occurs.
20
Creation, Manipulation and Operations on data
Searching
The process of finding the location of an existing element within a data
structure is called Searching.
Linear Search and Binary Search are two most popular searching
algorithms.
Sorting
The process of arranging the data structure elements in a specific order
either descending or ascending is known as Sorting.
There are many algorithms that can be used to perform sorting, for
example, insertion sort, selection sort, bubble sort, etc.
Merging
When two lists List A and List B of size M and N respectively, of similar
type of elements, clubbed or joined to produce the third list, List C of
size (M+N), then this process is called merging.
21
Operations Of Data Structures
1.Traversal
2.Search
3.Insertion
4.Deletion
5.Sorting
6.Merging
22
Classification of Data Structures
23
Classification of Data Structures
24
Classification of Data Structures
Definition Of ADT:
An Abstract Data Type (ADT) is defined as a mathematical
model with a collection of operations defined on that model.
Set of integers, together with the operations of union,
intersection and set difference form a example of an ADT.
An ADT consists of data together with functions that operate
on that data.
An ADT is a set of objects together with a set of operations.
An ADT is an extension of Modular Design
Example:
27
Why ADT?
Modularity
Divide program into small functions
Easy to debug and maintain
Easy to modify
Group work
Reuse
Do some operations onlyonce
Easy to changethe implementation
28
Advantages/Benefits of ADT:
1.Modularity
2.Reuse
3.Code is easier to understand
4.Implementation of ADTs can be changed
without requiring changes to the program
that uses the ADTs.
29
Advantages of Modularity
30
The ListADT
The List is an
Ordered sequence of data items
A1,A2, A3, …,AN isa list of sizeN
Sizeof an empty list is 0
Ai+1 succeeds Ai
Ai-1 preceeds Ai
Position ofAi is i
First elementisA1 called “head”
Last element isAN called “tail”
31
Various operations performed on List
32
List– AnExample
33
Implementation of List ADT
Array Implementation
34
Array
Arrays are defined as the collection of similar types of data items
stored at contiguous memory locations.
It is one of the simplest data structures where each data element
can be randomly accessed by using its index number.
Array is a static data structure i.e., the memory should be
allocated in advance and the size is fixed. This will waste the
memory space when used space is less than the allocated space.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical
index, which is used to identify the element.
35
Need for Array
37
Declaration of Array
int arr[5];
char arr[10];
float arr[20];
38
Initialization of Array
40
Types of Array
41
Basic Operations in Arrays
42
Print ListOperation
43
Traversal operation
The array elements are traversed using this operation. It sequentially
prints each element of the array.
#include <stdio.h>
void main()
{
int Arr[5] = {18, 30, 15, 70, 12};
int i;
printf("Elements of the array are:\n");
for(i = 0; i<5; i++) {
printf("%d”, Arr[i]);
}
}
44
Insertion operation
45
Insertion operation
#include <stdio.h> // Shift the elements after the
void main() insertion position to the right
{ for (i = size - 1; i >= pos-1; i--)
int a[50],i,size, pos, item; {
printf("Enter the size of the array: "); a[i+1] = a[i];
scanf("%d", &size); }
printf("Enter the elements of the // Insert the new element at the
array:"); desired position
for ( i = 0; i < size; i++) a[pos-1] = item;
{
scanf("%d", &a[i]); size++;
}
printf("Elements of the array before printf(" Elements of the array after
insertion:\n"); insertion :");
for(i = 0; i<5; i++) { for (i = 0; i < size; i++)
printf("%d”, a[i]); {
} printf("%d", a[i]);
printf("Enter the position and value }
of the new element to be inserted: "); getch(); 46
scanf("%d%d", &pos, &item); }
Insertion operation
47
Deletion operation
include <stdio.h> for (i = position-1; i < size-1; i++)
#include <conio.h> {
void main() a[i] = a[i+1];
{ }
int a[100], position, i, size; printf("Final array is");
49
Operations Of List
1.IsEmpty(LIST)
2.IsFull(LIST)
3.Insert Element to End of the LIST.
4.Delete Element from End of the LIST.
5.Insert Element to front of the LIST.
6.Delete Element from front of the LIST.
7.Insert Element to nth Position of the LIST.
8.Delete Element from nth Position of the LIST.
9.Search Element in the LIST.
10.Print the Elements in the LIST.
50
Advantage and Disadvantage Of Array
Implementation
Advantages of array implementation:
The elements are faster to access using random access
Searching an element is easier
Limitation of array implementation
An array store its nodes in consecutive memory locations.
The number of elements in the array is fixed and it is not possible to change the
number of elements .
Insertion and deletion operation in array are expensive. Since insertion is
performed by pushing the entire array one position down and deletion is
performed by shifting the entire array one position up.
Applications of arrays
Arrays are particularly used in programs that require storing large collection of
similar type data elements.
51
Linked List Implementation
DATA NEXT
ELEMENT POINTER
NODE
Consist of series of nodes.
Each node contains Element and pointer
Pointer points or links to the successor node
Pointer of last node points to NULL.
700 1000 800
10 20 30 40
500 700 1000 800
52
Type Of Linked List
53
Singly Linked List
Definition:
Each Node contains data field and only one
link field that link is pointing to the successor node.
700 1000 800
23 13 45 9
struct Node;
int IsEmpty(List L);
int IsLast(List L);
position Find(int X,List L);
position FindPrevious(int X,List L);
position FindNext(int X,List L);
void insert(int X,List L,Position P);
void Delete(int X,List L);
void DeleteList(List L);
55
Creating a Node:
struct Node
{
int element;
struct node* Next;
};
DATA NEXT
56
Routine to check whether the list is
empty or not:
int IsEmpty(List L)
{
if(L->Next = = NULL)
return (1);
}
int IsEmpty(List L)
{
if(L->Next = = NULL)
{ return (1); Header L 10
}
return (0);
57
}
Routine to check whether the current
position is last or not:
58
Find Routine
FIND (10)
L 10 20 25
P
Routine To Insert An Element In The List
L 10 20
61
AFTER DELETION
ROUTINE TO DELETE THE LIST
void DeleteList(List L)
{
position P, Temp;
P = L -> Next;
L -> Next = NULL;
while (P! = NULL)
{
Temp = P -> Next;
free (P);
P = Temp;
}
}
62
Header
L 10 20 L -> Next = NULL
L P
20 free (P)
P = Temp
P
Temp,P = NULL
63
Advantage and Disadvantage Of SLL
Advantages of SLL:
1.The elements can be accessed using the next pointer
2.Occupies less memory than DLL as it has only one
next field.
Disadvantages of SLL:
1.Traversal in the backwards is not possible
2. Less efficient for insertion and deletion
64
Doubly Linked List
DATA
BLINK FLINK
ELEMENT
65
STRUCTURE DECLARATION
struct Node
{ DATA
BLINK FLINK
int Element; ELEMENT
struct Node *FLINK;
struct Node *BLINK;
};
Header
L
10 20 30 40
66
ROUTINE TO INSERT AN ELEMENT IN A
DOUBLY LINKED LIST
void Insert(int X, List L, Position P)
{
struct Node *Newnode;
Newnode = malloc(sizeof(struct Node));
if (Newnode ! = NULL)
{
Newnode -> Element = X;
Newnode -> Flink = P -> Flink;
P -> Flink -> Blink = Newnode;
P -> Flink = Newnode;
Newnode -> Blink = P;
}
}
Header
L
10 20 30
P
15 67
Newnode
ROUTINE TO DELETE AN ELEMENT
Advantage of DLL:
Deletion operation is easier
Finding the predecessor and successor of a node is
easier
Disadvantage of DLL:
More memory space is required since it has two
pointers
69
Circular Linked List
Circular Linked List
In circular linked list the pointer of the last node points to the first
node. Circular linked list can be implemented as Singly linked list and Doubly
linked list with or without headers.
Header
L
10 20 30
70
Circular Linked List
10 20 30
Polynomial ADT
Radix Sort
Multilist
72
THE STACK ADT
Stack Model:
A Stack is a linear data structure which follows Last In First Out
(LIFO) principle, in which both insertion and deletion occur at only one end of
the list called the Top.
Top C
B
A
Stack Model
Example :-
Pile of coins, a stack of trays in cafeteria.
73
Operations On Stack
> Pop
Push
Operations on Stack
74
PUSH
Definition:
The process of inserting a new element to the top of the
stack .
For every push operation the top is incremented by 1.
Top=1 20
Top=0 10 10
Top=-1
Empty stack After inserting an After inserting an
element 10 element 20
Push Operations
75
POP
Definition:
The process of deleting an element from the top of stack is called pop
operation.
After every pop operation the top pointer is decremented by 1.
Top 8
Top
17 17
25 Top 25
25
Pop Operations
76
EXCEPTIONAL CONDITIONS
Over Flow
Attempt to insert an element when the stack is full
is said to be overflow.
Under Flow
Attempt to delete an element , when the stack is
empty is said to be underflow.
77
Stack Size Explanation
Array Size =3
(Top=2) 7
6
5 5
(Top=0)
(Top=-1) Empty Stack Topsize=Arraysize-1
78
Implementation On Stack
1. Array Implementation
2. Linked List Implementation
79
Array Implementation of Stack
Each Stack is associated with top value or pointer.
Empty stack top value is -1.
To Push an new X element onto the stack, Top pointer
is incremented and then set Stack[Top]=X.
To Pop an element, the stack[Top] is returned and the
top pointer is decremented.
Pop on empty stacks or push on full stack will exceed
the array bound.
Top 6
5
80
ROUTINE TO PUSH AN ELEMENT ONTO A STACK
81
ROUTINE TO POP AN ELEMENT FROM THE STACK
void pop(Stack S, int X)
{
if (Top = = -1)
printf(“Empty Stack”);
else
{
X = S[Top];
Top = Top – 1;
}
}
Top 7
Top
6 6
Top 5 5
(size=-1) Empty Stack
82
ROUTINE TO RETURN TOP ELEMENT OF THE
STACK
Top 7
5
Top
83
Routine to check whether the stack is full
or not
int IsFull(Stack S)
{ else
if(Top==Arraysize-1) return false;
return true; }
Top 30 Arraysize=3
20 Top 0=10,Top 1=20,Top 2=30
10
84
Routine to check Whether the stack is
Empty or not
int IsEmpty(Stack S)
{
if(Top = = -1) else
return true; return false;
}
Top
(Size=-1)
85
LINKED LIST IMPLEMENTATION OF STACK
Struct Node;
typedef Struct Node *Stack;
int IsEmpty (Stack S);
Stack CreateStack (void);
void MakeEmpty (Stack S);
void push (int X, Stack S);
int Topelement (Stack S);
void pop (Stack S);
Struct Node
{
int Element;
Struct Node *Next;
};
88
ROUTINE TO CHECK WHETHER THE STACK IS EMPTY
int IsEmpty(Stack S)
{
if (top= = NULL)
return(1);
}
89
ROUTINE TO PUSH AN ELEMENT ONTO A STACK
void push (int X, Stack S)
{
Struct Node *newnode;
newnode = malloc (sizeof(Struct Node));
If (newnode = = NULL)
Error (“Out of Space”);
else
{
newnode -> Element = X;
newnode -> Next = top;
top= newnode;
}
}
90
ROUTINE TO POP FROM A STACK
Void pop(Stack S)
{
Struct Node *Tempcell;
If (IsEmpty(S))
Error(“Empty Stack”);
else
{
Tempcell = top;
top= top-> Next;
Free(Tempcell);
}
}
91
ROUTINE TO RETURN TOP ELEMENT IN A
STACK
int Top(Stack S)
{
If(! IsEmpty(s))
return top-> Element;
Error(“Empty Stack”);
return 0;
}
92
APPLICATION OF STACK
93
Different Types of Notations To Represent
Arithmetic Expression
94
Types Of Notations
INFIX
In Infix notation, the arithmetic operator appears between the two operands to
which it is being applied.
For example:- 1. A+B 2.A/B+C
POSTFIX
The arithmetic operator appears directly after the two operands to which it
applies. Also called reverse polish notation. ((A/B)+C)
For example:- 1.AB+ 2.AB/C+
PREFIX
The arithmetic operator is placed before the two operands to which is applies.
Also called as polish notation. ((A/B)+C)
For example:- 1.+AB 2. +/ABC
95
1.Evaluating Arithmetic Expression
Read the infix expression one character at a time until it encounters the delimiter.
96
2.BALANCING THE SYMBOLS
10
0
THE QUEUE ADT
Queue Model
A Queue is a linear data structure which follows First In First Out (FIFO)
principle, in which insertion is performed at rear end and deletion is performed at front
end.
Dequeue (Q) Enqueue (Q)
QUEUE
FRONT REAR
10
1
Operations on Queue
Enqueue :
The process of inserting an element to the queue is
known as enqueue.
Dequeue :
The process of deleting an element from the queue
is known as dequeue.
10
2
Exception Conditions
Overflow :
Attempts to insert an element, when the queue is full is said to
be overflow.
Underflow :
Attempts to delete an element from the queue, when the queue
is empty is said to be underflow.
10
3
Implementation of Queue
10
4
ROUTINE TO ENQUEUE
10
5
ROUTINE TO DEQUEUE
void dequeue()
{ 0 1 2
if (Front < 0)
printf(“Queue Underflow”); 10 27
else
{ F R
X = Queue[Front];
if (Front == Rear) 27
{
Front = 0; F,R
Rear = -1;
}
else
Front = Front + 1;
}
}
10
6
Illustration for array implementation of
queue
-1 0 1 2 3 4 0 1 2 3 4
10 15 15
0 1 2 3 4 0 1 2 3 4
R F DEQUEUE (Q) 10
7
Types of Queue
1. DEQUE
2. Priority Queue
3. Circular Queue
Double Ended Queue (DEQUE)
Definition:
In DEQUE, both insertion and deletion are performed in
both ends.
Insertion Insertion
Deletion 10 20 30 40 50 60 70 80 Deletion
Applications:
– CPU scheduling
– Graph algorithms like Dijkstra’s shortest path, Prim’s
and minimum spanning tree, etc,.
CIRCULAR QUEUE
Advantages
• It overcomes the problem of unutilized space in linear queue, when it is
implemented as array.
Insertion in Circular Queue
FRONT
Q[5] Q[0]
Q[5] Q[0]
- -
10
Q[4] - - Q[1]
Q[4] 20 Q[1]
- -
REAR FRONT
30
Q[3] Q[2] Q[5] Q[0] Q[3] Q[2]
60 10
REAR
Q[4] 50 20 Q[1]
40 30
Q[3] Q[2] 11
4
ASYMPTOTIC NOTATIONS
1.Big oh (O)notation
2.Big omega (Ω) notation
3.Theta(Θ) notation
Big oh (O)notation
• Asymptotic “less than”(slower rate).
• This notation mainly represent upper bound of algorithm run time.
• Big oh (O)notation is useful to calculate maximum amount of time of
execution.
• By using Big-oh notation we have to calculate worst case time complexity.
Average bound
Ꝋ-Theta notation
void add()
{
poly *ptr1, *ptr2, *newnode;
ptr1 = list1;
ptr2 = list2;
while (ptr1! = NULL && ptr2! = NULL)
{
newnode = malloc(sizeof(Struct poly));
if (ptr1 -> power == ptr2 -> power)
{
newnode -> coeff = ptr1 -> coeff + ptr2 ->coeff;
newnode -> power = ptr1 -> power;
newnode -> next = NULL;
list3 = create(list3,newnode);
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
else 13
{ 0
if (ptr1 -> power > ptr2 -> power)
{
newnode -> coeff = ptr1 - > coeff;
newnode -> power = ptr1-> power;
newnode -> next = NULL;
list3 = create(list3,newnode);
ptr1 = ptr1 -> next;
}
else
{
newnode -> coeff = ptr2 -> coeff;
newnode -> power = ptr2 -> power;
newnode -> next = NULL;
list3 = create(list3,newnode);
ptr2 = ptr2 -> next;
}
}
} 13
1
MULTILIST
E1 E2 E3
P1
P2
13
2
Application of Linked List
Input:
1st number = 5x + 4x + 2x
2 1 0