SlideShare a Scribd company logo
1 of 83
EE 2204 - Data Structures
and Algorithms
N Radhakrishnan
Assistant Professor
Anna University, Chennai
29 September 2023 Anna University, Chennai - 600 025 2
Topics
 Introduction
 Definitions
 Classification of Data Structures
 Arrays and Linked Lists
 Abstract Data Types [ADT]
• The List ADT
 Array-based Implementation
 Linked List Implementation
 Cursor-based Implementation
 Doubly Linked Lists
29 September 2023 Anna University, Chennai - 600 025 3
Data Structure [Wikipedia]
 Data Structure is a particular way of storing
and organizing data in a computer so that it
can be used efficiently.
 Different kinds of data structures are suited
to different kinds of applications.
 Storing and retrieving can be carried out on
data stored in both main memory and in
secondary memory.
29 September 2023 Anna University, Chennai - 600 025 4
Merriam-Webster's Definition
 Way in which data are stored for efficient
search and retrieval.
 The simplest data structure is the one-
dimensional (linear) array.
 Data items stored non-consecutively in
memory may be linked by pointers.
 Many algorithms have been developed for
storing data efficiently
29 September 2023 Anna University, Chennai - 600 025 5
Algorithms [Wikipedia]
 An algorithm is a step-by-step procedure for
calculations.
 An algorithm is an effective method
expressed as a finite list of well-defined
instructions for calculating a function.
 The transition from one state to the next is
not necessarily deterministic; some
algorithms incorporate random input.
29 September 2023 Anna University, Chennai - 600 025 6
Merriam-Webster's Definition
 Procedure that produces the answer to a
question or the solution to a problem in a
finite number of steps.
 An algorithm that produces a yes or no
answer is called a decision procedure; one
that leads to a solution is a computation
procedure.
 Example: A mathematical formula and the
instructions in a computer program
29 September 2023 Anna University, Chennai - 600 025 7
Data Structure Classification
 Primitive / Non-primitive
• Basic Data Structures available / Derived from
Primitive Data Structures
 Homogeneous / Heterogeneous
• Elements are of the same type / Different types
 Static / Dynamic
• memory is allocated at the time of compilation /
run-time
 Linear / Non-linear
• Maintain a Linear relationship between element
29 September 2023 Anna University, Chennai - 600 025 8
ADT - General Concept
 Problem solving with a computer means
processing data
 To process data, we need to define the data
type and the operation to be performed on
the data
 The definition of the data type and the
definition of the operation to be applied to
the data is part of the idea behind an
Abstract Data Type (ADT)
29 September 2023 Anna University, Chennai - 600 025 9
ADT - General Concept
 The user of an ADT needs only to know that
a set of operations are available for the data
type, but does not need to know how they
are applied
 Several simple ADTs, such as integer, real,
character, pointer and so on, have been
implemented and are available for use in
most languages
29 September 2023 Anna University, Chennai - 600 025 10
Data Types
 A data type is characterized by:
• A set of values
• A data representation, which is common to all
these values, and
• A set of operations, which can be applied
uniformly to all these values
29 September 2023 Anna University, Chennai - 600 025 11
Primitive Data Types
 Languages like ‘C’ provides the following
primitive data types:
• boolean
• char, byte, int
• float, double
 Each primitive type has:
• A set of values
• A data representation
• A set of operations
 These are “set in stone”.
29 September 2023 Anna University, Chennai - 600 025 12
ADT Definition [Wikipedia]
 In computer science, an abstract data type
(ADT) is a mathematical model for a certain
class of data structures that have similar
behavior.
 An abstract data type is defined indirectly,
only by the operations that may be
performed on it and by mathematical
constraints on the effects (and possibly cost)
of those operations.
29 September 2023 Anna University, Chennai - 600 025 13
ADT Definition [Wikipedia]
 An ADT may be implemented by specific data
types or data structures, in many ways and
in many programming languages; or
described in a formal specification language.
 example, an abstract stack could be defined
by three operations:
• push, that inserts some data item onto the
structure,
• pop, that extracts an item from it, and
• peek, that allows data on top of the structure to
be examined without removal.
29 September 2023 Anna University, Chennai - 600 025 14
Definition from techforum4you
 Abstract data types or ADTs are a
mathematical specification of a set of data
and the set of operations that can be
performed on the data.
 They are abstract in the sense that the focus
is on the definitions and the various
operations with their arguments.
 The actual implementation is not defined,
and does not affect the use of the ADT.
29 September 2023 Anna University, Chennai - 600 025 15
ADT in Simple Words
 Definition:
• Is a set of operation
• Mathematical abstraction
• No implementation detail
 Example:
• Lists, sets, graphs, stacks are examples of
ADT along with their operations
29 September 2023 Anna University, Chennai - 600 025 16
Why ADT?
 Modularity
• divide program into small functions
• easy to debug and maintain
• easy to modify
• group work
 Reuse
• do some operations only once
 Easy to change the implementation
• transparent to the program
29 September 2023 Anna University, Chennai - 600 025 17
Implementing an ADT
 To implement an ADT, you need to choose:
• A data representation
 must be able to represent all necessary values of the
ADT
 should be private
• An algorithm for each of the necessary operation:
 must be consistent with the chosen representation
 all auxiliary (helper) operations that are not in the
contract should be private
 Remember: Once other people are using it
• It’s easy to add functionality
29 September 2023 Anna University, Chennai - 600 025 18
The List ADT
 The List is an
• Ordered sequence of data items called
elements
• A1, A2, A3, …,AN is a list of size N
• size of an empty list is 0
• Ai+1 succeeds Ai
• Ai-1 preceeds Ai
• Position of Ai is i
• First element is A1 called “head”
• Last element is AN called “tail”
29 September 2023 Anna University, Chennai - 600 025 19
Operations on Lists
 MakeEmpty
 PrintList
 Find
 FindKth
 Insert
 Delete
 Next
 Previous
29 September 2023 Anna University, Chennai - 600 025 20
List – An Example
 The elements of a list are 34, 12, 52, 16, 12
• Find (52) -> 3
• Insert (20, 4) -> 34, 12, 52, 20, 16, 12
• Delete (52) -> 34, 12, 20, 16, 12
• FindKth (3) -> 20
29 September 2023 Anna University, Chennai - 600 025 21
List - Implementation
 Lists can be implemented using:
• Arrays
• Linked List
• Cursor [Linked List using Arrays]
29 September 2023 Anna University, Chennai - 600 025 22
Arrays
 Array is a static data structure that
represents a collection of fixed number of
homogeneous data items or
 A fixed-size indexed sequence of elements,
all of the same type.
 The individual elements are typically stored
in consecutive memory locations.
 The length of the array is determined when
the array is created, and cannot be changed.
29 September 2023 Anna University, Chennai - 600 025 23
Arrays
 Any component of the array can be
inspected or updated by using its index.
• This is an efficient operation
• O(1) = constant time
 The array indices may be integers (C, Java)
or other discrete data types (Pascal, Ada).
 The lower bound may be zero (C, Java), one
(Fortran), or chosen by the programmer
(Pascal, Ada)
29 September 2023 Anna University, Chennai - 600 025 24
Different Types of Arrays
 One-dimensional array: only one index is
used
 Multi-dimensional array: array involving
more than one index
 Static array: the compiler determines how
memory will be allocated for the array
 Dynamic array: memory allocation takes
place during execution
29 September 2023 Anna University, Chennai - 600 025 25
One Dimensional Static Array
 Syntax:
• ElementType arrayName [CAPACITY];
• ElementType arrayName [CAPACITY] = {
initializer_list };
 Example in C++:
• int b [5];
• int b [5] = {19, 68, 12, 45, 72};
29 September 2023 Anna University, Chennai - 600 025 26
Array Output Function
void display(int array[],int num_values)
{
for (int I = 0; i<num_values; i++)
cout<< array[i] << “ ”;
}
29 September 2023 Anna University, Chennai - 600 025 27
List Implemented Using Array
29 September 2023 Anna University, Chennai - 600 025 28
Operations On Lists
 We’ll consider only few operations
and not all operations on Lists
 Let us consider Insert
 There are two possibilities:
• Ordered List
• Unordered List
29 September 2023 Anna University, Chennai - 600 025 29
Insertion into an Ordered List
29 September 2023 Anna University, Chennai - 600 025 30
Insertion in Detail
29 September 2023 Anna University, Chennai - 600 025 31
Insertion
29 September 2023 Anna University, Chennai - 600 025 32
Deletion
29 September 2023 Anna University, Chennai - 600 025 33
Find / Search
 Searching is the process of looking
for a specific element in an array
 For example, discovering whether a
certain score is included in a list of
scores.
 Searching, like sorting, is a common
task in computer programming.
 There are many algorithms and data
structures devoted to searching.
 The most common one is the linear
search.
29 September 2023 Anna University, Chennai - 600 025 34
Linear Search
 The linear search approach compares
the given value with each element in
the array.
 The method continues to do so until
the given value matches an element in
the list or the list is exhausted
without a match being found.
 If a match is made, the linear search
returns the index of the element in
the array that matches the key.
 If no match is found, the search
29 September 2023 Anna University, Chennai - 600 025 35
Linear Search
29 September 2023 Anna University, Chennai - 600 025 36
Linear Search Function
int LinearSearch (int a[], int n, int key)
{
int i;
for(i=0; i<n; i++)
{
if (a[i] == key)
return i;
}
return -1;
}
29 September 2023 Anna University, Chennai - 600 025 37
Using the Function
 LinearSearch (a,n,item,loc)
 Here "a" is an array of the size n.
 This algorithm finds the location of the
element "item" in the array "a".
 If search item is found, it sets loc to the
index of the element; otherwise, it sets loc
to -1
 index=linearsearch(array, num, key)
29 September 2023 Anna University, Chennai - 600 025 38
PrintList Operation
int myArray [5] = {19,68,12,45,72};
/* To print all the elements of the array
for (int i=0;i<5;i++)
{
printf("%d", myArray[i]);
}
29 September 2023 Anna University, Chennai - 600 025 39
29 September 2023 Anna University, Chennai - 600 025 40
Implementing Deletion
29 September 2023 Anna University, Chennai - 600 025 41
Deletion - Another Method
29 September 2023 Anna University, Chennai - 600 025 42
PrintList O(N)
Find
Insert O(N) (on avarage half
Delete needs to be moved)
FindKth
Next O(1)
Previous
Operations Running Times
29 September 2023 Anna University, Chennai - 600 025 43
Disadvantages of Using Arrays
 Need to define a size for array
• High overestimate (waste of space)
 insertion and deletion is very slow
• need to move elements of the list
 redundant memory space
• it is difficult to estimate the size of array
29 September 2023 Anna University, Chennai - 600 025 44
Linked List
 Series of nodes
• not adjacent in memory
• contain the element and a pointer to a node
containing its succesor
 Avoids the linear cost of insertion and
deletion!
29 September 2023 Anna University, Chennai - 600 025 45
Singly Linked List
29 September 2023 Anna University, Chennai - 600 025 46
Doubly Linked List
29 September 2023 Anna University, Chennai - 600 025 47
Singly Linked List
29 September 2023 Anna University, Chennai - 600 025 48
Singly-linked List - Addition
 Insertion into a singly-linked list has two
special cases.
 It's insertion a new node before the head (to
the very beginning of the list) and after the
tail (to the very end of the list).
 In any other case, new node is inserted in
the middle of the list and so, has a
predecessor and successor in the list.
29 September 2023 Anna University, Chennai - 600 025 49
Empty list case
 When list is empty,
which is indicated by
(head == NULL)
condition, the
insertion is quite
simple.
 Algorithm sets both
head and tail to
point to the new
node.
29 September 2023 Anna University, Chennai - 600 025 50
Add first
 In this case, new node is inserted right
before the current head node.
29 September 2023 Anna University, Chennai - 600 025 51
Add First - Step 1
 It can be done in two steps:
• Update the next link of the new node, to point to
the current head node.
29 September 2023 Anna University, Chennai - 600 025 52
Add First - Step 2
• Update head link to point to the new node.
29 September 2023 Anna University, Chennai - 600 025 53
29 September 2023 Anna University, Chennai - 600 025 54
Add last
 In this case, new node is inserted right after
the current tail node.
 It can be done in two steps:
• Update the next link of the current tail node, to
point to the new node.
• Update tail link to point to the new node.
29 September 2023 Anna University, Chennai - 600 025 55
29 September 2023 Anna University, Chennai - 600 025 56
Insert - General Case
 In general case, new node is always inserted
between two nodes, which are already in the
list. Head and tail links are not updated in
this case.
 We need to know two nodes "Previous" and
"Next", between which we want to insert the
new node.
 This also can be done in two steps:
• Update link of the "previous" node, to point to the new
node.
• Update link of the new node, to point to the "next" node.
29 September 2023 Anna University, Chennai - 600 025 57
29 September 2023 Anna University, Chennai - 600 025 58
Singly-linked List - Deletion
 There are four cases, which can occur while
removing the node.
 We have the same four situations, but the
order of algorithm actions is opposite.
 Notice, that removal algorithm includes the
disposal of the deleted node - unnecessary in
languages with automatic garbage collection
(Java).
29 September 2023 Anna University, Chennai - 600 025 59
List has only one node
 When list has only
one node, that the
head points to the
same node as the
tail, the removal is
quite simple.
 Algorithm disposes
the node, pointed
by head (or tail)
and sets both head
and tail to NULL.
29 September 2023 Anna University, Chennai - 600 025 60
Remove First
 In this case, first node (current head node)
is removed from the list.
 It can be done in two steps:
• Update head link to point to the node, next to the
head.
• Dispose removed node.
29 September 2023 Anna University, Chennai - 600 025 61
29 September 2023 Anna University, Chennai - 600 025 62
Remove Last
 In this case, last node (current tail node) is
removed from the list. This operation is a bit
more tricky, than removing the first node,
because algorithm should find a node, which
is previous to the tail first.
 It can be done in three steps:
• Update tail link to point to the node, before the
tail. In order to find it, list should be traversed
first, beginning from the head.
• Set next link of the new tail to NULL.
• Dispose removed node.
29 September 2023 Anna University, Chennai - 600 025 63
29 September 2023 Anna University, Chennai - 600 025 64
Remove - General Case
 In general case, node to be removed is
always located between two list nodes. Head
and tail links are not updated in this case.
 We need to know two nodes "Previous" and
"Next", of the node which we want to delete.
 Such a removal can be done in two steps:
• Update next link of the previous node, to point to
the next node, relative to the removed node.
• Dispose removed node.
29 September 2023 Anna University, Chennai - 600 025 65
29 September 2023 Anna University, Chennai - 600 025 66
Advantages of Using Linked Lists
 Need to know where the first node is
• the rest of the nodes can be accessed
 No need to move the elements in the list
for insertion and deletion operations
 No memory waste
29 September 2023 Anna University, Chennai - 600 025 67
Cursor Implementation
Problems with linked list implementation:
 Same language do not support pointers!
• Then how can you use linked lists ?
 new and free operations are slow
• Actually not constant time
 SOLUTION: Implement linked list on an array -
called CURSOR
29 September 2023 Anna University, Chennai - 600 025 68
Cursor Implementation - Diagram
29 September 2023 Anna University, Chennai - 600 025 69
Cursor Implementation
If L = 5, then L represents list (A, B, E)
If M = 3, then M represents list (C, D, F)
29 September 2023 Anna University, Chennai - 600 025 70
Arrays - Pros and Cons
 Pros
• Directly supported by C
• Provides random access
 Cons
• Size determined at compile time
• Inserting and deleting elements is
time consuming
29 September 2023 Anna University, Chennai - 600 025 71
Linked Lists - Pros and Cons
 Pros
• Size determined during runtime
• Inserting and deleting elements is
quick
 Cons
• No random access
• User must provide programming
support
29 September 2023 Anna University, Chennai - 600 025 72
Application of Lists
 Lists can be used
 To store the records sequentially
 For creation of stacks and queues
 For polynomial handling
 To maintain the sequence of operations
for do / undo in software
 To keep track of the history of web sites
visited
29 September 2023 Anna University, Chennai - 600 025 73
Why Doubly Linked List ?
 given only the pointer location, we cannot access its
predecessor in the list.
 Another task that is difficult to perform on a linear
linked list is traversing the list in reverse.
 Doubly linked list A linked list in which each node is
linked to both its successor and its predecessor
 In such a case, where we need to access the node
that precedes a given node, a doubly linked list is
useful.
29 September 2023 Anna University, Chennai - 600 025 74
Doubly Linked List
 In a doubly linked list, the nodes are linked
in both directions. Each node of a doubly
linked list contains three parts:
• Info: the data stored in the node
• Next: the pointer to the following node
• Back: the pointer to the preceding node
29 September 2023 Anna University, Chennai - 600 025 75
Operations on Doubly Linked Lists
 The algorithms for the insertion and deletion
operations on a doubly linked list are
somewhat more complicated than the
corresponding operations on a singly linked
list.
 The reason is clear: There are more pointers
to keep track of in a doubly linked list.
29 September 2023 Anna University, Chennai - 600 025 76
Inserting Item
 As an example, consider the Inserting an
item.
 To link the new node, after a given node, in
a singly linked list, we need to change two
pointers:
• newNode->next and
• location->next.
 The same operation on a doubly linked list
requires four pointer changes.
29 September 2023 Anna University, Chennai - 600 025 77
Singly Linked List Insertion
29 September 2023 Anna University, Chennai - 600 025 78
Doubly Linked List Insertion
29 September 2023 Anna University, Chennai - 600 025 79
The Order is Important
29 September 2023 Anna University, Chennai - 600 025 80
Doubly Linked List - Deletion
 One useful feature of a doubly linked list is
its elimination of the need for a pointer to a
node's predecessor to delete the node.
 Through the back member, we can alter the
next member of the preceding node to make
it jump over the unwanted node.
 Then we make the back pointer of the
succeeding node point to the preceding
node.
29 September 2023 Anna University, Chennai - 600 025 81
Doubly Linked List - Deletion
29 September 2023 Anna University, Chennai - 600 025 82
Special Cases of Deletion
 We do, however, have to be careful about
the end cases:
• If location->back is NULL, we are deleting the
first node
• if location->next is NULL, we are deleting the last
node.
• If both location->back and location->next are
NULL, we are deleting the only node.
29 September 2023 Anna University, Chennai - 600 025 83
Interaction

More Related Content

Similar to dsa.ppt

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
data structure
data structuredata structure
data structurehashim102
 
Iare ds lecture_notes_2
Iare ds lecture_notes_2Iare ds lecture_notes_2
Iare ds lecture_notes_2RajSingh734307
 
Introduction To Data Structures.ppt
Introduction To Data Structures.pptIntroduction To Data Structures.ppt
Introduction To Data Structures.pptNALESVPMEngg
 
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.pptLecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.pptiamsallauddin
 
Data Analysis – Technical learnings
Data Analysis – Technical learningsData Analysis – Technical learnings
Data Analysis – Technical learningsInvenkLearn
 
CHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptxCHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptxOnkarModhave
 
Data Structures - Lecture 1 - Unit 1.pptx
Data Structures  - Lecture 1 - Unit 1.pptxData Structures  - Lecture 1 - Unit 1.pptx
Data Structures - Lecture 1 - Unit 1.pptxDanielNesaKumarC
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
Presentation Template__TY_AIML_IE2_Project (1).pptx
Presentation Template__TY_AIML_IE2_Project (1).pptxPresentation Template__TY_AIML_IE2_Project (1).pptx
Presentation Template__TY_AIML_IE2_Project (1).pptxSYETB202RandhirBhosa
 
Lecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptxLecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptxmueedmughal88
 
DSA_Module 1-PPT for engineering students
DSA_Module 1-PPT for engineering studentsDSA_Module 1-PPT for engineering students
DSA_Module 1-PPT for engineering studentsriotsush12
 
Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...
Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...
Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...theijes
 
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...IOSR Journals
 

Similar to dsa.ppt (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
data structure
data structuredata structure
data structure
 
Iare ds lecture_notes_2
Iare ds lecture_notes_2Iare ds lecture_notes_2
Iare ds lecture_notes_2
 
Introduction To Data Structures.ppt
Introduction To Data Structures.pptIntroduction To Data Structures.ppt
Introduction To Data Structures.ppt
 
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.pptLecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
 
Data Analysis – Technical learnings
Data Analysis – Technical learningsData Analysis – Technical learnings
Data Analysis – Technical learnings
 
CHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptxCHAPTER-1- Introduction to data structure.pptx
CHAPTER-1- Introduction to data structure.pptx
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Data Structures - Lecture 1 - Unit 1.pptx
Data Structures  - Lecture 1 - Unit 1.pptxData Structures  - Lecture 1 - Unit 1.pptx
Data Structures - Lecture 1 - Unit 1.pptx
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Presentation Template__TY_AIML_IE2_Project (1).pptx
Presentation Template__TY_AIML_IE2_Project (1).pptxPresentation Template__TY_AIML_IE2_Project (1).pptx
Presentation Template__TY_AIML_IE2_Project (1).pptx
 
Lecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptxLecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptx
 
DSA_Module 1-PPT for engineering students
DSA_Module 1-PPT for engineering studentsDSA_Module 1-PPT for engineering students
DSA_Module 1-PPT for engineering students
 
Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...
Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...
Applying K-Means Clustering Algorithm to Discover Knowledge from Insurance Da...
 
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
 
F04463437
F04463437F04463437
F04463437
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 

More from ansariparveen06

discrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxdiscrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxansariparveen06
 
Introduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptIntroduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptansariparveen06
 
Fundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptFundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptansariparveen06
 
Combinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxCombinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxansariparveen06
 
presentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxpresentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxansariparveen06
 
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxBCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxansariparveen06
 
May14ProcessScheduling.ppt
May14ProcessScheduling.pptMay14ProcessScheduling.ppt
May14ProcessScheduling.pptansariparveen06
 
UNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxUNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxansariparveen06
 
1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptxansariparveen06
 
exception%20handlingcpp.pptx
exception%20handlingcpp.pptxexception%20handlingcpp.pptx
exception%20handlingcpp.pptxansariparveen06
 

More from ansariparveen06 (20)

discrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxdiscrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptx
 
Introduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptIntroduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).ppt
 
Fundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptFundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.ppt
 
pscheduling.ppt
pscheduling.pptpscheduling.ppt
pscheduling.ppt
 
kmap.pptx
kmap.pptxkmap.pptx
kmap.pptx
 
Combinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxCombinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptx
 
presentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxpresentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptx
 
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxBCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
 
11-IOManagement.ppt
11-IOManagement.ppt11-IOManagement.ppt
11-IOManagement.ppt
 
May14ProcessScheduling.ppt
May14ProcessScheduling.pptMay14ProcessScheduling.ppt
May14ProcessScheduling.ppt
 
UNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxUNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptx
 
1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx
 
CHAP4.pptx
CHAP4.pptxCHAP4.pptx
CHAP4.pptx
 
green IT cooling.pptx
green IT cooling.pptxgreen IT cooling.pptx
green IT cooling.pptx
 
06-Deadlocks.ppt
06-Deadlocks.ppt06-Deadlocks.ppt
06-Deadlocks.ppt
 
chp9 green IT.pptx
chp9 green IT.pptxchp9 green IT.pptx
chp9 green IT.pptx
 
regex.ppt
regex.pptregex.ppt
regex.ppt
 
BOM.ppt
BOM.pptBOM.ppt
BOM.ppt
 
Cooling.pptx
Cooling.pptxCooling.pptx
Cooling.pptx
 
exception%20handlingcpp.pptx
exception%20handlingcpp.pptxexception%20handlingcpp.pptx
exception%20handlingcpp.pptx
 

Recently uploaded

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesSHIVANANDaRV
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 

Recently uploaded (20)

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 

dsa.ppt

  • 1. EE 2204 - Data Structures and Algorithms N Radhakrishnan Assistant Professor Anna University, Chennai
  • 2. 29 September 2023 Anna University, Chennai - 600 025 2 Topics  Introduction  Definitions  Classification of Data Structures  Arrays and Linked Lists  Abstract Data Types [ADT] • The List ADT  Array-based Implementation  Linked List Implementation  Cursor-based Implementation  Doubly Linked Lists
  • 3. 29 September 2023 Anna University, Chennai - 600 025 3 Data Structure [Wikipedia]  Data Structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.  Different kinds of data structures are suited to different kinds of applications.  Storing and retrieving can be carried out on data stored in both main memory and in secondary memory.
  • 4. 29 September 2023 Anna University, Chennai - 600 025 4 Merriam-Webster's Definition  Way in which data are stored for efficient search and retrieval.  The simplest data structure is the one- dimensional (linear) array.  Data items stored non-consecutively in memory may be linked by pointers.  Many algorithms have been developed for storing data efficiently
  • 5. 29 September 2023 Anna University, Chennai - 600 025 5 Algorithms [Wikipedia]  An algorithm is a step-by-step procedure for calculations.  An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function.  The transition from one state to the next is not necessarily deterministic; some algorithms incorporate random input.
  • 6. 29 September 2023 Anna University, Chennai - 600 025 6 Merriam-Webster's Definition  Procedure that produces the answer to a question or the solution to a problem in a finite number of steps.  An algorithm that produces a yes or no answer is called a decision procedure; one that leads to a solution is a computation procedure.  Example: A mathematical formula and the instructions in a computer program
  • 7. 29 September 2023 Anna University, Chennai - 600 025 7 Data Structure Classification  Primitive / Non-primitive • Basic Data Structures available / Derived from Primitive Data Structures  Homogeneous / Heterogeneous • Elements are of the same type / Different types  Static / Dynamic • memory is allocated at the time of compilation / run-time  Linear / Non-linear • Maintain a Linear relationship between element
  • 8. 29 September 2023 Anna University, Chennai - 600 025 8 ADT - General Concept  Problem solving with a computer means processing data  To process data, we need to define the data type and the operation to be performed on the data  The definition of the data type and the definition of the operation to be applied to the data is part of the idea behind an Abstract Data Type (ADT)
  • 9. 29 September 2023 Anna University, Chennai - 600 025 9 ADT - General Concept  The user of an ADT needs only to know that a set of operations are available for the data type, but does not need to know how they are applied  Several simple ADTs, such as integer, real, character, pointer and so on, have been implemented and are available for use in most languages
  • 10. 29 September 2023 Anna University, Chennai - 600 025 10 Data Types  A data type is characterized by: • A set of values • A data representation, which is common to all these values, and • A set of operations, which can be applied uniformly to all these values
  • 11. 29 September 2023 Anna University, Chennai - 600 025 11 Primitive Data Types  Languages like ‘C’ provides the following primitive data types: • boolean • char, byte, int • float, double  Each primitive type has: • A set of values • A data representation • A set of operations  These are “set in stone”.
  • 12. 29 September 2023 Anna University, Chennai - 600 025 12 ADT Definition [Wikipedia]  In computer science, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior.  An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.
  • 13. 29 September 2023 Anna University, Chennai - 600 025 13 ADT Definition [Wikipedia]  An ADT may be implemented by specific data types or data structures, in many ways and in many programming languages; or described in a formal specification language.  example, an abstract stack could be defined by three operations: • push, that inserts some data item onto the structure, • pop, that extracts an item from it, and • peek, that allows data on top of the structure to be examined without removal.
  • 14. 29 September 2023 Anna University, Chennai - 600 025 14 Definition from techforum4you  Abstract data types or ADTs are a mathematical specification of a set of data and the set of operations that can be performed on the data.  They are abstract in the sense that the focus is on the definitions and the various operations with their arguments.  The actual implementation is not defined, and does not affect the use of the ADT.
  • 15. 29 September 2023 Anna University, Chennai - 600 025 15 ADT in Simple Words  Definition: • Is a set of operation • Mathematical abstraction • No implementation detail  Example: • Lists, sets, graphs, stacks are examples of ADT along with their operations
  • 16. 29 September 2023 Anna University, Chennai - 600 025 16 Why ADT?  Modularity • divide program into small functions • easy to debug and maintain • easy to modify • group work  Reuse • do some operations only once  Easy to change the implementation • transparent to the program
  • 17. 29 September 2023 Anna University, Chennai - 600 025 17 Implementing an ADT  To implement an ADT, you need to choose: • A data representation  must be able to represent all necessary values of the ADT  should be private • An algorithm for each of the necessary operation:  must be consistent with the chosen representation  all auxiliary (helper) operations that are not in the contract should be private  Remember: Once other people are using it • It’s easy to add functionality
  • 18. 29 September 2023 Anna University, Chennai - 600 025 18 The List ADT  The List is an • Ordered sequence of data items called elements • A1, A2, A3, …,AN is a list of size N • size of an empty list is 0 • Ai+1 succeeds Ai • Ai-1 preceeds Ai • Position of Ai is i • First element is A1 called “head” • Last element is AN called “tail”
  • 19. 29 September 2023 Anna University, Chennai - 600 025 19 Operations on Lists  MakeEmpty  PrintList  Find  FindKth  Insert  Delete  Next  Previous
  • 20. 29 September 2023 Anna University, Chennai - 600 025 20 List – An Example  The elements of a list are 34, 12, 52, 16, 12 • Find (52) -> 3 • Insert (20, 4) -> 34, 12, 52, 20, 16, 12 • Delete (52) -> 34, 12, 20, 16, 12 • FindKth (3) -> 20
  • 21. 29 September 2023 Anna University, Chennai - 600 025 21 List - Implementation  Lists can be implemented using: • Arrays • Linked List • Cursor [Linked List using Arrays]
  • 22. 29 September 2023 Anna University, Chennai - 600 025 22 Arrays  Array is a static data structure that represents a collection of fixed number of homogeneous data items or  A fixed-size indexed sequence of elements, all of the same type.  The individual elements are typically stored in consecutive memory locations.  The length of the array is determined when the array is created, and cannot be changed.
  • 23. 29 September 2023 Anna University, Chennai - 600 025 23 Arrays  Any component of the array can be inspected or updated by using its index. • This is an efficient operation • O(1) = constant time  The array indices may be integers (C, Java) or other discrete data types (Pascal, Ada).  The lower bound may be zero (C, Java), one (Fortran), or chosen by the programmer (Pascal, Ada)
  • 24. 29 September 2023 Anna University, Chennai - 600 025 24 Different Types of Arrays  One-dimensional array: only one index is used  Multi-dimensional array: array involving more than one index  Static array: the compiler determines how memory will be allocated for the array  Dynamic array: memory allocation takes place during execution
  • 25. 29 September 2023 Anna University, Chennai - 600 025 25 One Dimensional Static Array  Syntax: • ElementType arrayName [CAPACITY]; • ElementType arrayName [CAPACITY] = { initializer_list };  Example in C++: • int b [5]; • int b [5] = {19, 68, 12, 45, 72};
  • 26. 29 September 2023 Anna University, Chennai - 600 025 26 Array Output Function void display(int array[],int num_values) { for (int I = 0; i<num_values; i++) cout<< array[i] << “ ”; }
  • 27. 29 September 2023 Anna University, Chennai - 600 025 27 List Implemented Using Array
  • 28. 29 September 2023 Anna University, Chennai - 600 025 28 Operations On Lists  We’ll consider only few operations and not all operations on Lists  Let us consider Insert  There are two possibilities: • Ordered List • Unordered List
  • 29. 29 September 2023 Anna University, Chennai - 600 025 29 Insertion into an Ordered List
  • 30. 29 September 2023 Anna University, Chennai - 600 025 30 Insertion in Detail
  • 31. 29 September 2023 Anna University, Chennai - 600 025 31 Insertion
  • 32. 29 September 2023 Anna University, Chennai - 600 025 32 Deletion
  • 33. 29 September 2023 Anna University, Chennai - 600 025 33 Find / Search  Searching is the process of looking for a specific element in an array  For example, discovering whether a certain score is included in a list of scores.  Searching, like sorting, is a common task in computer programming.  There are many algorithms and data structures devoted to searching.  The most common one is the linear search.
  • 34. 29 September 2023 Anna University, Chennai - 600 025 34 Linear Search  The linear search approach compares the given value with each element in the array.  The method continues to do so until the given value matches an element in the list or the list is exhausted without a match being found.  If a match is made, the linear search returns the index of the element in the array that matches the key.  If no match is found, the search
  • 35. 29 September 2023 Anna University, Chennai - 600 025 35 Linear Search
  • 36. 29 September 2023 Anna University, Chennai - 600 025 36 Linear Search Function int LinearSearch (int a[], int n, int key) { int i; for(i=0; i<n; i++) { if (a[i] == key) return i; } return -1; }
  • 37. 29 September 2023 Anna University, Chennai - 600 025 37 Using the Function  LinearSearch (a,n,item,loc)  Here "a" is an array of the size n.  This algorithm finds the location of the element "item" in the array "a".  If search item is found, it sets loc to the index of the element; otherwise, it sets loc to -1  index=linearsearch(array, num, key)
  • 38. 29 September 2023 Anna University, Chennai - 600 025 38 PrintList Operation int myArray [5] = {19,68,12,45,72}; /* To print all the elements of the array for (int i=0;i<5;i++) { printf("%d", myArray[i]); }
  • 39. 29 September 2023 Anna University, Chennai - 600 025 39
  • 40. 29 September 2023 Anna University, Chennai - 600 025 40 Implementing Deletion
  • 41. 29 September 2023 Anna University, Chennai - 600 025 41 Deletion - Another Method
  • 42. 29 September 2023 Anna University, Chennai - 600 025 42 PrintList O(N) Find Insert O(N) (on avarage half Delete needs to be moved) FindKth Next O(1) Previous Operations Running Times
  • 43. 29 September 2023 Anna University, Chennai - 600 025 43 Disadvantages of Using Arrays  Need to define a size for array • High overestimate (waste of space)  insertion and deletion is very slow • need to move elements of the list  redundant memory space • it is difficult to estimate the size of array
  • 44. 29 September 2023 Anna University, Chennai - 600 025 44 Linked List  Series of nodes • not adjacent in memory • contain the element and a pointer to a node containing its succesor  Avoids the linear cost of insertion and deletion!
  • 45. 29 September 2023 Anna University, Chennai - 600 025 45 Singly Linked List
  • 46. 29 September 2023 Anna University, Chennai - 600 025 46 Doubly Linked List
  • 47. 29 September 2023 Anna University, Chennai - 600 025 47 Singly Linked List
  • 48. 29 September 2023 Anna University, Chennai - 600 025 48 Singly-linked List - Addition  Insertion into a singly-linked list has two special cases.  It's insertion a new node before the head (to the very beginning of the list) and after the tail (to the very end of the list).  In any other case, new node is inserted in the middle of the list and so, has a predecessor and successor in the list.
  • 49. 29 September 2023 Anna University, Chennai - 600 025 49 Empty list case  When list is empty, which is indicated by (head == NULL) condition, the insertion is quite simple.  Algorithm sets both head and tail to point to the new node.
  • 50. 29 September 2023 Anna University, Chennai - 600 025 50 Add first  In this case, new node is inserted right before the current head node.
  • 51. 29 September 2023 Anna University, Chennai - 600 025 51 Add First - Step 1  It can be done in two steps: • Update the next link of the new node, to point to the current head node.
  • 52. 29 September 2023 Anna University, Chennai - 600 025 52 Add First - Step 2 • Update head link to point to the new node.
  • 53. 29 September 2023 Anna University, Chennai - 600 025 53
  • 54. 29 September 2023 Anna University, Chennai - 600 025 54 Add last  In this case, new node is inserted right after the current tail node.  It can be done in two steps: • Update the next link of the current tail node, to point to the new node. • Update tail link to point to the new node.
  • 55. 29 September 2023 Anna University, Chennai - 600 025 55
  • 56. 29 September 2023 Anna University, Chennai - 600 025 56 Insert - General Case  In general case, new node is always inserted between two nodes, which are already in the list. Head and tail links are not updated in this case.  We need to know two nodes "Previous" and "Next", between which we want to insert the new node.  This also can be done in two steps: • Update link of the "previous" node, to point to the new node. • Update link of the new node, to point to the "next" node.
  • 57. 29 September 2023 Anna University, Chennai - 600 025 57
  • 58. 29 September 2023 Anna University, Chennai - 600 025 58 Singly-linked List - Deletion  There are four cases, which can occur while removing the node.  We have the same four situations, but the order of algorithm actions is opposite.  Notice, that removal algorithm includes the disposal of the deleted node - unnecessary in languages with automatic garbage collection (Java).
  • 59. 29 September 2023 Anna University, Chennai - 600 025 59 List has only one node  When list has only one node, that the head points to the same node as the tail, the removal is quite simple.  Algorithm disposes the node, pointed by head (or tail) and sets both head and tail to NULL.
  • 60. 29 September 2023 Anna University, Chennai - 600 025 60 Remove First  In this case, first node (current head node) is removed from the list.  It can be done in two steps: • Update head link to point to the node, next to the head. • Dispose removed node.
  • 61. 29 September 2023 Anna University, Chennai - 600 025 61
  • 62. 29 September 2023 Anna University, Chennai - 600 025 62 Remove Last  In this case, last node (current tail node) is removed from the list. This operation is a bit more tricky, than removing the first node, because algorithm should find a node, which is previous to the tail first.  It can be done in three steps: • Update tail link to point to the node, before the tail. In order to find it, list should be traversed first, beginning from the head. • Set next link of the new tail to NULL. • Dispose removed node.
  • 63. 29 September 2023 Anna University, Chennai - 600 025 63
  • 64. 29 September 2023 Anna University, Chennai - 600 025 64 Remove - General Case  In general case, node to be removed is always located between two list nodes. Head and tail links are not updated in this case.  We need to know two nodes "Previous" and "Next", of the node which we want to delete.  Such a removal can be done in two steps: • Update next link of the previous node, to point to the next node, relative to the removed node. • Dispose removed node.
  • 65. 29 September 2023 Anna University, Chennai - 600 025 65
  • 66. 29 September 2023 Anna University, Chennai - 600 025 66 Advantages of Using Linked Lists  Need to know where the first node is • the rest of the nodes can be accessed  No need to move the elements in the list for insertion and deletion operations  No memory waste
  • 67. 29 September 2023 Anna University, Chennai - 600 025 67 Cursor Implementation Problems with linked list implementation:  Same language do not support pointers! • Then how can you use linked lists ?  new and free operations are slow • Actually not constant time  SOLUTION: Implement linked list on an array - called CURSOR
  • 68. 29 September 2023 Anna University, Chennai - 600 025 68 Cursor Implementation - Diagram
  • 69. 29 September 2023 Anna University, Chennai - 600 025 69 Cursor Implementation If L = 5, then L represents list (A, B, E) If M = 3, then M represents list (C, D, F)
  • 70. 29 September 2023 Anna University, Chennai - 600 025 70 Arrays - Pros and Cons  Pros • Directly supported by C • Provides random access  Cons • Size determined at compile time • Inserting and deleting elements is time consuming
  • 71. 29 September 2023 Anna University, Chennai - 600 025 71 Linked Lists - Pros and Cons  Pros • Size determined during runtime • Inserting and deleting elements is quick  Cons • No random access • User must provide programming support
  • 72. 29 September 2023 Anna University, Chennai - 600 025 72 Application of Lists  Lists can be used  To store the records sequentially  For creation of stacks and queues  For polynomial handling  To maintain the sequence of operations for do / undo in software  To keep track of the history of web sites visited
  • 73. 29 September 2023 Anna University, Chennai - 600 025 73 Why Doubly Linked List ?  given only the pointer location, we cannot access its predecessor in the list.  Another task that is difficult to perform on a linear linked list is traversing the list in reverse.  Doubly linked list A linked list in which each node is linked to both its successor and its predecessor  In such a case, where we need to access the node that precedes a given node, a doubly linked list is useful.
  • 74. 29 September 2023 Anna University, Chennai - 600 025 74 Doubly Linked List  In a doubly linked list, the nodes are linked in both directions. Each node of a doubly linked list contains three parts: • Info: the data stored in the node • Next: the pointer to the following node • Back: the pointer to the preceding node
  • 75. 29 September 2023 Anna University, Chennai - 600 025 75 Operations on Doubly Linked Lists  The algorithms for the insertion and deletion operations on a doubly linked list are somewhat more complicated than the corresponding operations on a singly linked list.  The reason is clear: There are more pointers to keep track of in a doubly linked list.
  • 76. 29 September 2023 Anna University, Chennai - 600 025 76 Inserting Item  As an example, consider the Inserting an item.  To link the new node, after a given node, in a singly linked list, we need to change two pointers: • newNode->next and • location->next.  The same operation on a doubly linked list requires four pointer changes.
  • 77. 29 September 2023 Anna University, Chennai - 600 025 77 Singly Linked List Insertion
  • 78. 29 September 2023 Anna University, Chennai - 600 025 78 Doubly Linked List Insertion
  • 79. 29 September 2023 Anna University, Chennai - 600 025 79 The Order is Important
  • 80. 29 September 2023 Anna University, Chennai - 600 025 80 Doubly Linked List - Deletion  One useful feature of a doubly linked list is its elimination of the need for a pointer to a node's predecessor to delete the node.  Through the back member, we can alter the next member of the preceding node to make it jump over the unwanted node.  Then we make the back pointer of the succeeding node point to the preceding node.
  • 81. 29 September 2023 Anna University, Chennai - 600 025 81 Doubly Linked List - Deletion
  • 82. 29 September 2023 Anna University, Chennai - 600 025 82 Special Cases of Deletion  We do, however, have to be careful about the end cases: • If location->back is NULL, we are deleting the first node • if location->next is NULL, we are deleting the last node. • If both location->back and location->next are NULL, we are deleting the only node.
  • 83. 29 September 2023 Anna University, Chennai - 600 025 83 Interaction