The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Friday, February 25, 2011

Basic Operations of Pointer Nodes


#include<stdio.h>
#include<conio.h>

 struct node
  {
   int info;
   node *next;
  }*start,*nptr,*save,*ptr,*end;

 node *create(int);
 int new_node_input();
 void insert_node(node *,int);
 void display_node(node *);
 void del_node(node *,int);

 void main()
 {
 clrscr();
 int choice,inf;
 start = end = NULL;
 inf = new_node_input();
 nptr = create(inf);
 insert_node(nptr,1);
 display_node(start);
 do
 {
 printf("\n\n\t\t What Would Like To Do Now ? \n");
 printf("\n 1. Inserting Node From the Beginning ");
 printf("\n 2. Inserting Node From the End ");
 printf("\n 3. Deleting Node From the Beginning");
 printf("\n 4. Deleting Node From the End");
 printf("\n\t\t ..... Hit Other Keys To Exit .....");
 printf("\n\n Enter your choice : ");
 scanf("%d",&choice);
 switch(choice)
  {
  case 1 :
  case 2 : inf = new_node_input();
  nptr = create(inf);
  insert_node(nptr,choice);
  break;

  case 3 :
  case 4 : del_node(nptr,choice);
  break;

  default : break;
  }
 display_node(start);
 }while((choice>=1)&&(choice<=4));
 printf("\n\n\t\t !!!! Thanks Fro Using The Program !!!!");
 getch();
 }

 int new_node_input()
 {
  int number;
  printf("\n Enter The Information For The New Node : ");
  scanf("%d",&number);
  return number;
 }

 node *create(int number)
 {
  ptr = new node;
  ptr->info = number;
  ptr->next = NULL;
  return ptr;
 }

 void insert_node(node *np,int choice)
 {
  printf("\n\n\t\tInsertion has been done \n\n");
  getch();
  if(start == NULL)
   start = end = np;
  else
  {
   if(choice == 1)
   {
   save = start;
   start = np;
   np->next = save;
   }
   else
   {
   end->next = np;
   end  = np;
   }
  }
 }

 void display_node(node *np)
 {
  printf("\n\n Now The List Is : \n\t");
  while(np != NULL)
  {
   printf("%d ->",np->info);
   np = np->next;
  }
 }

 void del_node(node *np,int choice)
 {
  printf("\n\n\t\tDeletion has been done \n\n");
  getch();
  if(start == NULL)
   printf("\n\nUNDERFLOW !!!\n\n");
  else
  {
   if (choice == 3)
   {
    np = start;
    start = start->next;
   }
   else
   {
    np = end;
    end = NULL;
   }
   delete np;
  }
 }

Wednesday, February 23, 2011

Basic Operations of Arrays


#include<stdio.h>
#include<conio.h>
#define max 20

struct data
 {
   int rno;
   int marks;
 }dt[max];

 struct data input_data();
 void disp_data(struct data[],int);
 void disp_sdata(struct data);
 int disp_sbm(int);
 void add_ele(struct data[],int&,int);
 void del_ele(struct data[],int&,int);
 void Ssort(struct data[],int);
 void Bsort(struct data[],int);
 void Lsearch(struct data[],int,int);
 void Bsearch(struct data[],int,int);
 void trav(struct data[],int,int);

void main()
 {
  clrscr();
  int i,num,cho,sbch,item,ssbch;
  do
  {
  printf("Enter the no. of students :- ");
  scanf("%d",&num);
  }while( num <= 0 ||  num >= max );
  for(i=0;i<num;i++)
   {
    printf("\n\n Enter the data of the student #%d: \n",i+1);
    dt[i] = input_data();
   }
  clrscr();
  disp_data(dt,num);
  getch();
  do
  {
  clrscr();
  printf("\n\n\t\t\t   Menu Selection\n");
  printf("\n 1. Insertion of an Element ");
  printf("\n 2. Deletion of an Element ");
  printf("\n 3. Sorting of an Array ");
  printf("\n 4. Searching of an Element ");
  printf("\n 5. Traversing of all Elements ");
  printf("\n\t\t Hit Other Keys To Exit .....");
  printf("\n\n Enter your choice :- ");
  scanf("%d",&cho);
  switch(cho)
   {
    case 1: sbch = disp_sbm(1);
   switch(sbch)
    {
      case 1:
      case 2:
      case 3: add_ele(dt,num,sbch);
      break;
     default : break;
    }
   break;
    case 2: sbch = disp_sbm(2);
   switch(sbch)
    {
      case 1:
      case 2:
      case 3: del_ele(dt,num,sbch);
      break;
     default : break;
    }
   break;
    case 3: sbch = disp_sbm(3);
   switch(sbch)
    {
      case 1: Ssort(dt,num);
      break;
      case 2: Bsort(dt,num);
      break;
     default : break;
    }
   break;
    case 4: sbch = disp_sbm(4);
   printf("Enter the roll no. to be searched : ");
   scanf("%d",&item);
   switch(sbch)
    {
      case 1: Lsearch(dt,num,item);
      break;
      case 2: Bsearch(dt,num,item);
      break;
      default: break;
    }
   break;
    case 5: sbch = disp_sbm(5);
   switch(sbch)
    {
      case 1:
      case 2: trav(dt,num,sbch);
      break;
      default: break;
    }
   break;
   }
  if(cho>=1 && cho<=5)
   disp_data(dt,num);
  }while(cho>=1 && cho<=5);
  printf("\n\n\t\t !!!! Thanks Fro Using The Program !!!!");
  getch();
 }

struct data input_data()
 {
   struct data s;
   printf("\n\t Enter the Roll no. of the student :- ");
   scanf("%d",&s.rno);
   printf("\n\t Enter the marks of the student :- ");
   scanf("%d",&s.marks);
   return s;
 }

void disp_data(struct data s[],int noe)
 {
  int i;
  clrscr();
  printf("\n\n\t The current status of the current data \n");
  for(i=0;i<noe;i++)
  {
   printf("\n\n Data of the Student #%d: \n",i+1);
   printf("\n\t Roll no.:- %d",s[i].rno);
   printf("\n\t Marks :- %d",s[i].marks);
  }
  getch();
 }

void disp_sdata(struct data s)
 {
    printf("\n\n Data of the Student \n");
    printf("\n\t Roll no.:- %d",s.rno);
    printf("\n\t Marks :- %d",s.marks);
 }

 int disp_sbm(int choice)
  {
    int sb_choice;
    if(choice == 1 || choice ==2)
    {
     if(choice == 1)
      printf(" \n\n\t Select the position to insert an element ");
     else
      printf(" \n\n\t Select the position to delete an element ");
    printf("\n 1. At the beginning ");
    printf("\n 2. At the end ");
    printf("\n 3. At a desired position ");
    }
    else if(choice==3)
    {
     printf(" \n\n\t Select the option of sorting the array ");
     printf("\n 1. Selection Sort ");
     printf("\n 2. Bubble Sort  ");
    }
    else if(choice==4)
    {
     printf(" \n\n\t Select the option of searching an element ");
     printf("\n 1. Linear Search ");
     printf("\n 2. Binary Search  ");
    }
    else
    {
     printf(" \n\n\t Select the option of traversing of all element ");
     printf("\n 1. Addition of every Element ");
     printf("\n 2. Subtraction of every Element ");
    }
    printf("\n\n Choose your option : ");
    scanf("%d",&sb_choice);
    return sb_choice;
  }
 void add_ele(struct data x[],int &noe,int choice)
  {
   int i,loc=0;
   struct data temp;
   if(noe == max)
    printf("\n Warning : OVERFLOW !!! ");
   else
    {
      if(choice == 3)
       {
do
{
 printf("\n Enter the location you want insert the element");
 scanf("%d",&loc);
}while( loc>noe || loc<=0 );
loc -= 1;
       }
      printf("Enter the details of the element");
      temp = input_data();
      if(choice == 1 || choice == 3)
       {
for(i=noe;i>loc;i--)
 x[i] = x[i-1];
x[loc] = temp;
       }
      else
       {
x[noe] = temp;
       }
       noe += 1;
      printf("\n\n\t\t The element has been inserted ");
      getch();
    }
  }

 void del_ele(struct data x[],int &noe,int choice)
  {
   int i,loc=0;
   if(noe == 0)
    printf("\n Warning : UNDERFLOW !!! ");
   else
    {
      if(choice == 3)
       {
do
{
 printf("\n Enter the location you want delete the element");
 scanf("%d",&loc);
}while( loc>noe || loc<=0 );
loc -= 1;
       }
      if(choice == 1 || choice == 3)
       {
for(i=loc;i<noe-1;i++)
 x[i] = x[i+1];
       }
       noe -= 1;
      printf("\n\n\t\t The element has been deleted ");
      getch();
    }
  }
 void Ssort(struct data x[],int noe)
  {
   int i,j,flag,small,posn;
   struct data temp;
   for(i=0;i<noe;i++)
   {
    flag = 0;
    small = x[i].rno;
    for(j=i+1;j<noe;j++)
    {
     if( x[j].rno < small )
      { small = x[j].rno;  flag = 1; posn = j; }
    }
    if(flag==1)
     {
     temp = x[i];
     x[i] = x[posn];
     x[posn] = temp;
     }
   }
   printf("\n\n\t\t Sorting has been done ... \n\n");
  }
 void Bsort(struct data x[],int noe)
  {
   int i,j;
   struct data temp;
   for(i=0;i<noe;i++)
   {
    for(j=0;j<noe-1-i;j++)
    {
     if( x[j].rno > x[j+1].rno )
     {
     temp = x[j];
     x[j] = x[j+1];
     x[j+1] = temp;
     }
    }
   }
   printf("\n\n\t\t Sorting has been done ... \n\n");
  }

 void Lsearch(struct data x[],int noe,int ele)
  {
    int flag=0,i,noc=0,posn;
    for(i=0;i<noe;i++)
     {
      noc++;
      if(ele == x[i].rno)
       {  flag = 1;  posn = i;  break;  }
     }
    if(flag==0)
     printf("\n\nThe data not found ");
    else
     {
      printf("\n\nThe data has been found at position #%d",posn+1);
      printf("\n& The no. of comparisions were made : %d",noc);
      disp_sdata(x[posn]);
      }
    getch();
  }

void Bsearch(struct data x[],int noe,int ele)
   {
     int flag=0,i,noc=0,posn,beg,last,mid;
     beg = 0; last = noe - 1;
     Ssort(x,noe);
     while(beg<=last)
      {
       mid = (beg+last)/2;
       noc++;
       if(ele == x[mid].rno)
{
flag = 1;
posn = mid;
break;
}
       else if(ele > x[mid].rno)
beg = mid + 1;
       else
last = mid - 1;
      }
     if(flag==0)
      printf("\n\nThe data not found ");
     else
      {
      printf("\n\nThe data has been found at position #%d",posn+1);
      printf("\n& The no. of comparisions were made : %d",noc);
      disp_sdata(x[posn]);
      }
      getch();
   }
 void trav(struct data x[],int noe,int choice)
  {
   int i,ele;
   if(choice == 1)
    printf("\n Enter any number to be added to the data : ");
   else
    printf("\n Enter any number to be subtracted from the data : ");
   scanf("%d",&ele);
   for(i=0;i<noe;i++)
    {
     if(choice == 1)
       x[i].marks += ele;
     else
       x[i].marks -= ele;
    }
   printf("\n\n\t Traversing of all the elements of the data has been done \n\n");
   getch();
  }

Sunday, February 20, 2011

Radix Srt


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 10

struct ele
{
 int info;
}ar[max],qu[10][max],temp;

int noe,i,j,k,size,end,small,posn,flag;
int front[max],rear[max];

void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
 {
 scanf("%d",&ar[i].info);
 front[i] = rear[i] = 0;
 }
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void rdx_srt()
{
  int dig = 1,rem,exp,cnt=0,l,m;
 do
 {
  flag =0; m = 0;
  for(i=0;i<10;i++)
   rear[i] = 0;
  for(i=0;i<noe;i++)
  {
   exp = pow(10,dig-1);
   rem = (ar[i].info/exp) % 10;
   qu[rem][rear[rem]] = ar[i];
   rear[rem]++;
  }
  for(j=0;j<10;j++)
  {
   for(k=0;k<rear[j];k++)
    ar[m++] = qu[j][k];
  }
  printf("\n After Pass %d the array is :",++cnt);
  for(l=0;l<noe-1;l++)
  {
  printf(" %d",ar[l].info);
   if(ar[l].info > ar[l+1].info)
    flag=1;
  }
  printf(" %d",ar[l].info);
  dig+=1;
 }while(flag==1);

 printf("\n\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void main()
{
 clrscr();
 inp_dat();
 rdx_srt();
 getch();
}

Thursday, February 17, 2011

Merge Sort (Better Code)


#include<stdio.h>
#include<conio.h>
#define max 10

struct ele
{
 int info;
}ar[max],A[max],B[max],C[max];

int noe,i,j,k,size,mgsize,end,small,posn,flag,na,nb,nc;

void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
  scanf("%d",&ar[i].info);
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void merge()
{
 int a,b;
 for(a=0,b=0;a<na && b<nb;)
 {
  if(A[a].info <= B[b].info)
   C[nc++] = A[a++];
  else
   C[nc++] = B[b++];
 }
 while(a<na)
  C[nc++] = A[a++];
 while(b<nb)
  C[nc++] = B[b++];
}

void mrg_srt()
{
 int cnt = 0;
 size = 1;
 do
 {
 mgsize = size*2;
 for(i=0;i<noe;i+=mgsize)
  {
  end = i+mgsize >= noe ? noe:i+mgsize;
  na = nb = nc = 0;
  for(j=i;j<i+size;j++)
   A[na++] = ar[j];
  for(;j<end;j++)
   B[nb++] = ar[j];
  merge();
  for(j=0,k=i;j<nc;j++)
   ar[k++] = C[j];
  }
  printf("\n After Pass %d the array is :",++cnt);
  for(i=0;i<noe;i++)
   printf(" %d",ar[i].info);
  size = mgsize;
 }while(size<noe);
 printf("\n\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);

}

void main()
{
 clrscr();
 inp_dat();
 mrg_srt();
 getch();
}

Tuesday, February 15, 2011

Merge Sort


#include<stdio.h>
#include<conio.h>
#define max 10
struct ele
{
 int info;
}ar[max],temp;

int noe,i,j,k,size,end,small,posn,flag;

void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
  scanf("%d",&ar[i].info);
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void mrg_srt()
{

 int cnt = 0;
 size = 1;
 do
 {
 size *=2;
 printf("\n");
 for(i=0;i<noe;i+=size)
  {
  end = i+size >= noe ? noe:i+size;
  for(j=i;j<end;j++)
  {
   flag = 0;
   small = ar[j].info;
   for(k=j+1;k<end;k++)
    {
    if(small > ar[k].info)
    {
     small = ar[k].info; posn =k ;  flag = 1;
    }
    }
    if(flag==1)
     {
     temp = ar[j];
     ar[j] = ar[posn];
     ar[posn] = temp;
     }
   }
  }
  printf("\n After Pass %d the array is :",++cnt);
  for(i=0;i<noe;i++)
   printf(" %d",ar[i].info);
 }while(size<noe);
 printf("\n\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}
void main()
{
 clrscr();
 inp_dat();
 mrg_srt();
 getch();
}

Saturday, February 12, 2011

Huffman Tree Algorithm (Simple)


#include<conio.h>
#include <stdio.h>
#define max 0
#define mxbits 20

struct ele
{
  int posn;
  int info;
  int code[mxbits];
  int lft;
  int rgt;
}ar[max],tr[max],item,item1,tmp1,tmp2,tmp3;

int noe,i,j,ptr,par,ind,left,right;

void ins_heap(int n,struct ele item)
{
 tr[n] = item;
 if(n != 1)
 {
 ptr = n;
 par = ptr/2;
 while(ptr>1 && tr[par].info > tr[ptr].info)
 {
  item = tr[par];
  tr[par] = tr[ptr];
  tr[ptr] = item;
  ptr = par;
  par = ptr/2;
 }
 }
}

void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max/2);
 printf("\n\n Enter the frequencies '%d' elements : ",noe);
 for(i=1;i<=noe;i++)
  {
  scanf("%d",&ar[i].info);
  ar[i].posn = i;
  ar[i].lft = 0;
  ar[i].rgt = 0;
  ins_heap(i,ar[i]);
  }
}


struct ele del_heap(int end)
{
 item1 = tr[1];
 tr[1] = tr[end];
 par = 1; left = 2; right = 3;
 ind = 0;
 while(par < end && ind == 0)
 {
  if(tr[left].info < tr[par].info && tr[left].info < tr[right].info && left<end)
  {
  item = tr[left];
  tr[left] = tr[par];
  tr[par] = item;
  par = left;
  }
  else if(tr[right].info < tr[par].info && tr[left].info > tr[right].info  && right<end)
  {
  item = tr[right];
  tr[right] = tr[par];
  tr[par] = item;
  par = right;
  }
  else
   ind = 1;
  left = 2*par;
  right = 2*par+1;
 }
 return item1;
}

void hfmn_tree()
{
 i = noe;
 j = i+1;
 while(i>1)
 {
  tmp1 = del_heap(i);
  tmp2 = del_heap(--i);
  tmp3.info = tmp1.info + tmp2.info;
  tmp3.posn = j;
  tmp3.lft = tmp1.posn;
  tmp3.rgt = tmp2.posn;
  ar[j] = tmp3;
  ins_heap(i,tmp3);
  j++;
 }
 ar[j-1].posn = 1;
 i = j-1;
 while(i > noe)
 {
  ar[ar[i].lft].posn = 2*ar[i].posn;
  ar[ar[i].rgt].posn = 2*ar[i].posn+1;
  i--;
 }
}

void codes()
{
  int k;
  char ch;
  printf("\n The Codes for every input is : ");
  for(i=1;i<=noe;i++)
  {
   k = 0;
   ch = 64+i;
   printf(" \n For %c : %d. ",ch,ar[i].info);
   while(ar[i].posn > 1)
   {
   ar[i].code[k] = ar[i].posn % 2;
   ar[i].posn /= 2;
   k++;
   }
   for(j=k-1;j>=0;j--)
    printf("%d",ar[i].code[j]);
  }
}

void main()
{
clrscr();
inp_dat();
hfmn_tree();
codes();
getch();
}

Thursday, February 10, 2011

Huffman Tree Algorithm (Complex)


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<iomanip.h>
const MAX=55;
class m_heap      //a min heap
{ private:
int T[MAX],N,loc[MAX];//coz we take here,d sequential representation of trees here;
public:
m_heap()
{ N=0;
}
void insert(int item,int l)
{    N++;
    int ptr=N,par;
    while(ptr>1)
    {         par=ptr/2;
      if(item>=T[par])
      {        T[ptr]=item;
loc[ptr]=l;
return;
      }
      T[ptr]=T[par];
      loc[ptr]=loc[par];
      ptr=par;
    };
    T[1]=item;
    loc[1]=l;
}
void Sdis()
{ for(int i=1;i<=N;i++)
cout<<endl<<setw(9)<<T[i]<<setw(9)<<loc[i];
}
int del(int &lo)
{      int item=T[1],l=loc[1],last=T[N],ll=loc[N],ptr=1,left=2,right=3;
      N--;
      while(right<=N)
      {       if(last<=T[left] && last<=T[right])
{T[ptr]=last;
loc[ptr]=ll;
break;
}
if(T[right]>=T[left])
      { T[ptr]=T[left];
loc[ptr]=loc[left];
ptr=left;
      }
else
{T[ptr]=T[right];
loc[ptr]=loc[right];
ptr=right;
}

left=2*ptr;
right=left+1;
      };
      if(left==N && last>T[left])
      {ptr=left;
      T[1]=T[left];
      loc[1]=loc[left];
      }
      T[ptr]=last;
      loc[ptr]=ll;
      lo=l;
      return item;
}
int retN()
{ return N;
}



};
class hoffman
{ private:
int wt[MAX],left[MAX],right[MAX],n;
char info[MAX];
public:
void insert()
{       cout<<"Enter the no. of external nodes u want:";
cin>>n;
cout<<"\nEnter information and weight in order(info wt):";
for(int i=1;i<=n;i++)
{ cin>>info[i]>>wt[i];
left[i]=0;
right[i]=0;
}
}
void dis()
{
cout<<"s.no"<<setw(9)<<"INFO"<<setw(9)<<"WEIGHT"<<setw(9)<<"LEFT"<<setw(9)<<"RIGHT";
for(int i=1;i<=n;i++)
cout<<endl<<i<<"         "<<info[i]<<setw(9)<<wt[i]<<setw(9)<<left[i]<<setw(9)<<right[i];
}
void hoff()
{ m_heap H;
int w1,w2,l1,l2;
for(int i=1;i<=n;i++)
H.insert(wt[i],i);
while(1)
{     if(H.retN()==0)
    { n--;
     break;
    }

     w1=H.del(l1);
     w2=H.del(l2);
     n++;
     wt[n]=w1+w2;
     left[n]=l1;
     right[n]=l2;
     info[n]=' ';
     H.insert(wt[n],n);
}
}


};

void main()
{               clrscr();
hoffman H;
H.insert();
g:
clrscr();
cout<<"HOFFMAN menu:";
cout<<"\n1.> display";
cout<<"\n2.> Create Hoffman's tree";
cout<<"\n3.> Exit";
cout<<"\nEnter your choice:";
char ch;
cin>>ch;
switch(ch)
{ case '1':clrscr();
H.dis();
getch();
goto g;
case '2':clrscr();
H.hoff();
getch();
goto g;
default: exit(0);
};
getch();
}

Saturday, February 5, 2011

Basic Operations of Graph (Simple)


#include<stdio.h>
#include<conio.h>

struct node
{
  char info;
  int status;
  struct node *next;
  struct edge *adj;
};

struct edge
{
 struct node *dest;
 struct edge *link;
}*nedg,*ptre;

struct node *start,*nptr,*ptr,*loc,*rear;

int choice;
char item;

 void find_node()
 {
  loc = start;
  while(loc->info != item && loc != NULL)
   loc = loc->next;
 }

void create_node()
 {
  while(1)
  {
   nptr = new node;
   nptr->next = NULL;
   nptr->status = 1;
   nptr->adj = NULL;
   printf("\n Enter The New Node To be Inserted: ");
   cin>>item;
   nptr->info = item;
   if(item == '0')
    break;
   if(start == NULL)
    start = rear = nptr;
   else
    {
    find_node();
    if(loc == NULL)
     {
     printf("\n\n\t\tInsertion has been done \n\n");
     rear->next = nptr;
     rear = nptr;
     }
    else
     {
     printf("\n\n\t\tInsertion can't be done due to similiar vertices \n\n");
     delete nptr;
     }
    }
   }
  getch();
 }

void int_stats()
{
 ptr = start;
 while(ptr != NULL)
 {
  ptr->status = 1;
  ptr = ptr->next;
 }
}

void display_node()
 {
  ptr = start;
  printf("\n\n Now The List Is : \n\t");
  printf("\n Vertex List        Adjacent List \n");
  while(ptr != NULL)
  {
   printf("\n\t%c\t\t",ptr->info);
   ptre = ptr->adj;
   while(ptre != NULL)
   {
   printf(" %c",ptre->dest->info);
   ptre = ptre->link;
   }
   ptr = ptr->next;
  }
 }

void create_vertices()
{
 ptr = start;
 while(ptr != NULL)
 {
  choice = 0;
  while(1)
  {
  printf("\n Enter the element to be inserted as edge '%c': ",ptr->info);
  cin>>item;
  if(item == '0')
   break;
  find_node();
  if(loc != NULL)
  {
  nedg = new edge;
  nedg->dest = loc;
  nedg->link = NULL;
   if(ptr->adj == NULL)
    ptr->adj = nedg;
   else
   {
    nedg->link = ptr->adj;
    ptr->adj = nedg;
   }
   }
  }
   ptr = ptr->next;
 }
}

Friday, February 4, 2011

Shell Sort (Better Code)


#include<stdio.h>
#include<conio.h>
#define max 10
struct ele
{
 int info;
}ar[max],temp;

int noe,i,j,k,size,small,posn,flag;

void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
  scanf("%d",&ar[i].info);
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void shl_srt()
{

 int cnt = 0;
 size = noe/2;
 do
 {
 printf("\n (Size = %d)",size);
 for(i=size;i<noe;i++)
 {
  temp = ar[i];
  j = i;
  while(ar[j-size].info>temp.info && j>=0)
  {
   ar[j] = ar[j-size];
   j -= size;
  }
  ar[j] = temp;
 }
  printf("\n After Pass %d the array is :",++cnt);
  for(i=0;i<noe;i++)
   printf(" %d",ar[i].info);
  size /= 2;
 }while(size>=1);
 printf("\n\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void main()
{
 clrscr();
 inp_dat();
 shl_srt();
 getch();
}

Thursday, February 3, 2011

Shell Sort

#include<stdio.h>
#include<conio.h>
#define max 10
struct ele
{
 int info;
}ar[max],temp;

int noe,i,j,k,size,small,posn,flag;

void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
  scanf("%d",&ar[i].info);
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}

void shl_srt()
{

 int cnt = 0;
 size = noe%2 == 0 ? noe-1: noe;
 do
 {
 printf("\n (Size = %d)",size);
 for(i=0;i<size;i++)
 {
  for(j=i;j<noe;j+=size)
  {
   flag = 0;
   small = ar[j].info;
   for(k=j+size;k<noe;k+=size)
    {
    if(small > ar[k].info)
    {
     small = ar[k].info; posn =k ;  flag = 1;
    }
    }
    if(flag==1)
     {
     temp = ar[j];
     ar[j] = ar[posn];
     ar[posn] = temp;
     }
   }
  }
  printf("\n After Pass %d the array is :",++cnt);
  for(i=0;i<noe;i++)
   printf(" %d",ar[i].info);
  size -= 2;
 }while(size>=1);
 printf("\n\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}
void main()
{
 clrscr();
 inp_dat();
 shl_srt();
 getch();
}

Wednesday, February 2, 2011

Basic Operations of Graph


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

struct node
{
  char info;
  int status;
  struct node *next;
  struct edge *adj;
};

struct edge
{
 struct node *dest;
 struct edge *link;
}*nedg,*ptre;

struct node *start,*nptr,*ptr,*loc,*rear;

int choice;
char item;

 void find_node()
 {
  loc = start;
  while(loc->info != item && loc != NULL)
   loc = loc->next;
 }

void create_node()
 {
  while(1)
  {
   nptr = new node;
   nptr->next = NULL;
   nptr->adj = NULL;
   printf("\n Enter The New Node To be Inserted: ");
   cin>>item;
   nptr->info = item;
   if(item == '0')
    break;
   if(start == NULL)
    start = rear = nptr;
   else
    {
    find_node();
    if(loc == NULL)
     {
     printf("\n\n\t\tInsertion has been done \n\n");
     rear->next = nptr;
     rear = nptr;
     }
    else
     {
     printf("\n\n\t\tInsertion can't be done due to similiar vertices \n\n");
     delete nptr;
     }
    }
   }
  getch();
 }

void int_stats()
{
 ptr = start;
 while(ptr != NULL)
 {
  ptr->status = 1;
  ptr = ptr->next;
 }
}

void display_node()
 {
  ptr = start;
  printf("\n\n Now The List Is : \n\t");
  printf("\n Vertex List        Adjacent List \n");
  while(ptr != NULL)
  {
   printf("\n\t%c\t\t",ptr->info);
   ptre = ptr->adj;
   while(ptre != NULL)
   {
   printf(" %c",ptre->dest->info);
   ptre = ptre->link;
   }
   ptr = ptr->next;
  }
 }

void create_vertices()
{
 ptr = start;
 while(ptr != NULL)
 {
  choice = 0;
  while(1)
  {
  printf("\n Enter the element to be inserted as edge '%c': ",ptr->info);
  cin>>item;
  if(item == '0')
   break;
  find_node();
  if(loc != NULL)
  {
  nedg = new edge;
  nedg->dest = loc;
  nedg->link = NULL;
   if(ptr->adj == NULL)
    ptr->adj = nedg;
   else
   {
    nedg->link = ptr->adj;
    ptr->adj = nedg;
   }
   }
  }
   ptr = ptr->next;
 }
}

void main()
{
clrscr();
create_node();
create_vertices();
display_node();
getch();
}