All Ds Solution
All Ds Solution
All Ds Solution
void insert::display()
{
for(i=1;i<=6;i++)
{
cout<<"\n\t\t "<<data[i];
}
}
int main()
{
insert d;
d.getdata();
d.atlocation();
d.display();
getch();
return 0;
}
/*
OUTPUT==>
Enter the element in an array ==>
10
20
30
40
50
At which position you want to insert new element ==> 3
Which element you want to insert ==> 13
10
20
13
30
40
50
*/
main()
{
deletion d;
d.getdata();
d.atlocation();
d.display();
getch();
}
/*
OUTPUT
Enter the element in an array ==>
10
20
30
40
50
From which position you want to delete an element ==> 3
After deletion ==>
10
20
40
50
*/
/*
OUTPUT==>
Enter the stack limit==>6
Enter the element==>10
top==>1
Enter the element==>20
top==>2
Enter the element==>30
top==>3
Enter the element==>40
top==>4
Enter the element==>50
top==>5
Enter the element==>60
top==>6
Stack has values==>
60
50
40
30
20
10
*/
inserted:3
1.push 2.pop 3.display 4.exit
Enter ur choice:2
deleted:3
1.push 2.pop 3.display 4.exit
Enter ur choice:1
enter the element:5
inserted:5
1.push 2.pop 3.display 4.exit
Enter ur choice:3
52
1.push 2.pop 3.display 4.exit
Enter ur choice:4
data[rear]=ele;
}
}
void queue::display()
{
cout<<"\n\n Queue has elements ==>\n\n";
for(i=1;i<=n;i++)
{
cout<<"| "<<data[i];
}
}
main()
{
clrscr();
int i;
queue q;
q.front=0;
q.rear=0;
q.getdata();
q.insert();
q.display();
getch();
}
Output
Enter the limit of queue 4
Enter the element ==> 1
Enter the element ==> 2
Enter the element ==> 3
Enter the element ==> 4
Sorry, Queue is full
Queue has elements ==>
| 1| 2| 3| 4
else
{
front=front+1;
cout<<"\n\n Queue Status ==> ";
for(j=front;j<=rear;j++)
{
cout<<" "<<data[j];
}
}
}
}
}
main()
{
clrscr();
queue q;
q.getdata();
q.deletion();
getch();
return(0);
}
/* OUTPUT :Enter the Limit ==> 5
Insert elements ==
41
52
63
78
96
Queue Status ==>
41 52 63 78 96
52 63 78 96
63 78 96
78 96
96
12 32 45 65 78 96
32 45 65 78 96
45 65 78 96
65 78 96
78 96
96
abc= f.fact(f.num);
cout<<" Factorial ==> "<<abc;
//f.display();
getch();
return(0);
}
/* Output :Enter the number 6
The factorial of 6 is 720.
*/
int abc;
f.getdata();
abc= f.fact(f.num);
cout<<" Factorial ==> "<<abc;
//f.display();
getch();
return(0);
}
Output ==>
1
10
10
12
14
16
18
20
12
15
18
21
24
27
30
12
16
20
24
28
32
36
40
10
15
20
25
30
35
40
45
50
12
18
24
30
36
42
48
54
60
14
21
28
35
42
49
56
63
70
16
24
32
40
48
56
64
72
80
18
27
36
45
54
63
72
81
90
10
20
30
40
50
60
70
80
90
100
f.getdata();
f.process(f.n);
getch();
}
/* OUTPUT :Enter number of terms find fibonacci ==> 6
0
13
DS 10 : Write an algorithm, draw a flowchart and write a program in C++ for linear
search.
#include<iostream.h>
#include<conio.h>
class linear
{
public:
int data[8],i,element,loc;
void getdata();
void search();
void display();
};
void linear::getdata()
{
clrscr();
cout<<"\n\n\t\tEnter the elements in an array \n";
for(i=1;i<=5;i++)
{
cout<<"\t\t";
cin>>data[i];
}
cout<<"\n\n\t\twhich element you want to search ==> ";
cin>>element;
}
void linear::search()
{
loc=0;
for(i=1;i<=8;i++)
{
if(data[i]==element)
{
loc=i;
break;
}
}
}
void linear::display()
{
if(loc==0)
{
cout<<"\n\n\t\tSorry,"<<element<<" is not present";
}
else
{
cout<<"\n\n\t\t"<<element<<" is present at location "<<loc;
}
}
main()
{
linear l;
l.loc=0;
l.getdata();
l.search();
l.display();
getch();
return 0;
}
/* OUTPUT ==> 1
Enter the elements in an array
11
4
55
22
66
which element you want to search ==> 55
55 is present at location 3
OUTPUT ==> 2
Enter the elements in an array
1
44
11
55
22
which element you want to search ==> 100
Sorry,100 is not present
*/
DS 11 : Write an algorithm, draw a flowchart and write a program in C++ for binary
search.
#include<iostream.h>
#include<conio.h>
class BinSearch
{
public:
int beg,end,mid,i,data[10],loc,elmt;
void getValue();
void calResult();
void showResult();
};
void BinSearch::getValue()
{
cout<<"Enter values in Array==>";
for(i=1;i<=7;i++)
{
cin>>data[i];
}
}
void BinSearch::calResult()
{
beg=1,end=7,mid=0,loc=0;
cout<<"Enter the element u want to search==>";
cin>>elmt;
for(i=1;i<=7;i++)
{
mid = int(beg+end)/2;
if(data[mid]==elmt)
{
loc = mid;
break;
}
else
{
if(data[mid]>elmt)
{
end=mid-1;
}
else
{
beg=mid+1;
}
}
}
}
void BinSearch::showResult()
{
if(loc==0)
{
cout<<" Sorry, Element not found ";
}
else
{
cout<<"Element found at==>"<<loc;
}
}
main()
{
clrscr();
BinSearch bs;
bs.getValue();
bs.calResult();
bs.showResult();
getch();
}
/* OUTPUT :Enter values in Array==>10
20
30
40
50
60
70
Enter the element u want to search==>50
Element found at==>5
*/
DS 1 2: Write an algorithm, draw a flowchart and write a program in C++ for bubble
sort.
#include<iostream.h>
#include<conio.h>}
class BubblSort
{
public:
int data[10],j,i,ptr,limit;
void getValue();
void calSorting();
void showResult();
};
void BubblSort::getValue()
{
cout<<"\n\n\tEnter limit for Array==>";
cin>>limit;
cout<<"\n\n\tEnter values in Array==>";
for(i=1;i<=limit;i++)
{
cout<<"\n\n\t";
cin>>data[i];
}
}
void BubblSort::calSorting()
{
for(j=1;j<=limit-1;j++)
{
ptr=1;
while(ptr<=limit-j)
{
if(data[ptr]>data[ptr+1])
{
int temp = data[ptr];
data[ptr] = data[ptr+1];
data[ptr+1] = temp;
}
ptr = ptr+1;
}
cout<<"\n\n\tPass : "<<j<<" ==> ";
for(i=1;i<=limit;i++)
{
cout<<"\t"<<data[i];
}
}
}
void BubblSort::showResult()
{
cout<<"\n\n\tArray after Sorting....";
cout<<"\n\n\n";
for(i=1;i<=limit;i++)
{
cout<<"\t"<<data[i];
}
}
main()
{
clrscr();
BubblSort bs;
bs.getValue();
bs.calSorting();
bs.showResult();
getch();
}
11
20
36
45
Pass : 2 ==>
11
20
36
45
Pass : 3 ==>
11
20
36
45
Pass : 4 ==>
11
20
36
45
11
20
36
45
DS 13: Write an algorithm, draw a flowchart and write a program in C++ for selection
sort.
#include<iostream.h>
#include<conio.h>
class selection
{
public:
int data[10],k,j,loc,temp,n,min;
void getdata();
void sort();
void minimum();
};
void selection::getdata()
{
cout<<"\t\t Enter the value of N ==> ";
cin>>n;
for(k=1;k<=n;k++)
{
cout<<"\t\t";
cin>>data[k];
}
}
void selection::sort()
{
for(k=1;k<=n-1;k++)
{
minimum();
temp=data[k];
data[k]=data[loc];
data[loc]=temp;
cout<<"\n Pass : "<<k<<" ==> ";
for(j=1;j<=n;j++)
{
cout<<"\t"<<data[j];
}
getch();
}
}
void selection::minimum()
{
min=data[k];
loc=k;
for(j=k+1;j<=n;j++)
{
if(min>data[j])
{
min=data[j];
loc=j;
}
}
}
main()
{
clrscr();
selection s;
s.getdata();
s.sort();
getch();
return(0);
}
/* OUTPUT :Enter the value of N ==> 8
77
33
44
11
88
22
66
55
Pass : 1 ==>
Pass : 2 ==>
Pass : 3 ==>
Pass : 4 ==>
Pass : 5 ==>
Pass : 6 ==>
Pass : 7 ==>
11
11
11
11
11
11
11
33
22
22
22
22
22
22
44
44
33
33
33
33
33
77
77
77
44
44
44
44
88
88
88
88
55
55
55
22
33
44
77
77
66
66
66
66
66
66
66
77
77
55
55
55
55
88
88
88
DS 14 : Write an algorithm, draw a flowchart and write a program in C++ for insertion
Sort.
#include<iostream.h>
#include<conio.h>
class sort
{
public:
int data[10],i,temp,ptr,n,j;
void getdata();
void sorting();
};
void sort::getdata()
{
cout<<"\n\t\t Enter the limit ==> ";
cin>>n;
cout<<"\n\t\t Insert Elements ==> \n\n";
for(i=1;i<=n;i++)
{
cout<<"\t\t";
cin>>data[i];
}
}
void sort::sorting()
{
data[0]=-10; // Insert a smallest value.
for(i=2;i<=n;i++)
{
temp=data[i];
ptr=i-1;
while(temp<data[ptr])
{
data[ptr+1]=data[ptr];
ptr=ptr-1;
}
data[ptr+1]=temp;
cout<<"\n\n Pass : "<<i-1<<"==> ";
for(j=1;j<=n;j++)
{
cout<<" "<<data[j];
}
getch();
}
}
main()
{
clrscr();
sort s;
s.getdata();
s.sorting();
getch();
return(0);
}
/* OUTPUT :Enter the limit ==> 5
Insert Elements ==>
4
5
23
6
1
Pass : 1==>
4 5 23 6 1
Pass : 2==>
4 5 23 6 1
Pass : 3==>
4 5 6 23 1
Pass : 4==>
1 4 5 6 23
*/
/* OUTPUT :Enter the limit ==> 8
Insert Elements ==>
77
33
44
11
88
22
66
55
Pass : 1==>
33 77 44 11 88 22 66 55
Pass : 2==>
33 44 77 11 88 22 66 55
Pass : 3==>
11 33 44 77 88 22 66 55
Pass : 4==>
11 33 44 77 88 22 66 55
Pass : 5==>
11 22 33 44 77 88 66 55
Pass : 6==>
11 22 33 44 66 77 88 55
Pass : 7==>
*/
11 22 33 44 55 66 77 88
DS 15: Write an algorithm, draw a flowchart and write a program in C++ for merge
sort.
#include<iostream.h>
#include<conio.h>
class merge
{
public :
int a[10],b[10],c[10],na,nb,nc,ptr,r,s,i;
void getdata();
void sorting();
void display();
};
void merge::getdata()
{
cout<<"\n\t\t Enter the limit of 1st array ==> ";
cin>>r;
cout<<"\n\t\t Enter 1st array ==> \n\n";
for(i=1;i<=r;i++)
{
cout<<"\t\t";
cin>>a[i];
}
cout<<"\n\t\t Enter the limit of 2nd array ==> ";
cin>>s;
cout<<"\n\t\t Enter 2nd array ==> \n\n";
for(i=1;i<=s;i++)
{
cout<<"\t\t";
cin>>b[i];
}
}
void merge::sorting()
{
na=1;nb=1;ptr=1;
while((na<=r)&&(nb<=s))
{
if(a[na]<b[nb])
{
c[ptr]=a[na];
ptr=ptr+1;
na=na+1;
}
else
{
c[ptr]=b[nb];
ptr=ptr+1;
nb=nb+1;
}
}
if(na>r)
{
for(i=0;i<=s-nb;i++)
{
c[ptr+i]=b[nb+i];
}
}
else
{
for(i=0;i<=r-na;i++)
{
c[ptr+i]=a[na+i];
}
}
}
void merge::display()
{
cout<<"\n\n\t\t Array after merge sort ==> \n\n";
for(i=1;i<=r+s;i++)
{
cout<<"\n\t\t"<<c[i];
}
}
main()
{
clrscr();
merge m;
m.getdata();
m.sorting();
m.display();
getch();
return(0);
}
/* OUTPUT :Enter the limit of 1st array ==> 5
Enter 1st array ==>
1
3
5
7
9
Enter the limit of 2nd array ==> 5
Enter 2nd array ==>
2
4
6
8
10
Array after merge sort ==>
1
2
3
4
5
6
7
8
9
10
*/
/* OUTPUT :Enter the limit of 1st array ==> 5
Enter 1st array ==>
5
10
15
20
25
Enter the limit of 2nd array ==> 5
Enter 2nd array ==>
4
8
16
32
64
Array after merge sort ==>
4
5
8
10
15
16
20
25
32
64
*/
DS 16 : Write an algorithm, draw a flowchart and write a program in C++ for Quick
sort.
#include<process.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class quicks
{
public:
int a[20],top,loc,beg,end,n;
int lower[20],upper[20];
void getdata();
void display();
void quick();
void quicksort();
};
void quicks::getdata()
{
cout<<"\n\t\tEnter number of elements ==> ";
cin>>n;
cout<<"\n\t\tEnter Elements :\n";
for(int i=0;i<n;i++)
{
cout<<"\t\t\t\t";
cin>>a[i];
}
}
void quicks::display()
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
void quicks::quick()
{
int temp,left,right,flag;
left=beg;
right=end;
loc=beg;
flag=0;
while(flag!=1)
{
while(a[loc]<=a[right]&&loc!=right)
{
right--;
}
if(loc==right)
{
flag=1;
return;
}
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
}
while(a[loc]>=a[left]&&loc!=left)
{
left++;
}
if(loc==left)
{
flag=1;
return;
}
if(a[loc]<a[left])
{
temp=a[loc];
a[loc]=a[left];
a[left]=temp;
loc=left;
}
}
}
void quicks::quicksort()
{
top++;
lower[top]=0;
upper[top]=n-1;
while(top!=-1)
{
beg=lower[top];
end=upper[top];
top--;
quick();
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
}
void main()
{
clrscr();
quicks ob;
ob.top=-1;
ob.getdata();
cout<<"\n\n\t\tBefore sorting ==> ";
ob.display();
cout<<"\n\n\t\tAfter sorting ==> ";
ob.quicksort();
ob.display();
getch();
}
22
99
101
75
26
15
Before sorting ==> 44 66 22 99 101 75 26 15
After sorting ==> 15 22 26 44 66 75 99 101
*/