Professional Documents
Culture Documents
Introduction
Introduction
Email: rangi.dahanayake@cinec.edu
Unit 19 : Data Structures and Algorithm
LO1 Examine abstract data types, concrete data structures and algorithms
LO2 Specify abstract data types and algorithms in a formal notation
LO3 Implement complex data structures and algorithms
LO4 Present Assess the effectiveness of data structures and algorithms
Chapter 01
Introduction
What is a Data Structure?
• Data structure is a representation of data and the operations allowed
on that data.
• Non-Linear: The data values in this structure are not arranged in order.
➢ Hash tables: Unordered lists which use a ‘hash function’ to insert and search
➢ Tree: Data is organized in branches.
➢ Graph: A more general branching structure, with less strict connection
conditions than for a tree
Selection of Data Structures
• The choice of particular data model depends on two consideration:
➢ The structure should be simple enough that one can effectively process the
data when necessary
Represent Data Structure
• We can represent Data Structures in two ways.:
- Implementation: This is the actual code that creates the data structure in a
specific programming language. It takes the abstract model and translates it
into concrete instructions for the computer to follow. The implementation
details will vary depending on the programming language, the desired
performance characteristics, and other factors.
Abstract Data Type
Imagine you have a toolbox.
The toolbox itself is the ADT: It holds different tools (data) and allows you to
perform specific actions with them (operations).
You don't need to know how the tools are built inside (implementation): You
just know what each tool does and how to use it (abstract model).
For example:
A hammer: Tool for hitting nails (data). You swing it and hit something
(operation).
A screwdriver: Tool for turning screws (data). You twist it in a specific way
(operation).
Abstract Data Type
▪ ADTs are like toolboxes: They organize data and operations without showing how
they're built internally.
▪ Mathematical models: They describe ADTs using rules and logic, similar to a
toolbox blueprint.
▪ Operations: These are like "actions" you can perform on the data, like using the
tools in a toolbox.
▪ Abstraction: It's like focusing on what the ADT does, not how it does it. This
makes it easier to use and understand.
The Core Operations of ADTs
Every Collection ADT should provide a way to:
✓add an item
✓remove an item
ADT = Data + Operations
✓find, retrieve, or access an item
Arrays acts to store related data under a single variable name with an
index.
Ex:
int intArray[];
or int[] intArray;
Example : Arrays
If the array has n elements, the last array index number is n-1
n-1
Example: No of elements(n) = 5
0 1 2 3 4
age 5 6 8 7 9
Example : Arrays
You can define a variable called marks, which
represents not a single value of a marks, but an marks
entire set of grades.
0 14
Each element of the set can then be referenced 1 56
by means of a number called an index
2 44
If you need the marks of the 1st student, it is at: 3 36
marks[0] 4 89
If you need the marks of the 4th student, it is at:
5 43
marks[3]
Index Number
Assigning Values to Array Elements
marks[0] = 14 marks
marks[1] = 56
marks[2] = 44 0 14
marks[3] = 36 1 56
marks[4] = 89 2 44
marks[5] = 43 3 36
4 89
The values may come as an input from user 5 43
or from a file etc..
Index Number
Assigning Values to Array Elements
marks
You can also assign the values at the time you are
declaring the array. 0 14
int marks[6] = {14,56,44,36,89,43} 1 56
2 44
Determined the number of elements automatically 3 36
based on the number of initialization elements 4 89
int marks[] = {14,56,44,36,89,43} 5 43
Index Number
Stack
• Allows access to only one data item; the last item inserted
• If you remove this item, then you can access the next-to-last item
inserted
Stack
In a stack all insertions and deletions are made at
one end (Top). Insertions and deletions are
restricted from the Middle and at the End of a Stack
Top
Adding an item is called Push
Removing an item is called Pop
Push item 49
Pop
Peek
Peek is used to read the value from the top of the stack without removing it. You can peek
only the Top item, all the other items are invisible to the stack user.
Activity 01
Draw the stack frame after performing the below operations to the stack given
below.
25 Top
300
500
Push item 50
Push item 500
Peek
Push item 100 Push
Pop 50
25
Pop
Pop 300
Pop
Queues
In a queue all insertions are made at the Rear end
and deletions are made at the Front end.
Insertions and deletions are restricted from the
Middle of the Queue.
5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0
15 27 39 23 15 27 39 23 45 15 27 39 23
New Element
(Insert)
5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0
15 27 39 23 15 27 39 23 15 27 39
23
5 4 3 2 1 0 5 4 3 2 1 0
15 27 39 23 15 27 39 23
Peek → 23
Peek is used to read the value from the Front of the queue without removing it.
You can peek only the Front item, all the other items are invisible to the queue
user.
Activity 02
Draw the Queue frame after performing the below operations to the queue
given below.
6 5 4 3 2 1 0
24 67 99 47 60
Rear Front
i) Insert item 33
ii) Insert item 53
iii) Peek Front
iv) remove
v) remove
vi) remove
vii) remove
viii) remove
Activity 03
Draw the Queue frame after performing the below operations to the queue
given below.
6 5 4 3 2 1 0
67 12 22 55 34 70 60
Rear Front
i) remove
ii) remove
iii) remove
iv) Insert item 88
What is Circular Queue?
The operations are performed based on FIFO (First In First Out) principle. It is
also called ‘Ring Buffer’.
- Find
Find a link with a specified key value.
- Insert
Insert links anywhere in the list.
- Delete
Delete a link with the specified value.
Linked List
Mainly the following operations can be performed
on a linked list.
- Find
Find a link with a specified key value.
- Insert
Insert links anywhere in the list.
- Delete
Delete a link with the specified value.
Operations - Find
Start with the first item, go to the second link, then the third, until you find what you are looking for.
Step 2 : ‘next’ field of the new link points to the old first link
55
Question 05:
What steps need to be followed if a new link is inserted after the link ‘17’ ?
Operations - Insert
Inserting an item in the middle of the list
55
Operations - Delete
Deleting an item from the beginning of the list
First Value = FirstValue.Next
temp
Step 2 : Disconnect the first link by rerouting first to point to the second link
temp
Question 06:
❖ ALGORITHMS
Step by step procedure which can be applied to data to achieve some goal
❖ PROGRAM
Implements an algorithm
❖ DATA STRUCTURES
The manner in which data is represented in the computer to facilitate access and
manipulate by an algorithm.
What are data structures and algorithms good for?
Programmers' tools
The data structures that are meant for access of the program itself
Stack, Queue…
Real-world modelling
to model real world situations
Graphs, queues…
Algorithm Analysis
Big O notation is a way to describe how the running time of an algorithm grows as
the size of the input grows.
Thank You