Data Structure with CPP
Bubble short in cpp
#include<iostream> // bubble short
using namespace std;
int main()
{
int i,j,temp;
int n[5];
cout<<"Enter array data";
for(i=0;i<5;i++)
{
cin>>n[i];
}
cout<<"unshorted array data \n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
cout<<endl;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(n[j]<n[i])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"shorted array data\n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
Selection Short in cpp.
the selection sort technique first selects the smallest element in the array and swaps it with the first element in the array. finding the smallest element in every pass and placing it in the correct position
#include < stdio.h>
int main()
{
int i,j,num,temp,p,min;
int arr[5];
printf("Enter The Number of element");
scanf("%d",&num);
printf("Enter The Elements");
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<num-1;i++)
{
min=arr[i];
p=i;
for(j=i+1;j<num;j++)
{
if(min>arr[j])
{
min=arr[j];
p=j;
}
}
temp=arr[i];
arr[i]=arr[p];
arr[p]=temp;
}
for(i=0;i<num;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
Insertion short
In this sorting technique, the elements are sorted by comparing the elements with their previous elements. It starts by comparing the second element with the first element. If the second element is smaller than the first, then we will swap it.
#include<iostream>
using namespace std;
int main()
{
int a[5],i,j,temp,min;
cout<<"enter the 5 number in an array"<<endl;
for(i=0;i<5;i++)
cin>>a[i];
cout<<"the list are "<<endl ;
for(i=0;i<5;i++)
cout<<a[i]<<endl;
for(i=0;i<5;i++)
{
temp=a[i],j=i-1;
while(temp<a[j] && j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"out put are"<<endl ;
for(i=0;i<5;i++)
cout<<a[i]<<endl;
return 0;
}
Binary Search program using array :
#include<iostream>
using namespace std;
int main()
{ //in binary search first check mid point is equal
//or not with item
int ar[50],item,beg,last,n,mid,x=0,l; // if item is greater then chenge mid with beg
cout<<"Enter Size of array (Max 50 )"; //if item is smaller then change mid with last
cin>>n;
cout<<"Enter array elements in asc order \n" ;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
cout<<"enter element to be searched for\n";
cin>>item;
beg=0;
last=n-1;
while(beg<=last && !x)
{
mid=(beg+last)/2;
if(item==ar[mid])
{
x=1;
l=mid;
}
else if(item>ar[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
if(x==1)
{
cout<<"found possition at "<<l+1;
}
else
{
cout<<"not found" ;
}
return 0;
}
Stack program using array :
#include<iostream>
using namespace std;
#define length 10
void push(int);
int pop();
void listStack();
int top=-1;
int stack[length];
int main()
{
int ch,data;
do{
cout << endl << "1: push";
cout << endl << "2: pop";
cout << endl << "3: exit";
cout << endl << "enter choice";
cin >> ch;
switch(ch)
{
case 1:
cout<<"enter number" ;
cin>>data;
push(data);
listStack();
break;
case 2:
cout << "data poped= " << pop();
listStack();
break;
case 3:exit(0);
}
} while(ch);
return 0;
}
void push(int data)
{
if(top+1==length)
{
cout << "stack overflow\n Cannot enter new data";
return;
}
top++;
stack[top]=data;
}
int pop()
{
int tempVar;
if(top==-1)//we can also make isEmpty()
{
cout << "stack is underflow (Empty)";
return(-1);
}
tempVar = stack[top];
top--;
return tempVar;
}
void listStack()
{
if(top<0)
{
cout <<" stack empty";
return;
}
cout << endl << "The stack is" << endl ;
for(int i=top;i>=0;i--)
cout << stack[i] << endl;
}
Queue program in cpp using array :
#include<iostream>
using namespace std;
int num[5]; //Maximum 5 numbers
int front=-1,rear=-1;
void qins(int n);
void qdel();
void qshow();
void qins(int n)
{
if (rear==5-1)
//queue is full
cout<<"Insertion not possible\n";
else
{
if (front==-1)
//Que empty
{
front++;
num[front]=n;
rear=front;
}
else
{
rear++; //Data inserted at the end
num[rear]=n;
}
}
}
void qdel()
{
if (front==-1)
cout<<"Que empty\n";
else
{
int n;
if (front==rear)
{
n=num[front];
cout<<"No. removed is "<<n<<endl;
front=rear=-1; //re-initializing for new entry of data
}
else
{
int n=num[front];
front++; //front position increased
cout<<"No. removed is "<<n<<endl;
rear--;
}
}
}
void qshow()
{
if (front==-1)
cout<<"Que is empty\n";
else
{
for(int i=front;i<=rear;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
}
int main()
{
int ch,n;
ch=0;
while (ch<4)
{
cout<<" 1.ins\n 2.del\n 3.show\n 4.exit";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter no";cin>>n;
qins(n);
break;
case 2: qdel();
break;
case 3: qshow();
}
}
return 0;
}
Bubble short
#include<iostream> // bubble short
using namespace std;
int main()
{
int i,j,temp;
int n[5];
cout<<"Enter array data";
for(i=0;i<5;i++)
{
cin>>n[i];
}
cout<<"unshorted array data \n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
cout<<endl;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(n[j]<n[i])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"shorted array data\n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
file handling
/* File handling program with Add, Read, Search, Update, and Delete functionalities */
# include <stdio.h>
# include <string.h>
int writedata();
int readdata();
int searchdata(int admNo);
int updatedata(int admNo);
int deletedata(int admNo);
int main()
{
int n, admNo;
char ch;
do
{
printf("\nPress 1 for writing");
printf("\nPress 2 for reading");
printf("\nPress 3 for searching by admission number");
printf("\nPress 4 for updating a record");
printf("\nPress 5 for deleting a record");
printf("\nPress 0 for exit");
scanf("%d", &n);
fflush(stdin);
switch(n)
{
case 1:
writedata();
break;
case 2:
readdata();
break;
case 3:
printf("Enter admission number to search: ");
scanf("%d", &admNo);
searchdata(admNo);
break;
case 4:
printf("Enter admission number to update: ");
scanf("%d", &admNo);
updatedata(admNo);
break;
case 5:
printf("Enter admission number to delete: ");
scanf("%d", &admNo);
deletedata(admNo);
break;
case 0:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice");
}
printf("\n\nIf you want to continue, press (y/n): ");
fflush(stdin);
scanf(" %c", &ch);
}
while(ch == 'Y' || ch == 'y');
return 0;
}
int readdata()
{
FILE *fp;
int admissionNo;
char name[50];
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("Could not open file test.txt\n");
return 1;
}
printf("\nAdmission Records:\n");
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
printf("Admission No: %d\tName: %s\n", admissionNo, name);
}
fclose(fp);
return 0;
}
int writedata()
{
FILE *fp;
int admissionNo;
char name[50];
fp = fopen("test.txt", "a");
if (fp == NULL)
{
printf("Could not open file test.txt\n");
return 1;
}
printf("\nEnter Admission Number: ");
scanf("%d", &admissionNo);
printf("Enter Name: ");
scanf("%s", name);
fprintf(fp, "%d %s\n", admissionNo, name);
fclose(fp);
return 0;
}
int searchdata(int admNo)
{
FILE *fp = fopen("test.txt", "r");
int admissionNo;
char name[50];
int found = 0;
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
if (admissionNo == admNo)
{
printf("Record Found: Admission No: %d\tName: %s\n", admissionNo, name);
found = 1;
break;
}
}
fclose(fp);
if (!found) printf("Record not found.\n");
return 0;
}
int updatedata(int admNo)
{
FILE *fp = fopen("test.txt", "r");
FILE *temp = fopen("temp.txt", "w");
int admissionNo;
char name[50], newName[50];
int found = 0;
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
if (admissionNo == admNo)
{
printf("Enter new name: ");
scanf("%s", newName);
fprintf(temp, "%d %s\n", admissionNo, newName);
found = 1;
}
else
{
fprintf(temp, "%d %s\n", admissionNo, name);
}
}
fclose(fp);
fclose(temp);
remove("test.txt");
rename("temp.txt", "test.txt");
if (found)
printf("Record updated successfully.\n");
else
printf("Record not found.\n");
return 0;
}
int deletedata(int admNo)
{
FILE *fp = fopen("test.txt", "r");
FILE *temp = fopen("temp.txt", "w");
int admissionNo;
char name[50];
int found = 0;
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
if (admissionNo == admNo)
{
found = 1;
continue; // Skip writing this record (deleting it)
}
fprintf(temp, "%d %s\n", admissionNo, name);
}
fclose(fp);
fclose(temp);
remove("test.txt");
rename("temp.txt", "test.txt");
if (found)
printf("Record deleted successfully.\n");
else
printf("Record not found.\n");
return 0;
}
Last Updated on : 9th Apr 2025 17:04:43 PM
Latest Update
- JavaScript Interview Question MongoDb Command Curd Operation Node MonogoDb Express EJSC Practice Question Example Data Structure with CPPCurd Operation with static array in node js and ejs filejava practice questionCurd operation with localstorage in javascriptComplete curd operation in react with json server and axios and router-domCPP Practice Question Java Pattern Program