Unit-1 Notes - Data Structure
Unit-1 Notes - Data Structure
Unit-1 Notes - Data Structure
By
✓Classification
✓ADT
What is Data Structures?
Data: It can be defined as value or set of values.
Ex- Name of a Person, Id of an employee, salary etc..
Process
Data
Memory Information
Role of Data
Structures
What is Data Structures?
Data Structures: It is a way of organizing or storing
data into the memory so that operations on the data
can be performed efficiently.
•It provides possibly asymptotically tight lower bound for f(n) and it
does not give worst case complexity but can give best case
complexity, f(n) is said to be Big-Omega of g(n), written as f(n) =
Ω(g(n)), iff there are positive constants c and n0 such that
By
Left Child
Right Child
Leaf Node
Classification of Data Structures
Tree Data Structure:
Applications:
a) File system on a computer.
b) Efficient searching using Binary Search Tree.
c) Minimal Spanning Tree implementation.
d) Heap sort using Heap data structure.
e) Used in compilers to show syntax tree.
Classification of Data Structures
Graph Data Structure:
✓It is a non-linear , non-hierarchical data structure.
✓It consist of vertices or nodes and edges which connect
these nodes.
G(V, E) Where V→ Set of vertices and E → Set of edges
✓Each node may be connected to two or more elements.
Classification of Data Structures
Graph Applications:
a) Used to represent Networks like rail network, road
network, telephone network etc.
b) Social Networks such as Facebook, Whatsapp.
c) Google Map, Navigation tools.
d) Path optimization algorithms.
e) World Wide Web network.
f) Resource Allocation Graph in OS.
Data Structure(KCS-301)
Address Calculation in 1D & 2D Array
By
.
One Dimensional Array
Base Address: Address of first element in an array is called
base address.
✓As array elements are stored contiguously so there is no
need to keep track of address of each element of array.
✓We only need to know the base address of an array.
Formula to find address of any element A[i] in 1 D array
Address(A[i]) = Base address + i*esize
➢Here esize is size of the data type of array.
➢This is the formula to find address when array index
starts with 0.
One Dimensional Array
Q-1) Calculate the address of an array elements
a) A[3] b) A[2].
Given Data type of array is int. Base address=100.
Array index starts with 0. Size of each element=4 bytes
Number of elements in array=4
One Dimensional Array
Formula to find address of any element A[i] in 1 D
array- (When LB and UB is given and array index can
be start with any number either –ive or +ive or 0.)
Here LB→ Lower bound of an array i.e. lowest index
or starting index of an array.
UB→ Upper bound of an array i.e. highest index or
last index of an array. LB UB
One Dimensional Array
LB UB
Solution: As you see here the number of rows and columns are not given in the question. So they are calculated
as:
Number or rows say R = (UB1 – LB1) + 1 = [10 – (- 15)] +1 = 26
Number or columns say C = (UB2 – LB2) + 1 = [40 – 15)] +1 = 26
Total Number of elements=RxC=26x26 = 676
(i) Column Major Wise Calculation of above equation
The given values are: BA = 1500, eSize = 1 byte, i1 = 9 i2 = 20, LB1 = -15, LB2 = 15, R = 26
= 1500 + 1 * [(9 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [24 + 26 * 5] = 1500 + 1 * [154] = 1654 [Ans]
The given values are: BA = 1500, eSize = 1 byte, i1 = 15, i2 = 20, LB1 = -15, LB2 = 15, C = 26
= 1500 + 1* [26 * (9 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 24 + 5] = 1500 + 1 * [624+ 5] = 1500 + 629
= 2129 [Ans]
EXAMPLE-2D
Given A[ 2:5 :: -3:11] B[A]= 200, W=2 bytes
R=5-2+1=4
C=11+3=1=15
total number of elements= 60
Total memory allocated= 120 bytes
Address of A[3][0] = 236
and A[1][5]= Not exist
Two Dimensional Array
Effective Row Index (E1)= (i1- LB1)
Effective Column Index (E2)= (i2- LB2)
Address(A[i1][i2]) = BA + ((i1-LB1) x C + (i2-LB2) ) x esize
Ex- Consider the 2D array A[0 :2 : : 0 : 3]. Given BA=100 and
esize =4 bytes.
a) Find Row size and column size.
b) Total number of elements in A.
c) Total memory allocated.
d) Address of A[1][1] and A[-1][1].
Data Structures
Address Calculation in 2D & 3D Array
By
A[0][0] A[0][1]
A[1][0] A[0][0]
A[1][1] A[0][1]
A[1][0] A[1][1]
Three Dimensional Array
In generalized Format the formula to Find Address of
an element A[i1] [i2] [i3]using Row Major
➢First find the address of an array having plane
Index i1 = BA + i1 x D2 x D3 x esize
Address(A[i1][i2][i3]) = BA +( E1 x D2 x D3 +
E2 x D3 + E3 ) x esize
Three Dimensional Array
Ex- Consider the 3D array A[0 :2 : : 0 : 2 : : 0 :2].
Given BA=100 and esize =4 bytes.
a) Find Plane size ,Row size and column size.
b) Total number of elements in A.
c) Total memory allocated.
d) Address of A[1][1][1] and A[-1][1][1] using row major.
THANK YOU
Data Structure
(KCS-301)
Traverse & Insert Operation in Single
Linked List
By
DATA LINK
Single Linked List-Searching
START
Single Linked List-DELETE FIRST NODE
Case-2 When there are more than one node in the List.
Single Linked List-DELETE FIRST NODE
DELETE_F_SLL(START,ITEM)
Step-1 [First check List is empty or Not?]
IF(START==NULL) THEN
DISPLAY “ LIST is empty”
Goto Step-5
END of IF
Step-2 PTR=START and ITEM = PTR→INFO
DISPLAY ITEM
Single Linked List-DELETE FIRST NODE
Step-3 [Delete the first node.]
START= START→NEXT
Step-4 [Free the memory, allocated to deleted node.]
FREE(PTR)
Step-5 END.
Single Linked List-DELETE FIRST NODE(Pseudo code)
ALGORITHM DelBeg(START, item)
BEGIN:
IF START = = NULL THEN
WRITE ("Void Deletion”)
RETURN
ELSE
P= START
Item = P → Info
START = START→ Next
Free(P)
RETURN Item
END;
No of address Adjustment = 1 Complexity of
Operation Time Complexity = O(1)
Space Complexity = O(1)
Single Linked List-DELETE Last Node
Case-1 WHEN LIST has only one
node
START
Single Linked List-DELETE Last Node
Case-2 WHEN LIST has more than
one node
Single Linked List-DELETE Last Node
DELETE_L_SLL(START, ITEM)
Step-1 [First check List is empty or Not?]
IF(START==NULL) THEN
DISPLAY “ LIST is empty”
Goto Step-5
END of IF
Step-2 PTR=START and TPTR=NULL
Single Linked List-DELETE Last Node
Step-3 [Check If there is only one node in the List.]
IF (START→NEXT== NULL) THEN
START=START→NEXT
ELSE //Traverse the List
Repeat Steps (a) and (b)
WHILE (PTR→NEXT != NULL)
a) TPTR=PTR
b) PTR= PTR→NEXT //End of Loop
TPTR→NEXT = NULL //End of IF-ELSE
Single Linked List-DELETE Last Node
Step-4 [DISPLAY the Information of Deleted Node.]
DISPLAY PTR→INFO
Step-5 [Free the memory, allocated to deleted node.]
FREE(PTR)
Step-6 END.
Single Linked List-DELETE Last Node(Pseudo Code)
Single Linked List-DELETE node at Given
Position
IF Position is 1 IF Position is >1 i.e. Pos=3
START
Single Linked List-DELETE node at
Given Position
DELETE_POS_SLL(START, POS)
Step-1 [First check List is empty or Not?]
IF(START==NULL) THEN
DISPLAY “ LIST is empty”
Goto Step-4
END of IF
Step-2 PTR=START
Single Linked List-DELETE node at
Given Position
Step-3 [Check If Position is 1.]
IF (POS==1) THEN
START= PTR→NEXT
DISPLAY PTR→INFO and FREE(PTR) //Goto step 4
ELSE
Set i =1
Repeat Steps
WHILE(( i<POS-1) AND PTR!=NULL)
PTR= PTR→NEXT // End of Loop
Single Linked List-DELETE node at
Given Position
[Check If Position is valid or not?]
IF(PTR ==NULL) THEN
DISPLAY “Position is invalid”
Goto Step 4
ELSE
Set TPTR = PTR→NEXT
PTR→NEXT = TPTR→NEXT
// END of Inner IF-ELSE
Single Linked List-DELETE node at
Given Position
Step-3a [DISPLAY the Information of Deleted Node.]
DISPLAY TPTR→INFO
Step-3b [Free the memory, allocated to deleted node.]
FREE(TPTR)
// END of Outer IF-ELSE
Step-4 END.
THANK YOU
Data Structure(KCS-301)
TRAVERSING_CLL(LAST)
Step-1 [First check List is empty or not?]
IF ( LAST== NULL) THEN
DISPLAY “LIST is Empty”
Goto Step 5
END of IF
Circular Linked List-Traversing
Step-2 [Traverse the list]
PTR=LAST→NEXT
Step-3 Repeat Steps (a ) and (b)
WHILE (PTR→NEXT!=LAST)
(a) DISPLAY PTR→INFO
(b) PTR = PTR→NEXT // End of Loop
Step-4 [PRINT THE ITEM of LAST Node]
DISPLAY PTR→INFO
Step-5 END.
Circular Linked List--INSERT Node as
FIRST NODE
Case-1 When List is empty.
LAST
Case-2 When there are more than one node in the List.
Circular Linked List--INSERT Node as
FIRST NODE
INSERT_F_CLL(LAST,ITEM)
Step-1 First check free memory is available or Not?
Step-2 Allocate new node from the available memory
and assign address into pointer PTR.
Step-3 [Insert ITEM in the DATA field of node.]
PTR→INFO= ITEM
Circular Linked List--INSERT Node as
FIRST NODE
Step-4 [Check, IF LIST is empty or not?]
IF (LAST!=NULL) THEN
PTR→NEXT = LAST→NEXT
LAST→NEXT = PTR
ELSE
PTR→NEXT = PTR
LAST = PTR
// END of IF-ELSE
Step-5 END.
Circular Linked List--INSERT Node as FIRST
NODE(Pseudo Code)
Circular Linked List--INSERT Node as
LAST NODE
Case-1 When List is empty.
LAST
Case-2 When there are more than one node in the List.
Circular Linked List--INSERT Node as
LAST NODE
INSERT_L_CLL(LAST, DATA, LINK, PTR, ITEM)
Step-1 First check free memory is available or Not?
Step-2 Allocate new node from the available memory
and assign address into pointer PTR.
Step-3 [Insert ITEM in the DATA field of node.]
PTR→INFO= ITEM
Circular Linked List--INSERT Node as
LAST NODE
Step-4 [Check, IF LIST is empty or not?]
IF (LAST!=NULL) THEN
PTR→NEXT = LAST→NEXT
LAST→NEXT = PTR
LAST = PTR
ELSE
PTR→NEXT = PTR
LAST = PTR // END of IF-ELSE
Step-5 END.
Circular Linked List--INSERT Node as LAST
NODE(Pseudo Code)
cSTART=P
Circular Linked List--INSERT Node at
Given Position
Case-1 When POS=1 and List is empty.
LAST
Case-2 When Pos=1 and there are more than one node in the List.
Circular Linked List--INSERT Node at
Given Position
Case-3 When POS >1
Circular Linked List--INSERT Node at
Given Position
INSERT_POS_CLL(LAST, DATA, LINK, PTR, ITEM)
Step-1 First check free memory is available or Not?
Step-2 Allocate new node from the available memory
and assign address into pointer PTR.
Step-3 [Insert ITEM in the DATA field of node.]
PTR→INFO= ITEM
Circular Linked List--INSERT Node at
Given Position
Step-4 [Check, IF LIST is empty or not?]
IF(POS==1) THEN
IF (LAST==NULL) THEN
PTR→NEXT = PTR
LAST = PTR
ELSE
PTR→LINK = LAST→LINK
LAST→LINK = PTR // END of IF-ELSE
THANK YOU
Data Structure(KCS-301)
By
P=cSTART→Next
Circular Linked List—DELETE LAST NODE
Case-1 When there is only one node in the list.
Circular Linked List—DELETE LAST NODE
Case-2 When there are more than one node in the List.
Circular Linked List—DELETE LAST NODE
DELETE_L_CLL(LAST)
Step-1 [First check List is empty or Not?]
IF(LAST==NULL) THEN
DISPLAY “ LIST is empty”
Goto Step-5
END of IF
Step-2 PTR=LAST→NEXT and Q=LAST
Circular Linked List—DELETE LAST NODE
Step-3 [Check, IF LIST has only one node?]
IF (PTR==LAST) THEN
LAST=NULL and Goto Step-5
ELSE
Repeat Step WHILE(PTR→NEXT!=LAST)
PTR=PTR→NEXT //end of Loop
Step-4 [Delete Last Node]
PTR→NEXT = LAST→NEXT and LAST=PTR
Step-5 [Free the memory, allocated to deleted node.]
FREE(Q)
Step-6 END.
Circular Linked List:DELETE LAST NODE(Pseudo Code)
ALGORITHM DelEnd(cSTART)
BEGIN:
IF cSTART==NULL THEN
WRITE(“Void Deletion”)
ELSE
P=cSTART→Next
Q=cSTART
IF(P==cSTART) THEN
cSTART=NULL
ELSE
WHILE P→Next!=LAST DO
P=P→NEXT
P→Next=cSTART→Next
cSTART=P
Item=Q→Info Time Complexity: O(N)
Free(Q)
Traversal of circular Linked List requires O(N) Time.
Return Item
END;
Space Complexity:O(1)
Circular Linked List—DELETE NODE at
Given Position
Case-1 When POS=1 and there is only one node in the list.
Case-2 When POS=1 and there are more than one node in the List.
Circular Linked List—DELETE NODE at
Given Position
Case-3 When POS>1
Circular Linked List—DELETE NODE at
Given Position
THANK YOU
Data Structure(KCS-301)
Traverse & Insert Operation in Doubly
Linked List
By
Disadvantage:
✓ More memory is needed to implement as compare to
single linked list.
Doubly Linked List-Traversing
By