Unit-1 Notes - Data Structure
Unit-1 Notes - Data Structure
By
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
OUTLIN
E
✔ What is Data Structure?
✔ Classification
✔ ADT
What is Data
Data: Structures?
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
It basically describes:
a) Representation of data.
b) Operations that can be performed on these
data
without any implementation.
Abstract Data
Ex- Stack AbstractTypes
Data Type-
a) Stack follows the LIFO concepts.
b) Operations to be performed are :
i) PUSH(data) insert an element at the top
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
INDEX: Classification of Data
Structures
Data Structures
E1 E2 E3 ………En
✔ Except the first and last element, every element is
connected with its predecessor and successor.
Dequeue Enqueue
Left Child
Right Child
Leaf Node
Classification of Data
TreeStructures
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
GraphStructures
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(BCS-301)
Address Calculation in 1D & 2D
Array
By
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Arrays Data
Structure
✔ An array is collection of same type of elements which
stored contiguously in memory. So it is Linear
structure. Data
Index 0 1 2 3
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]
= 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
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.
Data Structures
Address Calculation in 2D & 3D
Array
By
Priti Chouddhary
Assistant Professor
Department of Computer Science & Engineering
ABES Engineering College, Ghaziabad
Two Dimensional
✔ A two Dimensional array is collection of same type of
Array
elements which are represented in the form on rows and
columns.
✔ It is also called matrix.
Declaration Syntax:
Data Type Arrayname[ R] [C]
R Number of rows. C Number of columns
Ex- Int A[2][3]
Memory Representation: a) Row-major Representation
b) Column-major Representation
Two Dimensional
Array
b) Column-major
Representation:
A[0][0] A[0][1] A[0][2]
A[1][0] A[1][1] A[1][2]
Priti Choudhary
Assistant Professor
Department of Information Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Single Linked
List-Traversing
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Single Linked
List-Searching
DATA LINK
Single Linked
List-Searching
DATA LINK
SEARCHING_SLL(ITEM, NEXT,
START)
Step-1 [First check List is empty or
not?] IF ( START== NULL) THEN
DISPLAY “LIST is Empty”
Goto Step
4 END of IF
Single Linked
List-Searching
Step-2 [Traverse the list]
PTR=START
Step-3 Repeat Steps (a ) and (b) WHILE (PTR
!=NULL)
(a) Check Item is present in List or
not? IF(PTR INFO == ITEM)
Goto step 5 //end of IF
THEN
(b) PTR = PTR NEXT // End of Loop
Display “ITEM is present in List”
Single Linked
Step-4 [IFList-Searching
ITEM is not present in the
List] IF (PTR==NULL) THEN
STAR
T
Single Linked List-DELETE FIRST
Case-2NODE
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=STARTand 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=STARTand 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 1IF Position is >1 i.e. Pos=3
STAR
T
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
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)
Step-4 END.
Data Structure(KCS-301)
Priti Choudhary
Assistant Professor
Ajay Kumar Garg Engineering College, Ghaziabad
Circular Linked
List-INTRODUCTION
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
Case-1 When List is empty.
NODE
LAST
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
Data Structure(KCS-301)
By
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Circular Linked List—DELETE FIRST
NODE
Case-1 When there is only one node in the list.
Circular Linked List—DELETE FIRST
NODE
Case-2 When there are more than one node in the
List.
Circular Linked List—DELETE FIRST
NODE
DELETE_F_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 ITEM= PTR INFO
DISPLAY ITEM
Circular Linked List—DELETE FIRST
NODE
Step-3 [Check, IF LIST has only one
node?] IF (PTR=LAST) THEN
LAST=NULL
ELSE
LAST NEXT = PTR NEXT // END of IF-ELSE
Step-4 [Free the memory, allocated to deleted node.]
FREE(PTR)
Step-5 END.
Circular Linked List—DELETE FIRST
NODE(Pseudo Code)
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=NULLand 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)
Circular Linked List:DELETE LAST NODE(Pseudo
ALGORITHM
Code) 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
Item=Q InfocSTART=P Time Complexity: O(N)
Free(Q)
Traversal of circular Linked List requires O(N)
Return Item
END;
Time.
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
Data Structure(KCS-301)
Traverse & Insert Operation in Doubly
Linked List
By
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Doubly Linked List-
Introduction
node
Doubly Linked List-
Introduction
Advantages:
✔ We can traverse in both directions i.e. forward as
well as backward.
✔ From current node, previous node canalso
be accessed using backward pointer field.
Disadvantage:
✔ More memory is needed to implement as compare to
single linked list.
Doubly Linked
List-Traversing
By
Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Doubly Linked List-Delete First
node
Case-1 When there is only one
node in the list.
Doubly Linked List-Delete First
node
Case-2 When there are more than one node in the
list.
Doubly Linked List-Delete Last
node
Case-1 When there is only
one node in the list.
Doubly Linked List-Delete Last
node
Case-2 When there are more than one node in the
list.