Data Structure and Algorithm Lab Programs
Data Structure and Algorithm Lab Programs
LAB PROGRAMS
#include<iostream>
using namespace std;
int main()
{
int arr[50],i,j,n,k,loc;
cout<<"Enter no. of elements in array";
cin>>n;
cout<<"\nEnter array:\n";
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"\nEnter the position and element to insert:\n";
cin>>loc>>k;
for(i=n-1;i>=loc-1;i--)
arr[i+1]=arr[i];
arr[loc]=k;n=n+1;
cout<<"The modified array is:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<endl;
}
Q2.Write a program for deletion in array.
#include<iostream>
using namespace std;
int main()
{
int arr[50],i,n,loc;
cout<<"Enter no. of elements in array";
cin>>n;
cout<<"\nEnter array:\n";
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"\nEnter the position of element to delete:\n";
cin>>loc;
for(i=loc-1;i<n;i++)
arr[i]=arr[i+1];
n=n-1;
cout<<"The modified array is:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<endl;
}
Q3.Write a program to sort an array.
#include<iostream>
using namespace std;
int main()
{
int arr[50],i,n,j,t;
cout<<"Enter no. of elements in array";
cin>>n;
cout<<"\nEnter array:\n";
for(i=0;i<n;i++)
cin>>arr[i];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[j]>arr[i])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
cout<<"The modified array is:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<endl;
}
Q4.Write a program to count frequency of elements.
#include<iostream>
using namespace std;
int main()
{
int arr[50],i,n,j,count;
}
}
cout<<"Frequency of element"<<arr[i]<<"is:
"<<count<<endl;
}
}
Q5.Write a program to reverse array without using extra
storage.
#include<iostream>
using namespace std;
int main()
{
int arr[50],i,n,mid,j;
cout<<"Enter no. of elements in array";
cin>>n;
cout<<"\nEnter array:\n";
for(i=0;i<n;i++)
cin>>arr[i];n=n-1;
if(n%2==0)
mid=n/2;
else
mid=n/2+1;
for(i=0;i<mid;i++)
{
arr[i]=arr[i]+arr[n-i];
arr[n-i]=arr[i]-arr[n-i];
arr[i]=arr[i]-arr[n-i];
}
}
Q6.Write a program to find multiplication of matrices.
#include<iostream>
using namespace std;
int main()
{
int a[3][3],b[3][3],i,j,k,c[3][3];
cout<<"Enter first matrix:\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\nEnter second matrix:\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
//Multiplication
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{ c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}
#include<iostream>
using namespace std;
int main()
{
int a[10],i,j,beg,en,mid,n,f,c;
beg=0;en=9;j=0;c=0;f=0;
cout<<"Enter array";
for(i=0;i<10;i++)
cin>>a[i];
if(a[mid]==n)
cout<<"\nNo. found after "<<j<<" iteration";
else
cout<<"\nNo. not found after "<<j<<" iteration";
}
LINKED LIST
Q1.Write a program to create and traverse a singly linked
list.
#include<iostream>
using namespace std;
struct node
{
int info;
node *ptr;
};
node *head,*start,*temp;
void create(int n)
{
head=new node;
head->info=n;
if(start==NULL)
{
start=temp=head;
}
else
{
temp->ptr=head;
temp=head;
}
}
void display()
{
temp=start;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->ptr;
}
}
int main()
{
int n,ch,m;
ch=1;
while(ch)
{
cout<<"Enter value to store"<<endl;
cin>>n;
create(n);
cout<<"Want to create more :0 or 1\n";
cin>>ch;
}
cout<<"\nValues in linked list are\n";
display();
}
int main()
{
int n,ch,m,t;
ch=1;
t=0;
while(ch)
{
cout<<"Enter value to store"<<endl;
cin>>n;
create(n,t);
t=n;
cout<<"Want to create more :0 or 1\n";
cin>>ch;
}
cout<<"\nValues in linked list are\n";
display();
}
}
cout<<"\nWant to swap non consecutive\n";
cin>>ch;
if(ch==1)
{
cout<<"Enter m and n";
cin>>m>>n;
swapp(m,n);
}
}
Q7.Write a program to split a linked list into even and
odd linked lists.
#include<iostream>
using namespace std;
struct node
{
int info;
node *ptr;
};
node
*head,*start,*temp,*starteven,*startodd,*tempeven,*te
mpodd,*heade,*heado;
void create(int n)
{
head=new node;
head->info=n;
if(start==NULL)
{
start=temp=head;
}
else
{
temp->ptr=head;
temp=head;
}
}
void display(node *start,node *temp)
{
temp=start;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->ptr;
}
}
void splitt()
{ cout<<"split started";
temp=start;
if(temp==NULL)
cout<<"Underflow";
else
{
while(temp!=NULL)
{
if(temp->info%2==0)
{
heade=temp;
if(starteven==NULL)
{
starteven=tempeven=heade;
}
else
{
tempeven->ptr=heade;
tempeven=heade;
}
}
else
{
heado=temp;
if(startodd==NULL)
{
startodd=tempodd=heado;
}
else
{
tempodd->ptr=heado;
tempodd=heado;
}
}
temp=temp->ptr;
}
cout<<"\nLinked list with even no. is\n";
display(starteven,tempeven);
cout<<"\nLinked list with odd no, is\n";
display(startodd,tempodd);
}}
int main()
{
int n,ch,m;
ch=1;
while(ch)
{
cout<<"Enter value to store"<<endl;
cin>>n;
create(n);
cout<<"Want to create more :0 or 1\n";
cin>>ch;
}
cout<<"\nValues in linked list are\n";
display(start,temp);
splitt();
}
if(start==NULL)
{
start=temp=ptr;
}
else
{
pre=temp;
temp->next=ptr;
temp=ptr;
temp->next=NULL;
}
}
void display()
{ temp=start;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->next;
}
}
void revers()
{
temp=start;
nxt=NULL;
pre=NULL;
while(temp!=NULL)
{
nxt=temp->next;
temp->next=pre;
pre = temp;
temp=nxt;
}
start=pre;
display();
int main()
{
int num,loc,y,c;
cout<<"press 1 for continue & 0 for exit \n";
cout<<"enter ur choice \n";
cin>>y;
while(y==1)
{
cout<<"enter the information \n";
cin>>num;
create(num);
cout<<"enter ur choice \n";
cin>>y;
}
display();
cout<<"list after reverse \n";
revers();
void display(Node*);
void display(Node*);
void insert(Node*);
void concat(Node*,Node*);
int main()
{
int i,j,k,p,q;
char ch='y';
cout<<"\nNODE1\n";
while(ch=='y'|| ch=='Y')
{
cout<<"\nEnter the info:";
cin>>b.info;
newptr=(Node*)malloc(sizeof(Node));
if(newptr==NULL)
cout<<"\nNode cannot be created\n";
else
cout<<"\nNode created\n";
newptr->info=b.info;
newptr->next=NULL;
cout<<"\nInsert new node at beginning\n";
if(start==NULL)
start=newptr;
else
{
save=start;
start=newptr;
newptr->next=save;
}
cout<<"\nWant to enter more elements,press y or
Y:";
cin>>ch;
}
cout<<"\nNODE2\n";
ch='y';
while(ch=='y'|| ch=='Y')
{
cout<<"\nEnter the info:";
cin>>b1.info;
newptr1=(Node*)malloc(sizeof(Node));
if(newptr1==NULL)
cout<<"\nNode cannot be created\n";
else
cout<<"\nNode created\n";
newptr1->info=b1.info;
newptr1->next=NULL;
cout<<"\nInsert new node at beginning\n";
if(head==NULL)
head=newptr1;
else
{
save=head;
head=newptr1;
newptr1->next=save;
}
cout<<"\nWant to enter more elements,press y or
Y:";
cin>>ch;
}
display(newptr);
display(newptr1);
concat(newptr,newptr1);
}
void display(Node*newptr)
{
cout<<"\nDISPLAY\n";
if(newptr==newptr1)
newptr=head;
else
newptr=start;
while(newptr!=NULL)
{
cout<<newptr->info<<"->";
newptr=newptr->next;
}
}
void concat(Node*newptr,Node*newptr1)
{
newptr1=head;
newptr=start;
while(newptr->next!=NULL)
{
newptr=newptr->next;
}
newptr->next=head;
display(newptr);
}
if(newptr==NULL)
cout<<"\nNode cannot be created\n";
else
cout<<"\nNode created\n";
newptr->info=b.info;
newptr->next=NULL;
cout<<"\nInsert new node at beginning\n";
if(start==NULL)
start=newptr;
else
{
save=start;
start=newptr;
newptr->next=save;
}
cout<<"\nWant to enter more elements,press y or
Y:";
cin>>ch;
}
freq(newptr);
return 0;
}
void display(Node*newptr)
{
cout<<"\nDISPLAY\n";
newptr=start;
while(newptr!=NULL)
{
cout<<newptr->info<<"->";
newptr=newptr->next;
}
}
void freq(Node*newptr)
{
int c=0;
newptr=start;
ptr=newptr;
while(ptr!=NULL)
{
save=(Node*)malloc (sizeof(Node));
save=newptr->next;
newptr=start;
while(newptr!=NULL)
{
if(ptr->info==newptr->info)
c++;
newptr=newptr->next;
}
c=0;
newptr=save;
ptr=(Node*)malloc (sizeof(Node));
ptr=save;
}
}
if(start==NULL)
{
start=temp=ptr;
}
else
{
temp->next=ptr;
temp=ptr;
}
}
void display()
{
temp=start;
while(temp!=0)
{
cout<<temp->info<<endl;
temp=temp->next;
}
}
void del(int a)
{ pre=NULL;
temp=start;
for(int i=1;i<a-1;i++)
{
temp=temp->next;
}
pre=temp;
ptr=temp->next;
temp=ptr->next;
pre->next=temp;
delete []ptr;
display();
}
int main()
{
int num,loc,y,c;
cout<<"press 1 for continue & 0 for exit \n";
cout<<"enter ur choice \n";
cin>>y;
while(y==1)
{
cout<<"enter the information \n";
cin>>num;
create(num);
cout<<"enter ur choice \n";
cin>>y;
}temp->next=0;
display();
cout<<"enter the location and value \n";
cin>>loc;
del(loc);
}
SPARSE MATRICES
Q1. Write a program to enter and display sparse matrix.
Ans.
#include<iostream>
using namespace std;
int main()
{
int i,j,a[10][10],n,m,l,k=1;
cout<<"entr the row and column\n";
cin>>n>>m;
cout<<"enter the element of matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}
}
cout<<" your sparse matric is \n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=0)
{
m[k][0]=i;
m[k][1]=j;
m[k][2]=a[i][j];
k++;
l++;
}
}
}
m[0][0]=n;
m[0][1]=m;
m[0][2]=l;
cout<<"3 column representation is \n";
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
cout<<m[i][j];
}
cout<<"\n";
}
}
s=s+1;
else
cout<<0;
cout<<endl;
}
}
};
int main()
{
int k;
int p;
int c[10][3];
sparse b[2];
int x;
b[0].input();
b[0].spar();
if((b[0].t)>(b[1].t))
k=b[0].t;
else
k=b[1].t;
int s=0;
int z=0;
int f=0;
cout<<endl<<endl;
{
x=b[0].a[i][1];
b[0].a[i][1]=b[0].a[i][0];
b[0].a[i][0]=x;
}
cout<<b[0].a[q][2];
q++;
}
else
cout<<0;
}
cout<<endl;
}
}
}
cout<<endl;
}
}
};
int main()
{
int k;
int p;
int c[10][3];
sparse b[2];
int x;
b[0].input();
b[1].input();
b[0].spar();
b[1].spar();
if((b[0].t)>(b[1].t))
k=b[0].t;
else
k=b[1].t;
int s=0;
int z=0;
cout<<endl<<endl;
if(b[0].columns==b[1].rows)
cout<<"\nMULTIPLICATION POSSIBLE\n";
else
cout<<"\nMULTIPLICATION NOT POSSIBLE\n";
for(int i=0;i<b[0].rows;i++)
{
for(int j=0;j<b[1].columns;j++)
{
int sum=0;
for(int m=0;m<b[0].columns;m++)
{
if((b[0].a[z][0]==i && b[0].a[z][1]==m) &&
(b[1].a[z][0]==m && b[1].a[z][1]==j))
{
sum=sum+b[0].a[z][2]*b[1].a[z][2];
z++;
}
}
cout<<sum;
}
cout<<endl;
}
}