All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Friday, 28 March 2014

Searching in Sorted Linked List (Ascending or Descending)

//Searching In Sorted Linked List

#include<iostream>
using namespace std;
struct node
{
 int info;
 node *link;
};
int main()
{
 node *start,*p,*pp,*temp;
 start=p=NULL;
 //Creating Linked List
 int n;
 cout<<"Press [0] to Exit :: Input Elements To List :";
 do
 {
    cin>>n;
  if(n)
  {
     temp=new node;
     temp->info=n;
     temp->link=NULL;
     if(start==NULL)
 start=temp;
     else
 p->link=temp;
     p=temp;
    }
 }while(n);
 //printing nodes
 p=start;
 cout<<"Created Nodes are : ";
 while(p)
 {
  cout<<p->info<<" ";
  p=p->link;
 }
//Check Accending or descending
pp=start;p=start->link;
if (pp->info<p->info)
{
cout<<"\nAsecnding list \n";

/////Search location
    cout<<"Enter Item to Search :";
    int item;
    cin>>item;
    int loc=0,k=1;
    if(item<p->info)
     {
  cout<<"Not Present" ;
  exit(0);
     }
while(p!=NULL)
{

if(item==p->info)
{
k++;
loc=k;
   break;
   }
else
  {
    p=p->link;
    k++;
  }
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}
else
{
cout<<"\nDescending List \n";
cout<<"Enter Item to Search :";
    int item;
    cin>>item;
    int loc=0,k=1;
    if(item>p->info)
     {
  cout<<"Not Present" ;
  exit(0);
     }
while(p!=NULL)
{

if(item==p->info)
{
k++;
loc=k;
   break;
   }
else
  {
    p=p->link;
    k++;
  }
}
if(loc!=0)
{
cout<<"found At Location ="<<loc;
}
else
{
cout<<"Not found ";
}
}

return 0;
}

Insertion in A Sorted Linked List (Ascending or Descending)

//Insertion in Sorted Linked List

#include<iostream>
using namespace std;
struct node
{
 int info;
 node *link;
};
int main()
{
 node *start,*p,*pp,*temp;
 start=p=NULL;
 //Creating Linked List
 int n;
 cout<<"Press [0] to Exit :: Input Elements To List :";
 do
 {
    cin>>n;
  if(n)
  {
     temp=new node;
     temp->info=n;
     temp->link=NULL;
     if(start==NULL)
 start=temp;
     else
 p->link=temp;
     p=temp;
    }
 }while(n);
 //printing nodes
 p=start;
 cout<<"Created Nodes are : ";
 while(p)
 {
  cout<<p->info<<" ";
  p=p->link;
 }

//find Location and insert item

cout<<"Enter Item to Insert :";
int item;
cin>>item;
    if(start==NULL)
{
temp=new node;
            temp->info=item;
            temp->link=NULL;
       start=temp;
            p=temp;
           
}

if(item<start->info)
{
p=start;
temp=new node;
            temp->info=item;
            temp->link=p;
       start=temp;
            p=temp;
}
pp=start;
p=start->link;
while(p!=NULL)
{
if(item<p->info)
{
temp=new node;
        temp->info=item;
pp->link=temp;
temp->link=p;
break;
   }
pp=p,p=p->link;
}
if(p==NULL)
{
temp=new node;
temp->info=item;
pp->link=temp;
temp->link=NULL;
}
//printing
p=start;
cout<<"After Insertion : ";
 while(p)
 {
  cout<<p->info<<" ";
  p=p->link;
 }
return 0;
}

Insertion In A Linked List At Some Location

//Insertion At Location

#include<iostream>
using namespace std;
struct node
{
 int info;
 node *link;
};
int main()
{
 node *start,*p,*loc,*temp;
 start=p=NULL;
 //Creating Linked List
 int n;
 cout<<"Press [0] to Exit :: Input Elements To List :";
 do
 {
    cin>>n;
  if(n)
  {
     temp=new node;
     temp->info=n;
     temp->link=NULL;
     if(start==NULL)
 start=temp;
     else
 p->link=temp;
     p=temp;
    }
 }while(n);
 //printing nodes
 p=start;
 cout<<"Created Nodes are : ";
 while(p)
 {
  cout<<p->info<<" ";
  p=p->link;
 }
///////////////////////////////
cout<<"\nEnter item to insert ";
int item;
cin>>item;
cout<<"Place where to Insert :";
int k;
cin>>k;
loc=NULL;
p=start;
 for(int i=1;i<=k-1;i++)
 {
p=p->link;
loc=p;
 }
temp=new node;
temp->info=item;
 if(loc==NULL)
 {
temp->link=start;
start=temp;
 }
 else
 {
temp->link=loc->link;
loc->link=temp;
 }
//printing
 p=start;
 cout<<"List After Insertion : ";
 while(p!=NULL)
 {
  cout<<p->info<<" ";
  p=p->link;
 }
return 0;
}

Thursday, 27 March 2014

Merging two Accending Arrays

//Merging two Accending Arrays
#include<iostream.h>
int main()
{
int a[50],b[50];
int i,j,k,m,n;
cout<<"enter no of elements in A ";
cin>>m;
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"enter no of elements in B ";
cin>>n;
for(j=0;j<n;j++)
{
cin>>b[j];
}
//////////////////////////////////////
int s=m+n;
int p=0;j=0;
label:i=p;
for(i=p;i<m;i++)
{
if(b[j]<a[i])
{
for(k=m;k>i;k--)
{
a[k]=a[k-1];
}
a[i]=b[j];
m++;
j++;
p=i+1;
goto label;
}
}

if(m<s)
{
 for(i=j;i<=n;i++,j++)
 {
  a[m]=b[j];
  m++;
 }
}
for(i=0;i<s;i++)
{
cout<<a[i]<<" ";
}
return 0;
}