Data Structures Unit-1
Data Structures Unit-1
DATA STRUCTURES
UNIT - I
To develop a program of an algorithm, we should select an appropriate data structure for that
S
algorithm. Therefore, data structure is represented as:
S
IB
R
Data Structure can be broadly classified as
i) Integer (Int): An integer is a number that can be written without a fractional component.
ii) Float / Real : A float is a number that can be written with a fractional component.
iii) Character ( Char) : A character is a single character enclosed with a single quote.
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
● 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.
1) Static Data Structure: In a static data structure,size and structure with memory location are fixed at
compile time.
2) Dynamic Data Structure: A dynamic data structure can shrink or expand program execution as
S
required by the program.
● 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.
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.
● The data structure in which data items are not arranged in order.
● Non-Linear Data Structures they establish relationships other than the adjacency relationship.
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
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.
● 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.
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.”
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
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
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.
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){
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 :
}else{
Flow Chart
S
IB
R
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){
}else if(condition2){
else if(condition3){
...
else{
S
}
Flow Chart
IB
R
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{
S
switch(expression){
case value1:
// code to be executed;
IB
break;//optional
case value2:
// code to be executed;
break;//optional
......
R
default:
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
int main(){
int number=0;
printf("enteranumber:")
scanf("%d",&number);
switch(number){
case 10:
Brea
S
IB
k;
case
50:
break;
R
case1
00:
Brea
k;
defa
ult:
return 0;
S
Sy
nta
lab
IB
el:
goto label;
ExampleProgra
R
m
#include<stdio.
h>int main()
int num,i=1;
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;
printf("%d
",i);
break;
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
//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
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
// code to be executed
S
IB
R
ExampleProgr
am
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
Program
#include<stdio.
R
h>int main(){
int i=1;
while(i<=10
){
printf("%d\n
",i); i++;
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
#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.
Orders Of Growth
S
● These notations provide a concise way to express the behavior of an
infinity.
IB
Asymptotic Notations
R
The commonly used asymptotic notations used for calculating the running time complexity
of an algorithm are given below:
S
IB
R
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:
S
IB
R
● It basically describes the best scenario, which is opposite to the bigo
notation.
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:
S
IB
R
● Big theta is mainly used when the value of the worst-case and the
best-case is the same.
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.