Pages

Jun 21, 2012

wap in c++ to merge two sorted arrays.


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int a[10],b[10],c[20];
int i,j,k,m,n;
cout<<"\nNo. of elementin A\t";
cin>>n;
cout<<"\nEnter the element in array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nNo. of elementin B\t";
cin>>m;
cout<<"\nEnter the element in array\n";
for(j=0;j<m;j++)
{
cin>>b[j];
}
i=0;j=0;k=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
k++;
}


if(i>=n)
{
for(int p=j;p<m;p++)
{
c[k]=b[p];
k++;
}
}
else
{
for(int p=i;p<n;p++)
{
c[k]=a[p];
k++;
}
}
cout<<"\nMerged array\n";
for(i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
getch();
}

Jun 20, 2012

wap in c++ to implement link-list.

// Following operation has been done:
1. Create
2. Insert at Beginning
3. Traverse
4. Insert at end
5. Search
6. Delete from Beginning




 #include<iostream.h>
 #include<conio.h>
 #include<alloc.h>
 #include<process.h>
 struct node
 {
 int info;
 struct node *link;
 };

 void main()
 {
 clrscr();
 void create(struct node **);
 void traverse(struct node *);
 void insbeg(struct node **,int);
 void search(struct node *,int);
 void delbeg(struct node **);
 void insend(struct node **,int);

 int item,item1,item2,choice;
 struct node *start,*ptr;

 do
{
cout<<"\n1. Create:"<<"\n2. Traverse:"
<<"\n3. Insbeg:"<<"\n4. Insend:"
<<"\n5. Search"<<"\n6. Delbeg:"
<<"\n7. Exit:\n";

cout<<"Enter ur choice:\t";
cin>>choice;
switch(choice)
{
case 1:create(&start);
break;
case 2:traverse(start);
break;
case 3:cout<<"\nenter the item to be insert:\t";
cin>>item;
insbeg(&start,item);
break;
case 5:cout<<"\nenter item to be search:\t";
cin>>item1;
search(start,item1);
break;
case 4:cout<<"\nenter item to be insert:\t";
cin>>item2;
 insend(&start,item2);
break;
case 6:delbeg(&start);
break;
}
}
 while(choice!=7);
 getch();
 }

 void create(struct node **start)
 {
 *start=NULL;
 cout<<"\nLinklist created sucessfully!\n";
 }

void traverse(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->info<<endl;
ptr=ptr->link;
}
}

 void insbeg(struct node **start,int item)
 {
 struct node *ptr;
 ptr=(struct node *)malloc(sizeof(struct node *));
 ptr->info=item;
 if(*start==NULL)
 {
 ptr->link=NULL;
 *start=ptr;
 }
 else
 {
 ptr->link=*start;
 *start=ptr;
 }
 }

void search(struct node *start,int item1)
{
struct node *ptr;
ptr=start;
if(start==NULL)
{
cout<<"\nEmpty List\n";
getch();
exit(0);
}
else
{
while(ptr!=NULL)
{
if(ptr->info==item1)
{
cout<<"\nFound at "<<ptr;
getch();
exit(0);
}
else
{
ptr=ptr->link;
}
}

cout<<"\nElement not present\n";
}
}
 void insend(struct node **start,int item2)
 {
 struct node *ptr1,*ptr2,*save;
 ptr1=(struct node *)malloc(sizeof(struct node *));
 ptr1->info=item2;
 ptr1->link=NULL;
 ptr2=*start;
 while(ptr2!=NULL)
 {
 save=ptr2;
 ptr2=ptr2->link;
 }
 save->link=ptr1;
 }

void delbeg(struct node **start)
{
struct node *ptr;
ptr=*start;
if(*start==NULL)
{
cout<<"\nUNDERFLOW CASE\n";
getch();
exit(0);
}
else
{
*start=(*start)->link;
cout<<"\nInfo deleted\t"<<ptr->info;
cout<<endl;
}
}