0% found this document useful (0 votes)
86 views37 pages

Overview of Data Structures

The document provides an introduction to data structures, covering concepts such as algorithms, abstract data types (ADTs), pointers, and dynamic memory allocation. It classifies data structures into linear and non-linear types and discusses operations like creation, deletion, searching, and sorting. Additionally, it explains the differences between structures and unions, as well as memory management functions like malloc, calloc, and realloc.

Uploaded by

gamerid45450
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views37 pages

Overview of Data Structures

The document provides an introduction to data structures, covering concepts such as algorithms, abstract data types (ADTs), pointers, and dynamic memory allocation. It classifies data structures into linear and non-linear types and discusses operations like creation, deletion, searching, and sorting. Additionally, it explains the differences between structures and unions, as well as memory management functions like malloc, calloc, and realloc.

Uploaded by

gamerid45450
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Data

Structures
Chittaranjan Pradhan

Data Structures 1 Problem Solving

Introduction to Data Structures Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
1.1
Introduction to Data
Problem Solving Structures
Chittaranjan Pradhan

Algorithm
Problem Solving

• Algorithm is a sequence of steps written in simple English Data structure

phrases to describe the solution to a given problem Abstract Data Type


(ADT)

• It breaks the solution of a problem into a series of simple Pointer


Pointer Operations
descriptive steps
Function

Structure vs. Union

Dynamic Memory
Flowchart Allocation

Algorithms
• Flowchart is the pictorial representation of an algorithm Recursive Algorithm
Algorithm Analysis

• It’s objective is to ease the understanding of programming


logic

Pseudocode

• Pseudocode represents the problem solution with the help


of generic syntax and normal English phrases

1.2
Introduction to Data
Problem Solving... Structures

Q: Find whether num is even or odd Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.3
Introduction to Data
Problem Solving... Structures

Q: Find whether num is prime or not Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.4
Introduction to Data
Data structure Structures
Chittaranjan Pradhan

Data structure
Problem Solving

• Data structure is representation of the logical relationship Data structure

existing between individual elements of data Abstract Data Type


(ADT)

Pointer
Pointer Operations
• It as the mathematical or logical model of particular
Function
organization of data items Structure vs. Union
• It is a storage that is used to store and organize data Dynamic Memory
Allocation
• Data structures are essential and responsible for Algorithms

organizing, processing, accessing and storing data Recursive Algorithm


Algorithm Analysis

efficiently
• By using data structure, one can organize and process a
very large amount of data in a relatively short period

Organized data + Allowed operations = Data structure

Algorithm + Data structure = Program

1.5
Introduction to Data
Classification of Data structure Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


Classification of Data structure (ADT)

Pointer
• Linear Data Structure: data elements are arranged Pointer Operations

sequentially or linearly Function

• Static Data structure: fixed memory size Structure vs. Union

Ex: Array Dynamic Memory


Allocation
• Dynamic Data structure: size is not fixed
Algorithms
Ex: Linked list, Stack, Queue Recursive Algorithm
Algorithm Analysis
• Non-Linear Data Structure: data elements are not placed
sequentially or linearly
Ex: Tree, Graph

1.6
Introduction to Data
Classification of Data structure... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.7
Introduction to Data
Classification of Data structure... Structures
Chittaranjan Pradhan

Commonly used operations on data structure


Problem Solving
• CREATE - results in reserving memory for the program Data structure

elements Abstract Data Type


(ADT)
• DESTROY or DELETE - destroys the memory space Pointer

allocated for the specified data structure. Pointer Operations

Function
• SELECTION - deals with accessing a particular data Structure vs. Union
within a data structure Dynamic Memory
Allocation
• UPDATION - updates or modifies the data in the data Algorithms
structure Recursive Algorithm
Algorithm Analysis

• Searching - finds the presence or location of the desired


data item in the list of data items
• Sorting - arranges all data items in a data structure in a
particular order
• Merging - combines the data items of two different sorted
lists into a single sorted list

1.8
Introduction to Data
Abstract Data Type (ADT) Structures
Chittaranjan Pradhan
Abstract Data Type (ADT)

• ADT is defined as a mathematical model of the data Problem Solving

Data structure
objects that make up a data type as well as the functions
Abstract Data Type
that operate on these objects (ADT)

• The specification of an ADT doesn’t imply any Pointer


Pointer Operations

implementation consideration. It includes the domain Function


values and operations on the domain Structure vs. Union

• Abstract data type allows re-usability of a code, i.e. it Dynamic Memory


Allocation
makes it convenient for a programmer to work a shorter Algorithms

code Recursive Algorithm


Algorithm Analysis

• It is a kind of data type whose behavior is defined with the


help of some attributes and some functions. Generally, we
write these attributes and functions inside a structure so
that we can use an instance of it
• An ADT consists of two parts: declaration of data and
declaration of operations
• Commonly used ADTs: Arrays, Stacks, Queues, Trees,
Graphs etc.
1.9
Introduction to Data
Abstract Data Type (ADT)... Structures
Chittaranjan Pradhan

Stack ADT Problem Solving

A Stack contains elements of same type arranged in sequential Data structure

order. All operations takes place at a single end that is top of Abstract Data Type
(ADT)
the stack and following operations can be performed: Pointer
Pointer Operations
• push() - Inserts an element at one end of the stack called
Function
top Structure vs. Union

• pop() - Removes and return the element at the top of the Dynamic Memory
Allocation
stack, if it is not empty Algorithms

• peek() - Returns the element at the top of the stack Recursive Algorithm
Algorithm Analysis

without removing it, if the stack is not empty


• size() - Returns the number of elements in the stack
• isEmpty() - Returns true if the stack is empty, otherwise
return false
• isFull() - Returns true if the stack is full, otherwise return
false

1.10
Introduction to Data
Pointer Structures
Chittaranjan Pradhan
Pointer

• A pointer can be used to store the memory address of Problem Solving

other variables or a memory location Data structure

Abstract Data Type


• We can access and manipulate the data stored in that (ADT)

memory location using pointers Pointer


Pointer Operations

• Pointers should always be initialized to some value before Function

starting using it Structure vs. Union

Dynamic Memory
• void * is considered as generic pointer Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.11
Introduction to Data
Pointer Operations Structures
Chittaranjan Pradhan
Pointer Operations

• Size of pointer depends on the OS and CPU architecture Problem Solving

Data structure
8 bytes for 64-bit system and 4 bytes for 32-bit system
Abstract Data Type
• %d - signed integer (ADT)

%u - unsigned integer Pointer


Pointer Operations

%p - pointer address Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.12
Introduction to Data
Pointer Operations... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.13
Introduction to Data
Function Structures
Chittaranjan Pradhan
Function

• Function declaration, function definition, function call Problem Solving

Data structure
• Actual parameter vs. formal parameter
Abstract Data Type
• Pass by value vs pass by reference (ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.14
Introduction to Data
Array, Pointer, Function Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.15
Introduction to Data
Structure vs. Union Structures
Chittaranjan Pradhan
Structure

• It allows a user to combine together logically related data Problem Solving

items of various data types Data structure

Abstract Data Type


• It represents a record. All of its elements get stored in the (ADT)

contiguous memory locations Pointer


Pointer Operations

Function

Union Structure vs. Union

Dynamic Memory
Allocation
• It combines various objects of different sorts and sizes
Algorithms
together Recursive Algorithm
Algorithm Analysis
• It allows storing different types in the same memory
location. It provides an efficient way of using the same
memory location for multiple purposes

1.16
Introduction to Data
Structure vs. Union... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.17
Introduction to Data
Structure vs. Union... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

struct Stud st[5];

1.18
Introduction to Data
Structure vs. Union... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.19
Introduction to Data
Dynamic Memory Allocation Structures
Chittaranjan Pradhan

Problem Solving
Dynamic Memory Allocation Data structure

Abstract Data Type


• Compile-time or Static allocation: the required amount of (ADT)

memory is allocated to the program element at the start of Pointer


Pointer Operations
the program. The memory to be allocated to the variable is
Function
fixed
Structure vs. Union
Ex: int a; Dynamic Memory
float a[5]; Allocation

• Problem 1: extra elements can be added as a part of the Algorithms


Recursive Algorithm
array Algorithm Analysis

• Problem 2: memory wastage for less numbers of elements

• Run-time or Dynamic allocation: makes efficient use of


memory, by allocating the required amount of memory
whenever needed

1.20
Introduction to Data
Dynamic Memory Allocation... Structures
Chittaranjan Pradhan
malloc()
Problem Solving
• It allocates a block of memory in bytes. User should
Data structure
explicitly give the block size it requires for the use Abstract Data Type
(ADT)
• The malloc() is like a request to RAM to allocate memory,
Pointer
if the request is granted, returns a pointer to the first block Pointer Operations

of that memory Function

Structure vs. Union


• It doesn’t intializes the allocated memory. It contains
Dynamic Memory
grabage values Allocation

Algorithms
• The type of the pointer it returns is void Recursive Algorithm

malloc (no. of elements * size of each element) Algorithm Analysis

• Ex: int *ptr;


ptr=malloc (10*sizeof(int))
Since this returns a void pointer, so a cast operator is
required as per the need
ptr_var = (type_cast *) malloc(size)
• Ex: int *ptr;
ptr=(int *) malloc (10*sizeof(int));
1.21
Introduction to Data
malloc()... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.22
Introduction to Data
Dynamic Memory Allocation... Structures
Chittaranjan Pradhan
calloc()

• It allocates multiple blocks of requested memory. It Problem Solving

Data structure
initializes the allocated memory to zero
Abstract Data Type
• It needs two arguments (ADT)

int *ptr; Pointer


Pointer Operations

ptr=(int *) calloc(10,2); Function

Structure vs. Union

Dynamic Memory
free() Allocation

Algorithms
• It is used to dynamically de-allocate the memory Recursive Algorithm
Algorithm Analysis

free(ptr);

realloc()

• If the memory previously allocated is insufficient, realloc


can be used to dynamically re-allocate memory
• Reallocation of remory maintains the already present
value and new blocks will be initialized with garbage value
ptr=realloc(ptr, new_size); 1.23
Introduction to Data
calloc()... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.24
Introduction to Data
realloc(), free()... Structures
Chittaranjan Pradhan

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.25
Introduction to Data
Algorithms Structures
Chittaranjan Pradhan

Algorithms Problem Solving

Data structure

• Algorithm is a self-contained step-by-step set of Abstract Data Type


(ADT)
operations to be performed for calculation, data Pointer
processing or automated reasoning tasks Pointer Operations

Function
• It is a finite set of instructions which takes some value or
Structure vs. Union
set of values as input; and produces some value or set of Dynamic Memory
values as output to accomplish a particular task Allocation

Algorithms
• Every algorithm must satisfy the following properties: Recursive Algorithm
Algorithm Analysis
• Input: input data supplied externally
• Output: output that has to be obtained
• Definiteness: each step should be clear and unambiguous
• Finiteness: must terminate after a finite number of steps
• Effectiveness: each step must be sufficiently basic and
simple

1.26
Introduction to Data
Recursive Algorithm Structures
Chittaranjan Pradhan
Recursive Algorithm

• It is an algorithm which calls itself Problem Solving

Data structure
• It is powerful and expresses an complex process very Abstract Data Type
clearly (ADT)

Pointer
• Two properties a recursive function must have: Pointer Operations

• Base criteria: There must be at least one base criteria to Function

stop calling itself Structure vs. Union

• Progressive criteria: The recursive calls should progress Dynamic Memory


Allocation
in such a way that each time a recursive call is made, it Algorithms
comes closer to the base criteria Recursive Algorithm
Algorithm Analysis
• It can be implemented by means of stack

1.27
Introduction to Data
Algorithm Analysis Structures
Chittaranjan Pradhan

Algorithm Analysis Problem Solving

Data structure
• In designing algorithms, methodologies are required to Abstract Data Type
find the most effective approach for the problems (ADT)

Pointer
• First approach to analyze an algorithm is to check the Pointer Operations

correctness of the algorithm Function


• Second approach of analysis is to check the simplicity of the Structure vs. Union
algorithm Dynamic Memory
Allocation
• The complexity of the algorithm is determined in terms of Algorithms
time and space Recursive Algorithm
Algorithm Analysis

• Time complexity of an algorithm is a function of size of the


input

• Space complexity of an algorithm is the amount of


memory that it needs

1.28
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan

Space Complexity
Problem Solving

• Space complexity of an algorithm represents the amount Data structure

of memory space required by the algorithm in its life cycle Abstract Data Type
(ADT)

• Space required by an algorithm is equal to the sum the Pointer

following two components: Pointer Operations

Function
• A fixed part (FP) that is a space required to store certain
Structure vs. Union
data and variables, that are independent of the size of the Dynamic Memory
problem. Ex: variables, constant, program size etc Allocation

• A variable part (VP) is a space required by variables, whose Algorithms


Recursive Algorithm
size depends on the size of the problem. Ex: dynamic Algorithm Analysis

memory allocation, recursion stack space etc


• Space complexity of any program P is SC(P)=FP+VP(I)
• Algorithm: SUM (A,B)
Step 1: Start
Step 2: C ← A + B + 10
Step 3: Stop
SC(SUM)=FP+VP=3*2+2+0=8Bytes

1.29
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan

Problem Solving
Time Complexity Data structure

Abstract Data Type


• Time complexity of an algorithm represents the amount of (ADT)

Pointer
time required by te algorithm to run to completion Pointer Operations

• Time requirements can be defined as a numerical function Function

T(n), where T(n) can be measured as Structure vs. Union

Dynamic Memory
number of steps * time taken by each step Allocation

• Time required by an algorithm falls under three types: Algorithms


Recursive Algorithm
• Best case: Minimum time required for program execution. Algorithm Analysis

Ex: Searching of first element in an array


• Average case: Average time required for program
execution. Ex: Searching of any middle element in an array
• Worst case: Maximum time required for program execution.
Ex: Searching of last element in an array

1.30
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan

Problem Solving
Asymptotic Analysis Data structure

Abstract Data Type


• It refers to computing the running time of any operation in (ADT)

mathematical units of computation Pointer


Pointer Operations

• O Notation - Big Oh: O(n) is the formal way to express the Function
upper bound of an algorithm’s running time. It measures the Structure vs. Union

worst case time complexity or longest amount of time an Dynamic Memory


Allocation
algorithm can possibly take to complete
Algorithms
• Ω Notation - Omega: Ω(n) is the formal way to express the Recursive Algorithm

lower bound of an algorithm’s running time. It measures the Algorithm Analysis

best case time complexity or best amount of time an


algorithm can possibly take to complete
• θ Notation - Theta: θ(n) is the formal way to express both
the lower bound and upper bound of an algorithm’s running
time

1.31
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan

Problem Solving

Data structure
Time Complexity... Abstract Data Type
(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

Time complexity of a function (or set of statements) is


considered as O(1) if it doesn’t contain loop, recursion and call
to any other non-constant time function

1.32
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan
Time Complexity...

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

Time complexity of the loop with varying number of iterations of


n would be O(n) 1.33
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan
Time Complexity...

• In the for index updation by +c or -c, number of iterations = Problem Solving

Data structure
n/c. Thus, complexity T(n)=O(n)
Abstract Data Type
• In the for index updation by *c or ÷c, number of (ADT)

iterations=logc (n). Thus, complexity T (n) = O(logc n) Pointer


Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.34
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan

Problem Solving

Time Complexity... Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

1.35
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan
Time Complexity...
If the loop variable is reduced or increased exponentially in a Problem Solving
constant amount, then complexity of the loop is considered as Data structure

O(loglogn) Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis


The time complexity of loop is considered O( n) if the loop
variable is multiplied by itself
The time complexity of loop is considered O(1) if the loop
contains break statement 1.36
Introduction to Data
Algorithm Analysis... Structures
Chittaranjan Pradhan
Time Complexity...

Problem Solving

Data structure

Abstract Data Type


(ADT)

Pointer
Pointer Operations

Function

Structure vs. Union

Dynamic Memory
Allocation

Algorithms
Recursive Algorithm
Algorithm Analysis

Logarithimic function < Polynomial function < Exponential


function 1.37

You might also like