0% found this document useful (0 votes)
150 views149 pages

Unit-1 Notes - Data Structure

Uploaded by

anubhavseth56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views149 pages

Unit-1 Notes - Data Structure

Uploaded by

anubhavseth56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 149

Data Structure(BCS-301)

Introduction to Data Structures

By

Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
OUTLIN
E
✔ What is Data Structure?

✔ Why, Data Structure?

✔ Classification

✔ Operations on Data Structures

✔ 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.

Ex- arrays, stack, queue, Tree, Graph etc..


Need of Data
Structures
Organize data has major impact on performance.
✔ To understand it, consider some real life examples such
as
Queue at a ticket counter.
Dictionary.
Queue in
printer.
Arrangement
Program of books in library etc..+ Data Structures
Algorithm
✔Therefore
Programming pointanofappropriate
choosing view Data structures
improves the performance of a program or a
Software.
Classification of Data
Structures
Data Structures

Primitive Data Non-Primitive


Structures Data
int Structures
float
Char Linear Non-Linear
double
Arrays Stack Queue Linked List Tre Graph
e
Classification of Data
Structures
Primitive Data Structures:
✔ These are Pre-defined, built in Data types.
✔ Machine dependent i.e. on 16 bit machine int data type

is 2 bytes in size while on 32bit or 64 bit it is of 4 bytes.

Non-Primitive Data Structures:


✔ User Defined Data types.
✔ Also called Derived data types as they are derived from
Operations on Data
Structures
✔Insertion: Add an element to the Data Structure. i.e.
insert an element as first element or last element or at
any location.
✔ Deletion: Remove an element from Data Structure.
i.e. delete first element, last element or element at
given location.
✔Traversing: Visit the element at least once to perform
some specific operation like find sum, average, count
nodes in list etc.
Operations on Data
Structures
✔Searching : To find whether an element is present or
not in the given data structure.
✔Sorting : It is method to arrange the elements in a
particular order either in ascending or in descending.
✔Merging : It is method which takes two sorted lists
of similar data types as input and produce combined
sorted list as output.
ALGORITHM
•It is a step-by-step procedure to solve a particular problem
statement.
•There are many ways to solve a particular problem, may be more
than one algorithm for a single problem.
•Choose an efficient algorithm among many there is a technique
known as ‘Complexity of Algorithm’.
CRITERIA OF AN
ALGORITHM
• Input
•Output
•Definiteness
•Finiteness
•Effectiveness
COMPLEXITY
OF ALGORITHM
• Time Complexity:-Time complexity of an algorithm is the
amount of time it needs in order to run to completion.
• Space Complexity:-Space Complexity of an algorithm is the
amount of space it needs in order to run to completion.
ASYMPTOTIC
•NOTATION
Mathematical notation that allow us to analyse an algorithm's
running time by identifying its behaviour as the input size for the
algorithm increases.
•This is also known as an algorithm's growth rate.
•Big-Oh Notation (O)
•Big-Theta Notation (Θ)
•Big-Omega Notation (Ω)
Big-Oh Notation (O)
•It provides possibly asymptotically tight upper bound for f(n).

•Let f be a nonnegative function and we say that f(n) is Big-O of


g(n), written as f(n) = O(g(n)), iff there are positive constants c and
n0 such that

0 ≤ f(n) ≤ c g(n) for all n ≥ n0

•If f(n) = O(g(n)), we say that g(n) is an upper bound on f(n).


Big-Theta Notation (Θ)

•We say that f(n) is Big-Theta of g(n), written as


f(n) = Θ(g(n)), iff there are positive constants c1,
c2 and n0 such that

0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)


for all n ≥ n0

•Equivalently, f(n) = Θ(g(n)) if and only if f(n) =


O(g(n)) and f(n) = Ω(g(n)). If f(n) = Θ(g(n)), we
say that g(n) is a tight bound on f(n).
Big-Omega Notation (Ω)

•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

0 ≤ c g(n) ≤ f(n) for all n ≥ n0

• If f(n) = Ω(g(n)), we say that g(n) is a lower bound on f(n).


EXAMPLE OF Θ,Ω,O
Abstract Data
Types Present only the simple view
Abstraction of any object but hide the
implementation details.
Its focuses only on
Ex- TV remote What rather Than
HOW.

To start the TV ,simply press ON button


without knowing its implementation
details.
Abstract Data
ADT represent theTypes
logical or mathematical model for
any data structure.

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

ii) POP() Remove an element from the top

iii) PEEK() view the element which present at the top

iv) is Empty() To check, whether stack is empty or not?


Data Structure(BCS-301)
Classification of Data
Structures
By

Priti Choudhary
Assistant Professor
Department of Information
Technology
Ajay Kumar Garg Engineering College, Ghaziabad
INDEX: Classification of Data
Structures
Data Structures

Primitive Data Non-Primitive


Structures Data
int Structures
float
Char Linear Non-Linear
double
Arrays Stack Queue Linked List Tre Graph
e
Classification of Data
Structures
Primitive Data Structures:
✔ These are Pre-defined, built in Data types.
✔ Machine dependent i.e. on 16 bit machine int data type

is 2 bytes in size while on 32bit or 64 bit it is of 4 bytes.

Non-Primitive Data Structures:


✔ User Defined Data types.
✔ Also called Derived data types as they are derived from
Classification of Data
Structures
Linear Data Structures:
✔ Elements are arranged in logical sequential
manner.

E1 E2 E3 ………En
✔ Except the first and last element, every element is
connected with its predecessor and successor.

✔ Easy to implement. Ex-Arrays, Stack, Queue etc.


Classification of Data
Structures
Arrays Data Structure:
✔ An array is collection of same type of elements which
stored contiguously in memory. So it is Linear
Data
structure.
Ex- int A[4]
✔ Also called Subscripted variable.
✔ Applications are searching, sorting, implement matrix
etc.
Classification of Data
StackStructures
Data Structure:
✔ Stack is a linear Data structure.
✔ Elements are inserted and deleted from one end, call
TOP
of the stack. POP
PUSH
It follows
LIFO/FILO
order
Classification of Data
Structures
Stack Data Structure:
✔ Stack can be implemented using arrays as well as
using linked list
✔ Some real life examples are piles of books, plates
etc.
Applications are
a) Function Call.
b) Infix to Postfix conversion.
c) Evaluation of Postfix expression, In DFS Graph
Algorithm etc..
Classification of Data
It follows
Structures
Queue Data Structure:
FIFO
✔ Queue is a linear data order
structure.are inserted at one end called Rear and
✔ Elements
deleted from other end which is called Front.

Dequeue Enqueue

Front End Rear End


Classification of Data
Structures
Queue Data Structure:
✔ Queue can be implemented using arrays as well as using
linked list
✔ Some real life examples are queue at ticket
counter,at Toll-Plaza
vehicles
etc.
Applications
a)CPU are
scheduling and Disk Scheduling algorithms
operating Systems, Interrupt handling. in
b) Job queue at printer.
c) BFS Graph Algorithm, Prim’s Algorithm
etc.
Classification of Data
Structures
Linked List Data Structure:
✔ Linked list is a linear Data Structures which stored
non-contiguously in memory.
NODE INFO Link

✔ It is collection of nodes where each node has two


fields:
a) INFO field: It actually stores value or set of values.
b) Link Field: It actually stores address to the next
node in the list.
Classification of Data
Structures
Linked List Data Structure:
✔ It provides memory utilization when memory
is not available contiguously.
✔ It is basically implemented using Dynamic
Memory
allocation.
Applications are: Used to implement Stack, Queue, Tree,
Graph.
Polynomial operations, Sparse matrices
representation
Classification of Data
Structures
Non-Linear Data Structures:
✔ Elements are arranged in non- sequential manner.
✔ Each element may be connected to two or more
elements.
✔ Ex- Tree, Graph.
Classification of Data
Tree Data Structure:
✔ It isStructures
a non-linear ,hierarchical data structure.
✔ It follows Parent- Child relationship.
✔ Each node in tree can have more than one children
but
at most one Parent node.
Root Node

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

Ex- int A[4]


✔ Also called Subscripted variable.
✔ Applications are searching, sorting, implement matrix
etc.
One Dimensional
elements.
Array
✔ A one Dimensional array is collection of same type of

✔ It is the simplest form of an array.


Declaration Syntax:
Data Type Arrayname[ Size ]
Ex- int A[4]
Memory Representation
Index 0 1 2 3

. Address 100 104 108 112


One Dimensional
Arrayof first element in an array is called
Base Address: Address
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
Address(A[i]) = Base address + i*esize
array
Here esize is size of the data type of array.
This is the formulato findaddress when array
index
One Dimensional
Q-1) CalculateArray
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

Index 0 1 2 3

Address 100 104 108 112


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 LB
UB
array.
One Dimensional
LB Array UB

No. of elements in array= (UB-LB +1)


Address(A[i]) First find effective index of
element (E) (i- LB) that
Address(A[i]) = BA + (i-LB) x esize
Here esize is size of the data type of
array.
One Dimensional
Array
Ex- Given an array A[-1 : 2]. Here LB=-1 and
UB=2.
Data type of array is int. Base address=100.
Find a) No. of elements in array A.
b) Address of A[1] , A[3] and A[-2].
EXAMPLE-1D
Given A [1:15], bytes per cell = 3,
base address = 1000 find the address of
A [9]. Solution:
Here, i = 9
eSize= 3
Lower Bound(LB) = 1
Upper Bound(UB) = 15
Address of A [i] = Base Address +eSize*(i-LB) Address
of A [9] = 1000 + 3*(9 - 1) = 1024
EXAMPLE-1D
For A[5:55], Base address= 300, eSize=4
(i)Find the number of elements?
(ii) Total memory allocated.
(iii) Find the address of A[15], A[40] and
A[60]?
EXAMPLE-1D
For A[5:55], Base address= 300,
eSize=4 Find the number of elements-
51
address of A[15]-
340 A[40]- 440
A[60] exceeded the size of array hence
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
a)
Array
Row-major Representation:

A[0][0] A[0][1] A[0][2]


A[1][0] A[1][1] A[1][2]

A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2]

Row Index-0 Row Index-1

Therefore 2D array is a collection of 1D arrays which is again a


collection of elements.
Two Dimensional
Ex- Find theArray
address of A[1][2] using row
major
representation.
First find the address of an array having row
Index 1 =100 + 1 x 3 x 4 = 112
Now Find the address of an element
A[1][2]= 112 + 2x 4 =120
Two Dimensional
In generalizedArray
Format the formula to Find Address of
an element A[i1] [i2] using Row Major
First find the address of an array having row
Index i1 = BA + i1 x C x esize
Now Find the address of an element
A[i1][i2] = BA + i1 x C x esize + i2 x esize
Address(A[i1][i2]) = BA + (i1 x C + i2 ) x esize
Two Dimensional
In generalizedArray
Format the formula to Find Address of an
element A[i1] [i2] using Row Major When LB and UB is given
for Rows and Columns.
[LB1 : UB1 : : LB2 : UB2]
LB1 Lower bound of Row. UB1 Upper bound of Row.
LB2 Lower bound of UB2 Upper bound
Column. Column. of
✔ No. of Rows(R) = (UB1- LB1 +1)
✔ No. of Columns(C) = (UB2- LB2 +1)
✔ Total No. of Elements (N) = R x C
EXAMPLE
Q . An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is 1500 determine
the location of X [9][20].

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

Address of A [ i1 ][ i2 ] = BA + eSize * [ ( i1 – LB1 ) + *R ( i2 – LB2 ) ]

= 1500 + 1 * [(9 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [24 + 26 * 5] = 1500 + 1 * [154] = 1654 [Ans]

(ii) Row Major Wise Calculation of above equation


The given values are: BA = 1500, eSize = 1 byte, i1 = 15, i2 = 20, LB1 = -15, LB2 = 15, C = 26

Address of A [ i1 ][ i2 ] = BA + eSize * [ C * ( i1 – LB1 ) + ( i2 – LB2 ) ]

= 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]

A[0][0] A[1][0] A[0][1] A[1][1] A[0][2] A[1][2]

Column Index-0 Column Index-1 Column Index-2

Therefore 2D array is a collection of 1D arrays which is again a


collection of elements.
Two Dimensional
Array
Ex- Find the address of A[1][2] using column major
representation.
First find the address of an array having column
Index 2 =100 + 2 x 2 x 4 = 116
Now Find the address of an element
A[1][2]= 116 + 1x 4 =120
Two Dimensional
In generalizedArray
Format the formula to Find Address of
an element A[i1] [i2] using Row Major
First find the address of an array having column
Index i2 = BA + i2 x R x esize
Now Find the address of an element
A[i1][i2] = BA + i2 x R x esize + i1 x esize
Address(A[i1][i2]) = BA + (i2 x R + i1 ) x esize
Two Dimensional
In generalizedArray
Format the formula to Find Address of an
element A[i1] [i2] using Column Major When LB and UB is
given for Rows and Columns.
[LB1 : UB1 : : LB2 : UB2]
LB1 Lower bound of Row. UB1 Upper bound of Row.
LB2 Lower bound of UB2 Upper bound
Column. Column. of
✔ No. of Rows(R) = (UB1- LB1 +1)
✔ No. of Columns(C) = (UB2- LB2 +1)
✔ Total No. of Elements (N) = R x C
Two Dimensional
Array
Effective Row Index (E1)= (i1- LB1)
Effective Column Index (E2)= (i2-
LB2)
Address(A[i1][i2]) = BA + (E2 x R + E1 ) 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.
Three Dimensional
✔ A three dimensional array is collection of two Dimensional
Array
array which is again a collection of one dimensional array.
✔ It is also called array of arrays of arrays.
✔ Assuming three dimesons are Plane, Rows and Columns.
Declaration Syntax:
Data Type Arrayname[ D1] [D2] [D3]
D1 No. of Planes D2 Number of Rows
D3 Number of Columns A[0][0] A[0][1]
Ex- Int A[2][2][2] A[1][0 ] A[0][0] ] A[0][1]
A[1][1
A[1][0] A[1][1]
Three Dimensional
Array
Memory Representation: a) Row-major Representation
b) Column-major
Representation
a) Row-major Representation
A[0][0] A[0][1]
A[1][0] A[0][0] ] A[0][1]
A[1][1
A[1][0] A[1][1]
Three Dimensional
In generalizedArray
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

find the address of an array having row index i2 in


plane index i1 =
BA + i1 x D2 x D3 x esize + i2 x D3x esize
Three Dimensional
Now Find theArray
address of an element having column index i3 in
row index i2 and plane index i1
A[i1][i2][i3] = BA + i1 x D2 x D3 x esize + i2 x
D3x esize + i3 x esize
Address(A[i1][i2][i3]) = BA +( i1 x D2 x D3 + i2 x D3 + i3 ) x esize
Three Dimensional
In generalizedArray
Format the formula to Find Address of an element
A[i1] [i2] [i3] using Row Major When LB and UB is given for Plane,
Rows and Columns.
[LB1 : UB1 : : LB2 : UB2 : : LB3 : UB3]
LB1 Lower bound of Plane. UB1 Upper bound of Plane.
LB2 Lower bound of Row. UB2 Upper bound of Row.
LB3 Lower bound of Column. UB3 Upper bound of Column.
✔ No. of Planes(D1) = (UB1- LB1 +1)
✔ No. of Rows(D2) = (UB2- LB2 +1)
✔ No. of Columns(D3) = (UB3- LB3 +1)
✔ Total No. of Elements (N) = D1 x D2 X D3
Three Dimensional
Array
Effective Plane Index (E1)= (i1- LB1)
Effective Row Index (E2)= (i2- LB2)
Effective Column Index (E3)= (i3-
Address(A[i1][i2][i3])
LB3) = BA +( E1 x D2 x D3 +
E2 x D3 + E3 ) x esize
Three Dimensional
Ex- ConsiderArray
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.
Data Structure
(KCS-301)
Traverse & Insert Operation in
Single Linked List
By

Priti Choudhary
Assistant Professor
Department of Information Technology
Ajay Kumar Garg Engineering College, Ghaziabad
Single Linked
List-Traversing

TRAVERSE_SLL(START) INFO NEXT


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-Traversing
Step-2 [Traverse the list]
PTR=START
Step-3 Repeat Steps (a ) and (b) WHILE (PTR
!=NULL)
(a) DISPLAY PTR INFO
(b) PTR = PTR Next
Step-4 END
Single Linked List-INSERT new node
as First node
Single Linked List-INSERT new node
as First node
INSERT_F_SLL(START,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
Step-4 [Insert new node as first
node] PTR Next= START
START= PTR
Single Linked List-INSERT new node
as First node(Pseudo Code)
Single Linked List-INSERT node as
Last Node
Case-1 WHEN LIST IS EMPTY
START
NULL
Single Linked List-INSERT node as Last
Node
Case-2 WHEN LIST IS NOT EMPTY
Single Linked List-INSERT node as
Last Node
INSERT_L_SLL(START, 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
Step-4-a) When List is empty
[Insert new node as first node]
PTR Next= START and START=
Single Linked List-INSERT node as
Last Node
Step-4-b) When List is not empty
[Traverse the list until Last node is
reached] TEMP= START
Repeat Step WHILE (TEMP Next != NULL)
TEMP= TEMP Next
Step-5 [Insert new node as last node.]
TEMP Next = PTR
PTR Next = NULL
Step-6 END.
Single Linked List-INSERT node at Given
Position
Case-1 IF Pos=1
START
NULL
Single Linked List-INSERT node at Given
Position
Case-2 IF POS>1
Single Linked List-INSERT node at Given
Position
INSERT_POS_SLL(START,ITEM, POS)
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
Step-4-a) When POS=1[Insert new node as first
node] IF (POS==1) THEN
PTR Next= START and START= PTR
Single Linked List-INSERT node at Given
Position
Step-4-b) When POS >1
ELSE
TEMP= START and I=1
Repeat Step WHILE I < (POS-1) AND (TEMP !=NULL)
TEMP= TEMP Next and I=I+1 // end of loop
Step-5 [Insert new node at given position.]
IF (TEMP != NULL) THEN
PTR Next = TEMP Next
TEMP Next = PTR
Data Structure(KCS-301)
Delete & Search Operation in Single
Linked List
By

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

DISPLAY “ITEM is not present in the


List”
Step-5 END
Single Linked List-DELETE FIRST
NODE
Case-1 When there is only one node in the List.

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)

// END of Outer IF-ELSE

Step-4 END.
Data Structure(KCS-301)

Traverse & Insert Operation in


Circular Linked List
By

Priti Choudhary
Assistant Professor
Ajay Kumar Garg Engineering College, Ghaziabad
Circular Linked
List-INTRODUCTION

✔ A circular linked list is similar to single linked list


except address field of last node contains the
address of first node.
✔ Here LAST pointer contains the address of the
last node.
Circular Linked
List-INTRODUCTION

✔ By taking LAST pointer, insert a node as first or


last node takes constant time i.e. O(1) time.
✔ A queue data structure can be implemented
using a circular linked list with single pointer.
There is no need to use two pointers i.e. Front
and Rear.
Circular Linked
List-Traversing

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

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
Case-1 When List is empty.
NODE
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.]
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
Data Structure(KCS-301)

Delete Operation in Circular Linked List

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

In Doubly Linked List, each node has three fields:


a) Backward Pointer: It contains the address of previous
node.
b) Forward Pointer: It contains the address of next node.
c) INFO Field:It contains the actual
BACKW INFO FORWvalue to be stored.

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

TRAVERSE_DLL(DATA, LINK, START) BACKW DATA FORW


Step-1 [First check List is empty or
not?] IF ( START== NULL) THEN
DISPLAY “LIST is Empty”
Goto Step 4
END of IF
Doubly Linked
List-Traversing
Step-2 [Traverse the list]
PTR=START
Step-3 Repeat Steps (a ) and (b) WHILE (PTR
!=NULL)
(a) DISPLAY PTR INFO
(b) PTR = PTR FORW
Step-4 END
Doubly Linked List-INSERT new node
as First node
Case-1 When list is empty
Doubly Linked List-INSERT new node
as First node
Case-2 When list is not empty
Doubly Linked List-INSERT node as
Last Node
Case-1 WHEN LIST IS EMPTY
START
NULL
Doubly Linked List-INSERT node as
Last Node
Case-2 When list is not empty
Data Structure(KCS-301)
Delete Operation in Doubly Linked List

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.

You might also like