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]);
}
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
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]);
}
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.
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.
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.
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.
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.
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.
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