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;
}

Saturday, 15 March 2014

Creating Nodes in Singly Linked List Using Structure.

//Creating Nodes in Singly Linked List Using Structure.
#include<iostream.h>
using namespace std;
struct node
{
 char info;
 node *link;
};
int main()
{
 node *start,*p,*temp;
 start=p=NULL;
 char ch;
 do
 {
  char item;
  cout<<"Input Element to info :";
  cin>>item;
  temp=new node;
  temp->info=item;
  temp->link=NULL;
  if(start==NULL)
start=temp;
  else
p->link=temp;
  p=temp;
  cout<<"Create More Nodes y or n :";
  cin>>ch;
 }while(ch=='y');
 //printing nodes
 p=start;
 while(p)
 {
  cout<<p->info;
  p=p->link;
 }
return 0;
}

Thursday, 13 March 2014

Creation of linklist by using the concept of Parallel Array

//Creation of linklist by using the concept of Parallel Array
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int info[10],link[10],item,p,i;
for(i=1;i<10;i++)
{link[i]=i+1;}
link[i]=0;
int start=0,free=1,node=0;
char ch;

do
{
if(start==0)
{
start=free;
cout<<"enter the item to insert ";
cin>>item;
info[start]=item;
free=link[free];
link[start]=0;
node++;
}
      else
{
p=start;
while(link[p]!=0)
{p=link[p];}
link[p]=free;
p=link[p];
cout<<"enter the no to be insert ";
cin>>item;
info[p]=item;
free=link[free];
link[p]=0;
node++;
}
cout<<"wants to continue y or n: ";
cin>>ch;
}while(ch=='y');

cout<<"created linklist are ";
for(i=1;i<=node;i++)
cout<<info[i]<<"  ";
return 0;
getch();
}

Thursday, 6 March 2014

Creation Of A Node In Singly Link List using Structure in C

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//-------------------------------------------------

void create()
{
char ch;
 do
 {
  struct node *new_node,*current;

  new_node=(struct node *)malloc(sizeof(struct node));

  printf("\n Enter the data : ");
  scanf("%d",&new_node->data);
  new_node->next=NULL;

  if(start==NULL)
  {
  start=new_node;
  current=new_node;
  }
  else
  {
  current->next=new_node;
  current=new_node;
  }

 printf("\n Do you want to creat another : ");
 ch=getche();
 }while(ch!='n');
}
//------------------------------------------------------------------

void display()
{
struct node *new_node;
 printf("The Linked List : \n");
 new_node=start;
 while(new_node!=NULL)
   {
   printf("%d->",new_node->data);
   new_node=new_node->next;
   }
  printf(" NULL");
}
//----------------------------------------------------
void main()
{
clrscr();
create();
display();
getch();
}
//----------------------------------------------------