Bca 3rd Sem Data St.
Bca 3rd Sem Data St.
Bca 3rd Sem Data St.
Department of BCA
Data Structure
( For BCA III SEM )
1
PRACTICAL 1
WAP to store records of 100 students using array?
#include<iostream.h>
#include<conio.h>
void main()
{
struct student
{
int roll_no;
char name[20];
char address[30];
int age;
}; 2
struct student s[100];
int i,n;
clrscr();
cout<<"enter no. of student";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter record of "<<i<<" student :\n";
cout<<"enter roll_no.=";
cin>>s[i].roll_no;
cout<<"enter name=";
cin>>s[i].name;
cout<<"enter address=";
cin>>s[i].address;
cout<<"enter age=";
cin>>s[i].age;
cout<<endl; 3
/*for displaying records */
cout<<"***Records of all student***\n";
for(i=1;i<=n;i++)
{
cout<<"record of "<<i<<" student is:\n";
cout<<"Roll no.="<<s[i].roll_no<<"\n";
cout<<"Name=" <<s[i].name<<"\n";
cout<<"Address="<<s[i].address<<"\n";
cout<<"Age="<<s[i].age<<"\n"<<"\n";
}
getch();
}
4
5
PRACTICAL 2
WAP for sorting given element of an array using
Selection Sort?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,j,min,pos,temp;
cout<<"\n----------SELECTION SORT ----------\n\n";
cout<<"Enter No. of Elements=";
cin>>n;
cout<<"\nEnter Elements=\n";
6
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n;i++) // Loop for Pass
{
min=a[i]; // Element Selection
pos=i;
for(j=i+1;j<=n;j++) // Finding Min Value
{
if(a[j]<min)
{
min=a[j];
pos=j;
}
7
}
temp=a[i]; // Swap Selected Element and Min Value
a[i]=a[pos];
a[pos]=temp;
}
cout<<"\nAfter Sorting : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
8
PROGRAM -3
WAP for sorting given element of an array using Bubble
Sort?
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,n,j,temp;
clrscr();
cout<<"enter no.";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
} 9
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
cout<<"sorted array:";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
} 10
PROGRAM -4
WAP for sorting given element of an array using Insertion
Sort ?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,n,temp,a[30];
cout<<"enter the total number of element";
cin>>n;
cout<<"enter elements:"
for(i=0;i<n;i++)
{
cin>>a[i];
11
}
for(i=1;i<n;i++)
{
for(j=i;j>=1;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
else
{
break;
}
}
} 12
cout<<"sorted array";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}
13
PROGRAM -5
WAP for searching a given no. using Linear Search?
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,n,search,flag=0;
clrscr();
cout<<"enter no.";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<i<<"=";
cin>>a[i];
}
cout<<"\n enter search element=";
14
cin>>search;
for(i=1;i<=n;i++)
{
if(search==a[i])
{
Cout<<”Element found”;
flag=1;
break;
}
}
if(flag==0)
{
cout<<"\n element not found";
}
else
{
cout<<"\n element found at="<<i<<"\n Element is="<<a[i];
}
getch(); 15
PROGRAM -6
WAP for searching a given no. using Binary Search?
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,n,beg,mid,end,flag=0,search;
clrscr();
cout<<"enter no.:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<i<<"=";
cin>>a[i];
} 16
cout<<"\n enter search element=";
cin>>search;
beg=0;
end=n-1;
for(i=0;i<n;i++)
{
mid=(beg+end)/2;
if(search==a[mid])
{
flag=1;
break;
}
else
{
if(search>a[mid])
beg=mid+1;
else
end=mid-1; 17
}
}
if(flag==0)
{
cout<<"\n element not found";
}
else
{
cout<<"\n element found at location="<<mid+1<<"\n
Element is="<< search;
}
getch();
}
18
PRACTICAL 7
WAP for Representation of Upper Traingular & Lower
Traingular Matrix in Linear Search ?
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],r,c,i,j;
clrscr();
do
{
cout<<"enter the row & column of matrix";
cin>>r>>c;
if(r==c)
{ 19
cout<<"enter element of array"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"enter integer element";
cin>>a[i][j];
}
}
cout<<"\n upper traingular matrix = \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(j>i)
{
cout<<a[i][j]<<"\t";
} 20
else
{
cout<<"\t";
}
}
cout<<"\n";
}
cout<<"\n Lower traingular matrix = \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(j<i)
{
cout<<a[i][j]<<"\t";
} 21
else
{
cout<<"\t";
}
}
cout<<"\n";
}
}
else
{
cout<<"\n please enter square matrix \n";
}
}while(r!=c);
getch();
} 22
PRACTICAL 8
WAP for Implementation of Stack(Push, Pop, Traversing ) ?
#include<iostream.h>
#include<conio.h>
#define size 5
class stack
{
int stk[size],top,item,i;
public:
void push();
void pop();
void traverse();
stack()
{
top=-1;
}
23
};
void stack::push()
{
if(top==size-1)
{
cout<<"Stack overflow";
return;
}
else
{
cout<<"\n Enter element =";
cin>>item;
top++;
stk[top]=item;
cout<<"\n element inserted";
}
24
}
void stack::pop()
{
if(top==-1)
{
cout<<"\n Stack overflow";
return;
}
else
{
item=stk[top];
top--;
cout<<"\n deleted item="<<item;
}
}
25
void stack::traverse()
{
if(top==-1)
{
cout<<"Stack underflow";
return;
}else
{
cout<<"stack element are=\n";
for(i=0;i<=top;i++)
{
cout<<stk[i]<<endl;
}
}
} 26
void main()
{
int c;
char ch;
clrscr();
stack s;
do
{
clrscr();
cout<<"\n 1:Insertion \n 2:Deletion \n 3:Traverse \n 4:Exit";
cout<<"\n Enter your choice :";
cin>>c;
switch (c)
{
case 1:
s.push();
27
break;
case 2:
s.pop();
break;
case 3:
s.traverse();
break;
case 4:
cout<<"Exit \n";
return;
default:
cout<<"Invalid choice";
}
cout<<"\n do u want to continue(y/n)=";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch(); 28
}
29
PRACTICAL 9
WAP for implementation of Queue(Addition, Deletion , Traversing) ?
#include<iostream.h>
#include<conio.h>
#define size 5
class queue
{
int q[size],f,r,item,i;
public:
void ins();
void del();
void traverse();
queue()
{
f=0,r=-1;
}
}; 30
void queue::ins()
{
if(r==size-1)
{
cout<<"\n Queue is overflow";
return;
}
else
{
cout<<"\n Enter element =";
cin>>item;
r++;
q[r]=item;
cout<<"\n element inserted";
31
if(f==-1)
{
f=0;
}
}
}
void queue::del()
{
if(r==-1)
{
cout<<"\n Queue is empty";
return;
}
else
{
item=q[f];
cout<<"\n deleted item is = "<<item;
if(f==r)
{
f=r=-1;
}
32
else
{
f++;
}
}
}
void queue::traverse()
{
if(r==-1)
{
cout<<"Queue is underflow \n";
return;
}
else
{
cout<<"element of queue are=\n";
33
for(i=f;i<=r;i++)
{
cout<<"\t"<<q[i]<<endl;
}
}
}
void main()
{
int c;
char ch;
clrscr();
queue q;
do
{
clrscr();
cout<<"\n 1:Insertion \n 2:Deletion \n 3:Traverse \n 4:Exit";
cout<<"\n Enter your choice :";
cin>>c; 34
switch (c)
{
case 1:
q.ins();
break;
case 2:
q.del();
break;
case 3:
q.traverse();
break;
case 4:
cout<<"Exit \n";
return;
default:
cout<<"Invalid choice";
}
35
cout<<"\n do u want to continue(y/n)=";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}
36
37
38
PRACTICAL 10
WAP Exchanging value of two variables using Pointer?
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void main()
{
clrscr();
int x,y;
cout<<"Enter the value of x & y =\n";
39
cin>>x>>y;
cout<<"Values before exchange \n";
cout<<"value of x="<<x<<endl;
cout<<"value of y="<<y<<endl;
cout<<"Values after exchange\n";
swap(&x,&y);
cout<<"value of x="<<x<<endl;
cout<<"value of y="<<y;
getch();
}
40