0% found this document useful (0 votes)
7 views53 pages

Data Structures Unit-1

This document provides an overview of data structures, defining them as representations of logical relationships between data elements. It classifies data structures into primitive and non-primitive categories, detailing linear and non-linear structures, as well as operations such as traversing, insertion, deletion, searching, sorting, and merging. Additionally, it discusses abstract data types, algorithm complexity, control statements, and their applications in programming.

Uploaded by

karth2054
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)
7 views53 pages

Data Structures Unit-1

This document provides an overview of data structures, defining them as representations of logical relationships between data elements. It classifies data structures into primitive and non-primitive categories, detailing linear and non-linear structures, as well as operations such as traversing, insertion, deletion, searching, sorting, and merging. Additionally, it discusses abstract data types, algorithm complexity, control statements, and their applications in programming.

Uploaded by

karth2054
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
You are on page 1/ 53

UNIT-1 DATA STRUCTURES

DATA STRUCTURES

UNIT - I

INTRODUCTION AND OVERVIEW

Definition: Data structure is a representation of logical relationships existing between individual


elements of data.
In other words, a data structure defines a way of organizing all data items that considers not only the
elements stored but also their relationship to each other. The term data structure is used to
describe the way data is stored.

To develop a program of an algorithm, we should select an appropriate data structure for that

S
algorithm. Therefore, data structure is represented as:

Algorithm + Data structure = Program

Elementary Data Organization


IB
The study of computer science includes the study of data being organized in a computer, how data can
be manipulated, and how data can be retrieved and utilized.

● Data − Data are values or sets of values.


● Data Item − Data item refers to a single unit of values.
● Group Items − Data items that are divided into sub-items are called group items.
● Elementary Items − Data items that cannot be divided are called elementary items.
R
● Attribute and Entity − An entity is that which contains certain attributes or properties, which may be
assigned values.

● Entity Set − Entities of similar attributes form an entity set.

● Field − Field is a single elementary unit of information representing an attribute of an entity.

● Record − Record is a collection of field values of a given entity.

● File − File is a collection of records the entities agave entity set.

MAHESH BALGOL , Assistant Professor , RIBS


Data Structures
Data structures are defined as a logical or mathematical model of a particular organization of data.

Data Structure Classification

S
IB
R
Data Structure can be broadly classified as

1) Primitive Data Structure:

●​ Theprimitive data structures are primitive data types.


●​ These are data structures that are directly operated upon by machine-level instructions.
●​ The storage structures (memory representation) of these data structures vary from one machine
to another.
●​ PDS are those that are predefined ways of storing data by the system.

MAHESH BALGOL , Assistant Professor , RIBS


●​ Examples : The int, char, float, double, and pointer are the primitive data structures that can hold
a single value.

i) Integer (Int): An integer is a number that can be written without a fractional component.

Examples : 25 , 100 and 250 are Integers.

ii) Float / Real : A float is a number that can be written with a fractional component.

Examples : 25.5 , 100.50 and 250.75 are floats.

iii) Character ( Char) : A character is a single character enclosed with a single quote.

Examples : ‘A’, ‘a’ , and ‘s’ are characters.

iv) Boolean : A boolean expression is an expression that results in a boolean value , that is , in a value of either

S
true or false.

Examples : A flag indicating if a user is logged in: isLoggedIn = true (logged in), isLoggedIn = false
(not logged in)

v) Pointer : Pointer is a variable which is used to store the memory address of another variable.
IB
2) Non-Primitive Data Structure:

●​ The Data Structures which are not primitive are called Non- Primitive Data Structures.
●​ The non-primitive data structure is a kind of data structure that can hold multiple values.
●​ This means that they cannot be operated upon directly by machine level instructions.Depending
on the type of relationships between data elements in a data structure .
R
Non-Primitive Data Structure are classified as

1) Linear Data Structure :

●​ The arrangement of data items in a sequential manner is known as a linear data structure.
●​ This type of data structures establishes the relationship of adjacency between the elements.

Linear Data Structure is classified as

1) Static Data Structure: In a static data structure,size and structure with memory location are fixed at
compile time.

MAHESH BALGOL , Assistant Professor , RIBS


●​ Arrays : An array is a collection of multiple data items of the same data type in a single
variable name and each dataitem is called an element of the array.

2) ​ Dynamic Data Structure: A dynamic data structure can shrink or expand program execution as

S
required by the program.

Dynamic Data Structure Classified into :

●​ Linked List : Linked list is a linear data structure which is used to maintain a list in the
memory.
IB
R
●​ Stack:Stack is a linear list in which both insertion and deletions are allowed only at one
end of stack , called top.

MAHESH BALGOL , Assistant Professor , RIBS


→ Stack is a Last-In-First-Out (LIFO).

S
●​ Queue : Queue is a linear list in which elements can be inserted only at one end called
rear and deleted only at the other end called front.

→ Queue is a First-In-First-Out (FIFO)


IB
R

2) Non-Linear Data Structure :

●​ The data structure in which data items are not arranged in order.
●​ Non-Linear Data Structures they establish relationships other than the adjacency relationship.

Examples for Non - Linear Data Structures are

MAHESH BALGOL , Assistant Professor , RIBS


1)​ Trees :
●​ Trees are Non - Linear / multilevel which organizes data in hierarchical structure.
●​ In tree data structure , every individual element is called a Node.
●​ The bottom most nodes in the hierarchy are called leaf nodes while the topmost
node is called root node.

2)​
●​

●​
Graphs :

S
Graphs can be defined as the pictorial representation of the set of elements
connected by the links known as edges.
The interconnected objects are represented by points termed as vertices , and the
IB
links that connect the vertices are edges.
R
3)​ Tables
4)​ Sets

Non-linear data structures are classified into two types :

1)​ Homogenous Data Structures : If similar types of data elements are stored by data structures
(or) In a homogenous data structure , all elements are of the same type.
2)​ Non - Homogeneous Structures : If two or more types of data elements are stored by data
structures , then it is called non-homogeneous structures.

MAHESH BALGOL , Assistant Professor , RIBS


Data structure Operations
1) Traversing :

●​ It is the process of accessing each element exactly once to perform a certain operation on it. (or)
●​ An element must be visited before performing any of the operations.
●​ Traversing means visiting each element exactly once.

2) Insertion : Insertion can be defined as the process of adding the elements to the data structure at any
location.

3) Deletion: The process of removing an element from the data structure is called deletion.

4) Searching :

S
●​ The process of finding the location of an element within the data structure is called searching.
●​ There are two algorithms to perform searching: Linear search and Binary search.

5) Sorting :
IB
●​ The process of arranging the data structure in a specific order is known as Sorting.
●​ There are many algorithms that can be used to perform sorting, for example, insertion sort,
selection sort, bubble sort, etc.

6) Merging :

●​ When two lists, List A and List B, of sizes M and N, respectively, of similar types of elements,
are clubbed or joined to produce the third list, List C, of size (M+N), then this process is called
R
merging.

Abstract Data Type (ADT)

ADT is a mathematical model with a collection of operations defined on that model.


Or
An abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed
on the data.

MAHESH BALGOL , Assistant Professor , RIBS


Or
An abstract data type is a data type associated with operations but whose representation is hidden.
Abstract Data Type Model

S
IB
Algorithm: “An algorithm is a step-by-step procedure for performing some tasks in a
R
finite amount of time.”

Algorithm Complexity
The complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an
input of a given size (n).

In order to find the complexity of an algorithm, two kinds of efficiencies are to be kept in mind.

1. Space Complexity : It indicates the amount of storage required for running the algorithm. I.e., “The amount
of memory needed by the algorithm to run to completion.”

MAHESH BALGOL , Assistant Professor , RIBS


2. Time Complexity : Time complexity of an algorithm is the amount of time it needs in order to run to
completion.

TimeSpace Tradeoff
→ The best algorithm to solve a given problem is one that requires less memory space and less time to run to
completion.
Or

→ Time-Space tradeoff is a way of solving a problem or calculation in less time by using more storage space or
by solving a problem in very little space by spending a long time.

Preliminaries

S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


Control Statement
Control statements enable us to specify the flow of program control, i.e., the order in which
the instructions in a program must be executed.

S
IB
1)​ Sequential statement: The programmer writes a sequence of statements to do a
specific activity. All these statements in the program are executed in the order in which they
appear in the program. These programming statements that are executed sequentially, that is,
one after the other, are called sequential control statements.
R

MAHESH BALGOL , Assistant Professor , RIBS


2) Branching statements

Conditional Statement: These statements that alter the sequence of execution of the
program based on some condition are called conditional branch statements. They are also
called selection statements or decision statements.

1) If statement : The if statement is used to check some given condition and


perform some operations depending on the correctness of that condition.

Syntax :

if(expression){

// code to be executed

S
IB
}
Example
Program
#include<stdioh
>

int main(){
R
int number=0;

printf("Enteranumber:"

);

scanf("%d",&number);

if(number%2==0){

printf("%d is even number, number);

MAHESH BALGOL , Assistant Professor , RIBS


return 0;

Flow Chart

S
IB
R

2)​ If-else Statement : The if-else statement is used to perform two operations for a
single condition. The if-else statement is an extension to the if statement, using which we can
perform two different operations.

Syntax :

MAHESH BALGOL , Assistant Professor , RIBS


if(expression){

// code to be executed if condition is true

}else{

// code to be executed if condition is false

Flow Chart

S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


ExampleProgra
m
#include<stdio.
h>

int main(){

int number=0;

printf("enteranumber:")

scanf("%d",&number);

if(number%2==0){

else{
printf("%d is even number, number);

S
IB
printf("%d is odd number);

return 0;

}
R
3)​ If-else-if ladder statement : The if-else-if ladder statement is an extension to the
if-else statement. It is used in the scenario where there are multiple cases to be performed for
different conditions.
Syntax :

if(condition1){

// code to be executed if condition1 is true

}else if(condition2){

MAHESH BALGOL , Assistant Professor , RIBS


//code to be executed if condition2 is true

else if(condition3){

// code to be executed if condition3 is true

...

else{

// code to be executed if all the conditions are false

S
}

Flow Chart
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


Example

Program

#include<stdio.

h>

int main(){

int number=0;

S
printf("enteranumber:")

scanf("%d",&number);

if(number==10){
IB
printf("The number is equal to 10");

else if(number==50){

printf("numberisequalto50");
R
}

else if(number==100){

printf("numberisequalto100");

else{

printf("The number is not equal to 10, 50 or 100");

MAHESH BALGOL , Assistant Professor , RIBS


return 0;

4)​ C Switch Statement : The switch statement in C is an alternative to


if-else-if ladder statement, which allows us to execute multiple operations for the
different possible values of a single variable called the switch variable.
Syntax

S
switch(expression){

case value1:

// code to be executed;
IB
break;//optional

case value2:

// code to be executed;

break;//optional

......
R
default:

code to be executed if all cases are not matched;

Rules for switch statement in C language

1) The switch expression must be of an integer or character type.

MAHESH BALGOL , Assistant Professor , RIBS


2) The case value must be an integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in the switch case is not a must. It is optional. If there is no break
statement found in the case, all the cases will be executed present after the matched case. It is
known as falling through the state of the C switch statement.

Flow Chart

S
IB
R

ExampleProgr
am

MAHESH BALGOL , Assistant Professor , RIBS


#include<stdio.
h>

int main(){

int number=0;

printf("enteranumber:")

scanf("%d",&number);

switch(number){

case 10:

printf("The number is equal to 10");

Brea

S
IB
k;

case

50:

printf("The number is equal to 50");

break;
R
case1

00:

printf("The number is equal to 100");

Brea

k;

defa

ult:

MAHESH BALGOL , Assistant Professor , RIBS


printf("The number is not equal to 10, 50 or 100");

return 0;

UnconditionalStatement: Unconditional branching is when the programmer forces the


execution of a program to jump to another part of the program.

1)​ Goto : The goto statement is known as a jump statement in C. As the


name suggests, goto is used to transfer the program control to a predefined
label.

S
Sy

nta

lab
IB
el:

//some part of the code;

goto label;

ExampleProgra
R
m

#include<stdio.

h>int main()

int num,i=1;

printf("Enter the number whose table you want to print?");

MAHESH BALGOL , Assistant Professor , RIBS


scanf("%d",&num);

table:

printf("%dx%d=%d\n",num,i,num*i);

i++;

if(i<=10

gotota

ble;

S
2) ​ Break: The break is a keyword in C that is used to bring the program control out
of the loop. The break statement is used inside loops or switch statements.

Syntax
IB
//loop or switch case

break;

Example Program

#include<stdlib.h>

void main ()
R
{

int i;

for(i = 0; i<10; i++)

printf("%d

",i);

MAHESH BALGOL , Assistant Professor , RIBS


if(i == 5)

break;

printf("came outside of loop i = %d);

Flow Chart

S
IB
R
3) Continue : The continue statement in the C language is used to bring the program
control to the beginning of the loop. The continuous statement skips some lines of code
inside the loop and continues with the next iteration.

Syntax

//loop statements

MAHESH BALGOL , Assistant Professor , RIBS


continue;

//somelinesofthecodewhichistobeskipped

Example Program

#include<stdio.h>

void main ()

int i = 0;

while(i!=10)

S
{

printf("%d

",i);

continue;
IB
i++;

}
R
4) Return: A return statement ends the execution of a function and returns control
to the calling function.

Syntax

return expression;

ExampleProgr

am

MAHESH BALGOL , Assistant Professor , RIBS


int main()

int n = 1;

int x = INT_MAX;

report_square();

report_ratio(n,x);

}
return 0;

S
IB
3) LoopStatement: The looping can be defined as repeating the same process
multiple times until a specific condition is satisfied.

1) For Statement: The for looping in the C language is used to iterate the statements or a
part of the program several times. It is frequently used to traverse the data structures like the
R
array and linked list.

Syntax

for (Expression 1; Expression 2; Expression 3) or for (initialiazation; condition; Incr/Decr)

// code to be executed

MAHESH BALGOL , Assistant Professor , RIBS


Flow chart

S
IB
R

ExampleProgr
am

MAHESH BALGOL , Assistant Professor , RIBS


#include<stdio.
h>

int main(){

int i=0;

for(i=1;i<=10;i++){

printf("%d \n",i);

return 0;

S
}

2) While statement: A while loop is also known as a pre-tested loop. In general, a while
loop allows a part of the code to be executed multiple times depending upon a given boolean
condition.

Syntax
IB
while(condition){

// code to be executed

}
R
Flow Chart

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
Exam

Program

#include<stdio.
R
h>int main(){

int i=1;

while(i<=10

){

printf("%d\n

",i); i++;

MAHESH BALGOL , Assistant Professor , RIBS


return 0;

3) Do While : The do while loop is a post-tested loop. Using the do-while loop, we can
repeat the execution of several parts of the statements. The do-whileloop is mainly used in
the case where we need to execute the loop at least once.

Syntax

do{

// code to be executed

}while(conditi

S
on);

Flow Chart
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


ExampleProgram

#include<stdio.

h>

int main(){

int i=1;

S
do{

printf("%d\n

",i); i++;
IB
}while(i<=10);

return 0;

}
R
Complexities of an Algorithm
The complexity of an algorithm is the amount of time and space required by an algorithm for
an input of size (n). The complexity of an algorithm can be divided into two types.

The time complexity and the space complexity.

Orders Of Growth

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

Usually, the time required by an algorithm falls under three types:

MAHESH BALGOL , Assistant Professor , RIBS


● Best Case: − It gives the minimum time required for program execution.
● Average Case− It gives the average time required for program execution.
● Worst Case: It gives the maximum time required for program execution.

Asymptotic Notation for Complexity of Algorithms


Asymptotic​ analysis of an algorithm refers to defining the mathematical
foundation/framing of its run-time performance.

→ Asymptotic notations are mathematical tools used to analyze the


performance of algorithms by understanding how their efficiency changes
as the input size grows.

S
●​ These notations provide a concise way to express the behavior of an

algorithm’s time or space complexity as the input size approaches

infinity.
IB
Asymptotic Notations
R
The commonly used asymptotic notations used for calculating the running time complexity
of an algorithm are given below:

● Big oh Notation (O)

● Omega Notation (Ω)

● Theta Notation (θ)

MAHESH BALGOL , Assistant Professor , RIBS


Big oh Notation (O)

● A Big O notation is an asymptotic notation that measures the


performance of an algorithm by simply providing the order of growth of the
function.

● This notation provides an upper bound on a function, which ensures


that the function never grows faster than the upper bound. So, it gives the least
upper bound on a function so that the function never grows faster than this upper
bound.

S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


Def:

If f(n) and g(n) are the two functions defined for positive integers, then f(n) = O(g(n)) as f(n)
is big oh of g(n) or f(n) is on the order of g(n). If there exist constants c and no such that:

f(n) ≤ c.g(n) for all n ≥ 𝑛0

Omega Notation (Ω)

S
IB
R
● It basically describes the best scenario, which is opposite to the bigo
notation.

● It is the formal way to represent the lower bound of an algorithm's running


time. It measures the best amount of time an algorithm can possibly take to complete
or the best-case time complexity.

● It determines what is the fastest time that an algorithm can run.

MAHESH BALGOL , Assistant Professor , RIBS


Def:

If f(n) and g(n) are the two functions defined for positive integers, then f(n) = Ω(g(n)) as f(n)
is the big omega of g(n) or f(n) is on the order of g(n). If there exist constants c and no such
that:

c.g(n) ≤ f(n) for all n ≥ 𝑛0

Theta Notation (θ)

S
IB
R

● The theta notation mainly describes the average case scenarios.

MAHESH BALGOL , Assistant Professor , RIBS


● It represents the realistic time complexity of an algorithm. Every time an
algorithm does not perform worst or best in real-world problems, algorithms
mainly fluctuate between the worst-case and best-case, and this gives us the
average case of the algorithm.

● Big theta is mainly used when the value of the worst-case and the
best-case is the same.

● It is the formal way to express both the upperbound and lowerbound


of an algorithm's running time.

Def:

S
If f(n) and g(n) are the two functions defined for positive integers, then f(n) = Ө(g(n)), as f(n)
is the big theta of g(n) or f(n) is on the order of g(n). If there exist constants c and no such
that:

c1.g(n) ≤ f(n) ≤
IB
c2.g(n) for all n ≥ 𝑛0

String Processing
●​ A string is a sequence of characters stored as a single entity.
R
●​ It can include letters, numbers, symbols, and spaces.
●​ Strings are commonly used in programming for text processing, user inputs, and
data manipulation.

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS


S
IB
R

MAHESH BALGOL , Assistant Professor , RIBS

You might also like