You are on page 1of 43

Introduction

 The tasks of programming and data processing require


efficient algorithms for accessing the data both in main
memory and on secondary storage devices.

 This efficiency is directly linked to the structure of the data


being processed. A data item that can be effectively linked
to other data items takes on meaning that transcends
its individual value.

 A data structure is a way of organizing data that considers


not only the items stored but also their relationship to each other.
What are data structures?
 A data structure is a way of arranging data in a computer's memory or other
disk storage.
 A data structure is a collection of data, organized so that items can be stored
and retrieved by some fixed techniques.
 This is a way of storing data in a computer so that it can be used efficiently.
 Often a carefully chosen data structure will allow a more efficient algorithm
to be used.

2
Why data structures?
 Ultimate goal -- to write efficient programs. In order to do that, one
needs to organize the data in such a way that it can be accessed and manipulated
efficiently.
 Any data structure is designed to organize data to suit a specific purpose
so that it can be accessed and worked in appropriate ways both effectively and
efficiently.

3
Data type
 A data type, in programming, is a classification that specifies

which type of value a variable has and what type of mathematical,

relational or logical operations can be applied to it without

causing an error. A string, for example, is a data type that is used

to classify text and an integer is a data type used to classify whole

numbers.
Examples of data types

 Data types
Integer Whole numbers 7, 12, 999

Float (floating point) Number with a decimal 3.15, 9.06, 00.13


point

Character Encoding text numerically 97 (in ASCII, 97 is a lower


case 'a')

Boolean Representing logical values TRUE, FALSE


Data type cont…

 The data type defines which operations can safely be performed to create,
transform and use the variable in another computation. When a program
language requires a variable to only be used in ways that respect its data
type, that language is said to be strongly typed. This prevents errors, because
while it is logical to ask the computer to multiply a float by an integer (1.5 x
5), it is illogical to ask the computer to multiply a float by a string (1.5 x
Alice). When a programming language allows a variable of one data type to be
used as if it were a value of another data type, the language is said to be
weakly typed.
Algorithm definition

 In computer programming terms, an algorithm is a set of well-defined

instructions to solve a particular problem. It takes a set of input(s) and

produces the desired output.


 Well defined instruction in an algorithm include
1. when given an initial state, (INPUT)

2. proceed through a well-defined series


3. successive states, (PROCESS)
4. eventually terminating in an end-state(OUTPUT)
Algorithm
Initial condition

Insert
Insert amount to be withdraw amount
again

~---i Check balance

Series of no
process
including Deduct amount from balance
loop and
selection

Dispense the cash

Get the money

exit
Algorithm
– Three types of algorithm basic control structure
• Sequential
• Selection
• Repeatition (Looping)
Algorithm
• Basic algorithm characteristics
– Finite solution
– Clear instructions
– Has input to start the execution
– Has output as the result of the execution
– Operate effectively
• Algorithm creation techniques
– Flowchart, pseudo code, language etc
• Factors for measuring good algorithm
– Running time
– Total memory usage
Abstraction
 Abstraction is one of the key concepts of object-oriented programming (OOP)

languages. Its main goal is to handle complexity by hiding unnecessary details

from the user. That enables the user to implement more complex logic on top

of the provided abstraction without understanding or even thinking about all

the hidden complexity.

 That’s a very generic concept that’s not limited to object-oriented

programming. You can find it everywhere in the real world.


Abstraction example
 I’m a coffee addict. So, when I wake up in the morning, I go into my kitchen,
switch on the coffee machine and make coffee. Sounds familiar?

 Making coffee with a coffee machine is a good example of abstraction.

 You need to know how to use your coffee machine to make coffee. You need to
provide water and coffee beans, switch it on and select the kind of coffee you
want to get.

 The thing you don’t need to know is how the coffee machine is working internally
to brew a fresh cup of delicious coffee. You don’t need to know the ideal
temperature of the water or the amount of ground coffee you need to use.

 Someone else worried about that and created a coffee machine that now acts as
an abstraction and hides all these details. You just interact with a simple interface
that doesn’t require any knowledge about the internal implementation.
Abstraction example 2
Abstraction in OOP

 Objects in an OOP language provide an abstraction that hides the internal

implementation details. Similar to the coffee machine in your kitchen, you

just need to know which methods of the object are available to call and

which input parameters are needed to trigger a specific operation. But you

don’t need to understand how this method is implemented and which kinds of

actions it has to perform to create the expected result.


Abstract data types (ADTs)
 Data types such as int, float, double, long, etc. are considered to be in-built

data types and we can perform basic operations with them such as addition,

subtraction, division, multiplication, etc. Now there might be a situation

when we need operations for our user-defined data type which have to be

defined. These operations can be defined only as and when we require them.

So, in order to simplify the process of solving problems, we can create data

structures along with their operations, and such data structures that are not

in-built are known as Abstract Data Type (ADT).


ADTs cont…
 Abstract Data type (ADT) is a type (or class) for objects whose behavior is

defined by a set of values and a set of operations. The definition of ADT only

mentions what operations are to be performed but not how these operations

will be implemented. It does not specify how data will be organized in

memory and what algorithms will be used for implementing the operations. It

is called “abstract” because it gives an implementation-independent view.

 The process of providing only the essentials and hiding the details is known as

abstraction.
ADTs cont…
abstraction.
ADTs Cont…

 The user of data type does not need to know how that data type is
implemented, for example, we have been using Primitive values like int,
float, char data types only with the knowledge that these data type can
operate and be performed on without any idea of how they are
implemented.

 So a user only needs to know what a data type can do, but not how it will be
implemented. Think of ADT as a black box which hides the inner structure and
design of the data type. Now we’ll define three ADTs namely List ADT, Stack
ADT, Queue ADT.
ADTs and their functions
List ADT

Linked list: is an implementation based on the concept of


a node. A node is made up of two pieces of information,
data and a pointer containing address of the next node in the list
•The data is generally stored in key sequence in a list which has a
head structure consisting of count, pointers and address of compare
function needed to compare the data in the list.
•The data node contains the pointer to a data structure and a
self-referential pointer which points to the next node in the list.
ADTs and their functions

List ADT
•The List ADT Functions is given below:
•get() – Return an element from the list at any given position.
•insert() – Insert an element at any position of the list.
•remove() – Remove the first occurrence of any element from a non-empty list.
•removeAt() – Remove the element at a specified location from a non-empty list.
•replace() – Replace an element at any position by another element.
•size() – Return the number of elements in the list.
•isEmpty() – Return true if the list is empty, otherwise return false.
•isFull() – Return true if the list is full, otherwise return false.
Linked list consists of:

– a sequence of nodes,
– data fields
– one or two links or references pointing to the next
and/or previous nodes

3 12 11
Stack ADTs

•In Stack ADT Implementation instead of data being stored in each node,
the pointer to data is stored.
•The program allocates memory for the data and address is passed to the stack ADT.
•The head node and the data nodes are encapsulated in the ADT.
The calling function can only see the pointer to the stack.
•The stack head structure also contains a pointer to top and count of number
of entries currently in stack.
Stack ADTs

The Stack ADT Functions is given below:


•push() – Insert an element at one end of the stack called top.
•pop() – Remove and return the element at the top of the stack, if it is not empty.
•peek() – Return the element at the top of the stack without removing it, if the stack is
not empty.
•size() – Return the number of elements in the stack.
•isEmpty() – Return true if the stack is empty, otherwise return false.
•isFull() – Return true if the stack is full, otherwise return false.
• Stack
– Based on the principle of Last In First Out (LIFO)
– Stacks are used extensively at every level of a modern
computer system (compiler etc.)
In Out

Top
Stack Application
Queue ADTs
•The queue abstract data type (ADT) follows the basic design of the
stack abstract data type.
•Each node contains a void pointer to the data and the link pointer to the next
element in the queue. The program’s responsibility is to allocate memory for storing the
data.
•enqueue() – Insert an element at the end of the queue.
•dequeue() – Remove and return the first element of the queue, if the queue is not
empty.
•peek() – Return the element of the queue without removing it, if the queue is not
empty.
•size() – Return the number of elements in the queue.
•isEmpty() – Return true if the queue is empty, otherwise return false.
•isFull() – Return true if the queue is full, otherwise return false.

– First-In-First-Out (FIFO) data structure
– the first element added to the queue will be the
first one to be removed (post office, bank etc)

Out
In
Back Front
Features of ADT
 Abstraction: The user does not need to know the implementation of the data
structure.
 Better Conceptualization: ADT gives us a better conceptualization of the real
world.
 Robust: The program is robust and has the ability to catch errors.
Basic terminologies

 What is a Data Structure?

 As the name indicates, Data Structure is used for organizing the data in
memory. There are various ways of organizing the data in the memory, for eg.
array, list, stack, queue, and many more. Check out these free data structure
courses today!

 The data structure isn’t a programming language like C, C++, Java, etc. It is a
set of algorithms that can be used in any programming language to organize
the data in the memory.
Need of Data Structure

 As applications are becoming more complex and the amount of data is

increasing day by day, which may cause problems with processing speed,

searching data, handling multiple requests etc. Data structure provides a way

of organizing, managing, and storing data efficiently. With the help of data

structure, the data items can be traversed easily. Data structure provides

efficiency, reusability and abstraction. It plays an important role in enhancing

the performance of a program because the main function of the program is to

store and retrieve the user’s data as fast as possible.


Why Learn Data Structure?

 Data structure and algorithms are two of the most important aspects of

computer science. Data structures allow us to organize and store data, while

algorithms allow us to process that data in a meaningful way. Learning data

structure and algorithms will help you become a better programmer. You will

be able to write code that is more efficient and more reliable. You will also

be able to solve problems more quickly and more effectively.


Types of data structures
Types of data structures cont…

Primitive Data Structure –

 Primitive Data Structures directly operate according to the machine instructions. These are
the primitive data types. Data types like int, char, float, double, and pointer are primitive
data structures that can hold a single value.

Non – Primitive Data Structure –

 Non-primitive data structures are complex data structures that are derived from primitive
data structures. Non – Primitive data types are further divided into two categories.

Linear Data Structure

Non – Linear Data Structure


Linear Data Structure –

 Linear Data Structure consists of data elements arranged in a sequential

manner where every element is connected to its previous and next elements.

This connection helps to traverse a linear arrangement in a single level and in

a single run. Such data structures are easy to implement as memory is

additionally sequential. Some examples of Linear Data Structure are List,

Queue, Stack, Array etc.


Types of Linear Data Structure –
1] Arrays –

 An array is a collection of similar data elements stored at contiguous memory locations. It is

the simplest data structure where each data element can be accessed directly by only using

its index number.

2] Linked List –

 A linked list is a linear data structure that is used to maintain a list-like structure in the

computer memory. It is a group of nodes that are not stored at contiguous locations. Each

node of the list is linked to its adjacent node with the help of pointers.

3] Stack –
 Stack is a linear data structure that follows a specific order during which the operations are
performed. The order could be FILO (First In Last Out) or LIFO (Last In First Out).
Non-Linear Data Structure –

 Non-linear Data Structures do not have any set sequence of connecting all its

elements and every element can have multiple paths to attach to other

elements. Such data structures support multi-level storage and sometimes

can’t be traversed in a single run. Such data structures aren’t easy to

implement but are more efficient in utilizing memory. Some examples of non-

linear data structures are Tree, BST, Graphs etc.


Types of Non-Linear Data Structure
 1] Tree –

 A tree is a multilevel data structure defined as a set of nodes. The topmost


node is named root node while the bottom most nodes are called leaf nodes.
Each node has only one parent but can have multiple children.
Tree

Root
of node 20

7
Types of Non-Linear Data Structure
 2] Graph

 A graph is a pictorial representation of a set of objects connected by links


known as edges. The interconnected nodes are represented by points named
vertices, and the links that connect the vertices are called edges.
Graph Example

Undirected Directed graph


Classification of Data Structure
 Data Structure can be further classified as

 Static Data Structure

 Dynamic Data Structure

 Static Data Structure

 Static Data Structures are data structures where the size is allocated at the
compile time. Hence, the maximum size is fixed and cannot be changed.

 Dynamic Data Structure

 Dynamic Data Structures are data structures where the size is allocated at the
run time. Hence, the maximum size is flexible and can be changed as per
requirement.
Data Structure Operations –
 Searching – We can easily search for any data element in a data structure.

 Sorting – We can sort the elements either in ascending or descending order.

 Insertion – We can insert new data elements in the data structure.

 Deletion – We can delete the data elements from the data structure.

 Updation – We can update or replace the existing elements from the data

structure.
Advantages of Data Structure –
 Data structures allow storing the information on hard disks.

 An appropriate choice of ADT (Abstract Data Type) makes the program more
efficient.

 Data Structures are necessary for designing efficient algorithms.

 It provides reusability and abstraction.

 Using appropriate data structures can help programmers save a good amount
of time while performing operations such as storage, retrieval, or processing
of data.

 Manipulation of large amounts of data is easier.


Data Structure Applications
 1. Organization of data in a computer’s memory

 2. Representation of information in databases

 3. Algorithms that search through data (such as a search engine)

 4. algorithms that manipulate data (such as a word processor)

 5. algorithms that analyze data (such as a data miner)

 6. algorithms that generate data (such as a random number generator)

 7. algorithms that compress and decompress data (such as a zip utility)

 8. algorithms that encrypt and decrypt data (such as a security system)

 9. software that manages files and directories (such as a file manager)

You might also like