Data Structures and Algorithms Experiment 1
Data Structures and Algorithms Experiment 1
Experiment No. 01
PART A
A.1 Aim:
Implementation of various array operations like
Traversal
Insertion
Deletion
A.2 Prerequisite:
Basics of Array, C++
A.3 Outcome:
After successful completion of this experiment students will be able to:
1. Operations on arrays
2. Traversing
3. Insertion
4. Deletion
A.4 Theory:
Arrays:-
An array is defined as an ordered set of similar data items. All the data items of
an array are stored in consecutive memory locations in RAM. The elements of an
array are of same data type and each item can be accessed using the same name.
Array is a foundation of other data structures.
For example other data structures such as Linked List, Stack, and Queue etc. are
implemented using array.
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
BASIC OPERATIONS ON ARRAYS
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store:
**********************
#include<array>
class insert_into_arr{
public:
int arr[10],i,j,insert_num,insert_index;
void create_array(){
for(i=0;i<7;i++){
cin>>arr[i];
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}
void display_array(){
for(i=0;i<7;i++){
cout<<arr[i]<<" ";
void insert_new_number(){
cin>>insert_index;
cin>>insert_num;
for(i=8;i>=insert_index;i--){
arr[i+1]=arr[i];
arr[insert_index]=insert_num;
int s=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<8;i++){
cout<<arr[i]<<" ";
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
};
int main(){
insert_into_arr a1;
a1.create_array();
a1.display_array();
a1.insert_new_number();
return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
Deletion − Deletes an element at the given index
#include<iostream>
#include<array>
using namespace std;
class array_deletion{
public:
int i,j,arr[5],delete_ele,delete_ind,temp;
void create_arr(){
for(i=0;i<5;i++){
cin>>arr[i];
}
}
void display_arr(){
cout<<"Array before deletion:";
for(i=0;i<5;i++){
cout<<arr[i]<<" ";
}
}
void delete_element(){
cout<<"\nEnter the element you want to delete:";
cin>>delete_ele;
for(i=0;i<5;i++){
if(arr[i]==delete_ele){
temp=i;
for(i=temp;i<5;i++){
arr[i]=arr[i+1];
}
}
}
cout<<"Array after deletion:";
for(i=0;i<4;i++){
cout<<arr[i]<<" ";
}
}
};
int main(){
array_deletion d1;
d1.create_arr();
d1.display_arr();
d1.delete_element();
return 0;
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}
#include<iostream>
#include<array>
class array_deletion{
public:
int i,j,arr[5],del_ele;
void create_array(){
for(i=0;i<5;i++){
cin>>arr[i];
void display_array(){
for(i=0;i<5;i++){
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
cout<<arr[i]<<" ";
void delete_array_element_by_index(){
cin>>del_ele;
for(i=0;i<5;i++){
if(i==del_ele){
for(i=del_ele;i<5;i++){
arr[i]=arr[i+1];
for(i=0;i<4;i++){
cout<<arr[i]<<" ";
};
int main(){
array_deletion d1;
d1.create_array();
d1.display_array();
d1.delete_array_element_by_index();
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
return 0;
#include<array>
class array_traverse{
public:
int i,j,arr[10];
void create_array(){
for(i=0;i<5;i++){
cin>>arr[i];
void display_array(){
for(i=0;i<5;i++){
cout<<arr[i]<<" ";
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}
};
int main(){
array_traverse a1;
a1.create_array();
a1.display_array();
return 0;
array_updation u1;
u1.create_array();
u1.display_array();
u1.arr_updation();
return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
Search − Searches an element using the given index or by the
value
#include<iostream>
#include<array>
using namespace std;
class array_searching{
public:
int i,j,arr[10],search_ele,search_element_index;
void create_array(){
for(i=0;i<10;i++){
cin>>arr[i];
}
}
void display_array(){
cout<<"Original array is:";
for(i=0;i<10;i++){
cout<<arr[i]<<" ";
}
}
void search_element(){
cout<<"\nEnter the element you want to search:";
cin>>search_ele;
for(i=0;i<10;i++){
if(arr[i]==search_ele){
search_element_index=i;
break;
}
}
cout<<"\nThe entered element is present at "<<search_element_index<<" index
";
}
};
int main(){
array_searching s1;
s1.create_array();
s1.display_array();
s1.search_element();
return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
};
int main(){
array_application a1;
a1.create_array();
a1.display_array();
a1.insert_name();
a1.delete_name();
a1.search_name();
a1.update_nam();
return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
PART B