SlideShare a Scribd company logo
1 of 66
DSA - Introduction
Module2
6 September 2022 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
6 September 2022 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
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
6 September 2022 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.
6 September 2022 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
6 September 2022 7
Data Structure Classification
โ€ข Primitive / Non-primitive
โ€“ Basic Data Structures available(defined by programming languages like int , float etcโ€ฆ) / Derived
from Primitive Data Structures(Array, structure, union, link list, stacks, queue etcโ€ฆ)
โ€ข 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
โ€ข A linear data structure has data elements connected to each other so that elements
are arranged in a sequential manner and each element is connected to the element in
front of it and behind it.
There are four types of linear data structures: Array,Linked list,Stack,Queue.
โ€ข The Non-Linear DS is a form of data structure where the data elements don't stay
arranged linearly or sequentially. Since the data structure is non-linear, it does not
involve a single level. Therefore, a user can't traverse all of its elements in a single run.
โ€ข Example : Trees, Graph
6 September 2022 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)
6 September 2022 9
ADT(Abstract Data type) - 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
6 September 2022 10
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.
6 September 2022 11
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.
6 September 2022 12
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.
6 September 2022 13
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
6 September 2022 14
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
6 September 2022 15
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โ€.
6 September 2022 16
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โ€
6 September 2022 17
Operations on Lists
โ€ข MakeEmpty
โ€ข PrintList
โ€ข Find
โ€ข Find k th
โ€ข Insert
โ€ข Delete
โ€ข Next
โ€ข Previous
6 September 2022 18
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
6 September 2022 19
List - Implementation
โ€ข Lists can be implemented using:
โ€“ Arrays
โ€“ Linked List
โ€“ Cursor [Linked List using Arrays]
6 September 2022 20
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.
6 September 2022 21
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)
6 September 2022 22
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
6 September 2022 23
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};
6 September 2022 24
Array Output Function
void display(int array[],int num_values)
{
for (int i = 0; i<num_values; i++)
printf( โ€œ%dโ€, array[i]);
}
6 September 2022 25
List Implemented Using Array
6 September 2022 26
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
6 September 2022 27
Insertion into an Ordered List
6 September 2022 Anna University, Chennai - 600 025 28
Insertion in Detail
6 September 2022 Anna University, Chennai - 600 025 29
Insertion
6 September 2022 Anna University, Chennai - 600 025 30
Deletion
6 September 2022 Anna University, Chennai - 600 025 31
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.
6 September 2022 Anna University, Chennai - 600 025 32
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 returns -1.
6 September 2022 Anna University, Chennai - 600 025 33
Linear Search
6 September 2022 Anna University, Chennai - 600 025 34
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;
}
6 September 2022 Anna University, Chennai - 600 025 35
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)
6 September 2022 Anna University, Chennai - 600 025 36
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]);
}
6 September 2022 Anna University, Chennai - 600 025 37
6 September 2022 Anna University, Chennai - 600 025 38
Implementing Deletion
6 September 2022 Anna University, Chennai - 600 025 39
Deletion - Another Method
6 September 2022 Anna University, Chennai - 600 025 40
PrintList O(N)
Find
Insert O(N) (on avarage half
Delete needs to be moved)
FindKth
Next O(1)
Previous
Operations Running Times
6 September 2022 Anna University, Chennai - 600 025 41
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
6 September 2022 Anna University, Chennai - 600 025 42
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!
6 September 2022 Anna University, Chennai - 600 025 43
Singly Linked List
6 September 2022 Anna University, Chennai - 600 025 44
Singly Linked List
6 September 2022 Anna University, Chennai - 600 025 45
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.
6 September 2022 Anna University, Chennai - 600 025 46
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.
6 September 2022 Anna University, Chennai - 600 025 47
Add first
โ€ข In this case, new node is inserted right before the
current head node.
6 September 2022 Anna University, Chennai - 600 025 48
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.
6 September 2022 Anna University, Chennai - 600 025 49
Add First - Step 2
โ€“ Update head link to point to the new node.
6 September 2022 Anna University, Chennai - 600 025 50
6 September 2022 Anna University, Chennai - 600 025 51
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.
6 September 2022 Anna University, Chennai - 600 025 52
6 September 2022 Anna University, Chennai - 600 025 53
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.
6 September 2022 Anna University, Chennai - 600 025 54
6 September 2022 Anna University, Chennai - 600 025 55
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).
6 September 2022 Anna University, Chennai - 600 025 56
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.
6 September 2022 Anna University, Chennai - 600 025 57
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.
6 September 2022 Anna University, Chennai - 600 025 58
6 September 2022 Anna University, Chennai - 600 025 59
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.
6 September 2022 Anna University, Chennai - 600 025 60
6 September 2022 Anna University, Chennai - 600 025 61
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.
6 September 2022 Anna University, Chennai - 600 025 62
6 September 2022 Anna University, Chennai - 600 025 63
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
6 September 2022 Anna University, Chennai - 600 025 64
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
6 September 2022 Anna University, Chennai - 600 025 65
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
6 September 2022 Anna University, Chennai - 600 025 66
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

More Related Content

Similar to FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_DSA_-_ADT_1_Fall22 (1).pptx

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
OnkarModhave
ย 

Similar to FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_DSA_-_ADT_1_Fall22 (1).pptx (20)

Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
ย 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
ย 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
ย 
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
ย 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
ย 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
ย 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
ย 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
ย 
Unit 1.ppt
Unit 1.pptUnit 1.ppt
Unit 1.ppt
ย 
dsa.pptx
dsa.pptxdsa.pptx
dsa.pptx
ย 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
ย 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
ย 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
ย 
data structure
data structuredata structure
data structure
ย 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
ย 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
ย 
Iare ds lecture_notes_2
Iare ds lecture_notes_2Iare ds lecture_notes_2
Iare ds lecture_notes_2
ย 
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
ย 
Dsa unit 1
Dsa unit 1Dsa unit 1
Dsa unit 1
ย 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
ย 

More from AntareepMajumder

FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...
AntareepMajumder
ย 
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
AntareepMajumder
ย 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
ย 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
AntareepMajumder
ย 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
ย 
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
AntareepMajumder
ย 

More from AntareepMajumder (6)

FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_A...
ย 
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
ย 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
ย 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
ย 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
ย 
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
ย 

Recently uploaded

Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
amitlee9823
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
ย 

Recently uploaded (20)

Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
ย 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
ย 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
ย 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ย 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
ย 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 

FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_DSA_-_ADT_1_Fall22 (1).pptx

  • 2. 6 September 2022 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
  • 3. 6 September 2022 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. 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. 6 September 2022 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.
  • 6. 6 September 2022 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. 6 September 2022 7 Data Structure Classification โ€ข Primitive / Non-primitive โ€“ Basic Data Structures available(defined by programming languages like int , float etcโ€ฆ) / Derived from Primitive Data Structures(Array, structure, union, link list, stacks, queue etcโ€ฆ) โ€ข 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 โ€ข A linear data structure has data elements connected to each other so that elements are arranged in a sequential manner and each element is connected to the element in front of it and behind it. There are four types of linear data structures: Array,Linked list,Stack,Queue. โ€ข The Non-Linear DS is a form of data structure where the data elements don't stay arranged linearly or sequentially. Since the data structure is non-linear, it does not involve a single level. Therefore, a user can't traverse all of its elements in a single run. โ€ข Example : Trees, Graph
  • 8. 6 September 2022 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. 6 September 2022 9 ADT(Abstract Data type) - 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. 6 September 2022 10 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.
  • 11. 6 September 2022 11 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.
  • 12. 6 September 2022 12 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.
  • 13. 6 September 2022 13 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
  • 14. 6 September 2022 14 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
  • 15. 6 September 2022 15 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โ€.
  • 16. 6 September 2022 16 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โ€
  • 17. 6 September 2022 17 Operations on Lists โ€ข MakeEmpty โ€ข PrintList โ€ข Find โ€ข Find k th โ€ข Insert โ€ข Delete โ€ข Next โ€ข Previous
  • 18. 6 September 2022 18 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
  • 19. 6 September 2022 19 List - Implementation โ€ข Lists can be implemented using: โ€“ Arrays โ€“ Linked List โ€“ Cursor [Linked List using Arrays]
  • 20. 6 September 2022 20 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.
  • 21. 6 September 2022 21 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)
  • 22. 6 September 2022 22 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
  • 23. 6 September 2022 23 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};
  • 24. 6 September 2022 24 Array Output Function void display(int array[],int num_values) { for (int i = 0; i<num_values; i++) printf( โ€œ%dโ€, array[i]); }
  • 25. 6 September 2022 25 List Implemented Using Array
  • 26. 6 September 2022 26 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
  • 27. 6 September 2022 27 Insertion into an Ordered List
  • 28. 6 September 2022 Anna University, Chennai - 600 025 28 Insertion in Detail
  • 29. 6 September 2022 Anna University, Chennai - 600 025 29 Insertion
  • 30. 6 September 2022 Anna University, Chennai - 600 025 30 Deletion
  • 31. 6 September 2022 Anna University, Chennai - 600 025 31 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.
  • 32. 6 September 2022 Anna University, Chennai - 600 025 32 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 returns -1.
  • 33. 6 September 2022 Anna University, Chennai - 600 025 33 Linear Search
  • 34. 6 September 2022 Anna University, Chennai - 600 025 34 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; }
  • 35. 6 September 2022 Anna University, Chennai - 600 025 35 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)
  • 36. 6 September 2022 Anna University, Chennai - 600 025 36 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]); }
  • 37. 6 September 2022 Anna University, Chennai - 600 025 37
  • 38. 6 September 2022 Anna University, Chennai - 600 025 38 Implementing Deletion
  • 39. 6 September 2022 Anna University, Chennai - 600 025 39 Deletion - Another Method
  • 40. 6 September 2022 Anna University, Chennai - 600 025 40 PrintList O(N) Find Insert O(N) (on avarage half Delete needs to be moved) FindKth Next O(1) Previous Operations Running Times
  • 41. 6 September 2022 Anna University, Chennai - 600 025 41 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
  • 42. 6 September 2022 Anna University, Chennai - 600 025 42 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!
  • 43. 6 September 2022 Anna University, Chennai - 600 025 43 Singly Linked List
  • 44. 6 September 2022 Anna University, Chennai - 600 025 44 Singly Linked List
  • 45. 6 September 2022 Anna University, Chennai - 600 025 45 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.
  • 46. 6 September 2022 Anna University, Chennai - 600 025 46 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.
  • 47. 6 September 2022 Anna University, Chennai - 600 025 47 Add first โ€ข In this case, new node is inserted right before the current head node.
  • 48. 6 September 2022 Anna University, Chennai - 600 025 48 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.
  • 49. 6 September 2022 Anna University, Chennai - 600 025 49 Add First - Step 2 โ€“ Update head link to point to the new node.
  • 50. 6 September 2022 Anna University, Chennai - 600 025 50
  • 51. 6 September 2022 Anna University, Chennai - 600 025 51 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.
  • 52. 6 September 2022 Anna University, Chennai - 600 025 52
  • 53. 6 September 2022 Anna University, Chennai - 600 025 53 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.
  • 54. 6 September 2022 Anna University, Chennai - 600 025 54
  • 55. 6 September 2022 Anna University, Chennai - 600 025 55 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).
  • 56. 6 September 2022 Anna University, Chennai - 600 025 56 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.
  • 57. 6 September 2022 Anna University, Chennai - 600 025 57 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.
  • 58. 6 September 2022 Anna University, Chennai - 600 025 58
  • 59. 6 September 2022 Anna University, Chennai - 600 025 59 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.
  • 60. 6 September 2022 Anna University, Chennai - 600 025 60
  • 61. 6 September 2022 Anna University, Chennai - 600 025 61 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.
  • 62. 6 September 2022 Anna University, Chennai - 600 025 62
  • 63. 6 September 2022 Anna University, Chennai - 600 025 63 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
  • 64. 6 September 2022 Anna University, Chennai - 600 025 64 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
  • 65. 6 September 2022 Anna University, Chennai - 600 025 65 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
  • 66. 6 September 2022 Anna University, Chennai - 600 025 66 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