Tuesday, September 29, 2015

Simple string matching algorithm to find position of a pattern in a given string without using any string function in C

#include<stdio.h>
#include<stdlib.h>
void main()
{ int i=0,j,count;
  char string[]={"kfhisdcodesoecodeupzz"};
  char pattern[]={"code"};
 
 
  for(i=0;i<sizeof(string)-1;i++)
     {
     
       if(string[i]==pattern[0])
         { count =1;

           for(j=1;j<sizeof(pattern)-1;j++)
              {
                if(pattern[j]==string[i+j])
                   count++;
                else break;
              }

         if(count==(sizeof(pattern)-1))
         printf("\none match at position : %d ",i);
           
         }
     
     }
printf("\n");
}

Saturday, September 26, 2015

Implementation of Breadth First search in C using dynamic adjacency list

#include <stdio.h>
#include <stdlib.h>
typedef struct node{int key;struct node * next;}node;
node * init_node(node * n,int b){n=(node *)malloc(sizeof(node));n->key=b;n->next=NULL;return n;}
node **list,*front=NULL,*rear=NULL;
void enqueue(node * n)
{ if(rear==NULL)front=rear=n;
  else{ rear->next=n;rear=n; } }
node * dequeue()
{
if(front==NULL&&rear==NULL){return NULL;}
if(front==rear){ node *temp=front;front=rear=NULL;return temp;}
node * temp=front;front=front->next;return temp;
}
void add_edge(int a,int b)
{  node * ptr=NULL,*n=NULL,*p;
   n=init_node(n,b);
    if(list[a]==NULL){p=init_node(p,a);list[a]=p;}
    ptr=list[a]; while(ptr->next!=NULL)ptr=ptr->next;ptr->next=n;

    ptr=NULL;n=NULL;
   n=init_node(n,a);
    if(list[b]==NULL){p=init_node(p,b);list[b]=p;}
    ptr=list[b]; while(ptr->next!=NULL)ptr=ptr->next;ptr->next=n;
}
void dfs(int s,int n)
{   node * ptr;int visited[n],i,d=6,parent[n],distance[n],parenti;
      for(i=0;i<n;i++){visited[i]=0;parent[i]=0;distance[i]=-1;}
    if(list[s]==NULL)return;
  enqueue(list[s]);
  parenti=s;
  distance[s]=0;
  visited[s]=1;
printf("\nDFS Distance from %d  :\n",s);
  while(front!=NULL)
  {
     ptr=dequeue();
     parenti=ptr->key;
     ptr=list[ptr->key]->next;

    while(ptr!=NULL)
     {   if(!visited[ptr->key])
         {enqueue(ptr);if(parent[ptr->key]==0)parent[ptr->key]=parenti;visited[ptr->key]=1;
          distance[ptr->key] =distance[parent[ptr->key]]+6;
          printf(" node-%d:parent-:%d:distance:%d\n ",ptr->key,parent[ptr->key],distance[ptr->key]);}
         ptr=ptr->next;
     }


  }
}
int main()
{int n=10,i,j;node *ptr=NULL;
 list=(node **)malloc(n*(sizeof(node *)));

for(i=0;i<n;i++)
{  list[i]=(node *)malloc(n*(sizeof(node *)));list[i]=NULL;}

add_edge(2,1);
add_edge(0,1);add_edge(2,0);
add_edge(2,3);
add_edge(9,6);add_edge(9,0);add_edge(9,5);add_edge(9,4);add_edge(9,7);add_edge(9,8);add_edge(9,3);
add_edge(8,7);add_edge(8,6);
add_edge(7,2);add_edge(7,2);
add_edge(6,0);add_edge(6,1);
add_edge(5,4);add_edge(5,2);add_edge(5,3);
add_edge(4,2);
add_edge(3,0);add_edge(3,1);
add_edge(1,0);

for(j=0;j<n;j++)
    if(list[j]!=NULL)
         { ptr=list[j];
           while(ptr!=NULL){printf("%d --> ",ptr->key);ptr=ptr->next;}
          printf("\n");
         }
dfs(3,10);

return 0;
}

Implementation of Binary Search Tree in C using dynamic linked list

//Binary Search Tree
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
  int key;
  struct node *left;
  struct node* right;
}node;

void printIO(node * n)
{ if(!n)return;
  if(n->left)
    printIO(n->left);
  printf("%d.",n->key);
  if(n->right)
    printIO(n->right);
}

node * create(node * n,int k)
{ n=(node *)malloc(sizeof(node));
  n->key=k;
  n->left=NULL;
  n->right=NULL;  
 return n;
 }

node * insert(node * n,int k)
{  
   node * root=n;
   if(n==NULL)
     {n= create(n,k);printf("%d.",k);}
   else if(k>n->key){n->right= insert(n->right,k);}
   else if(k<=n->key){n->left= insert(n->left,k);} 
   return n;
}

int main()
{
node * root=NULL;
printf("\n Inserting Sequence\n");
root=insert(root,202);root=insert(root,125);root=insert(root,255);root=insert(root,313);
root=insert(root,116);root=insert(root,222);root=insert(root,150);

printf("\n In Order of Created Binary Search tree\n");
printIO(root);
printf("\n");
return 0;
}

Friday, September 25, 2015

Implementation of AVL tree in C

#include<stdio.h>
#include<stdlib.h>
int kk;
typedef struct node{
int key;
int height;
struct node * left;
struct node * right;
}node;

node * create(node * n,int k)
{ n=(node *)malloc(sizeof(node));
  kk=k;
  n->key=k;
  n->height=1;
  n->left=NULL;
  n->right=NULL;
  return n;
}

int max(int a,int b){return (a<b)?b:a;}

node *updateheight(node *n )
{
  if(n->left!=NULL&&n->right!=NULL)
  {n->height=max(n->left->height,n->right->height)+1;printf("height updated\n");}
  else if(n->left!=NULL)
  {n->height=n->left->height;printf("height updated\n");}
  else if(n->right!=NULL)
  {n->height=n->right->height;printf("height updated\n");}
  else
  n->height=1;

  return n;
}
node * rotateR(node * r)
{
 
  node *n=r->left;
  node *temp;
  if(r->left->right!=NULL)
    temp=r->left->right;
  else temp=NULL;
  n->right=r;
  r->left=temp;
  if(n->right!=NULL)
  n->right=updateheight(n->right);
  n=updateheight(n);
  printf(".R.");
 return n;
 
}
node * rotateL(node * r)
{ node *n=r->right;
  node *temp;
  if(r->right->left!=NULL)
  temp=r->right->left;
  else temp =NULL;
  n->left=r;
  r->right=temp;
  updateheight(n->left);
  updateheight(n);
  printf(".L.");
  return n;

}

node * balance(node *n,int k)
{ int balance,l=0,r=0;
   if(n->left)
     l=n->left->height;
   if(n->right)
     r=n->right->height;
   balance=l-r;
     if(balance>1&&k<=n->left->key)
        {n=rotateR(n);printf("LL..");}
     else if(balance>1&&k>n->left->key)
       {printf("LR..");n=rotateL(n);n=rotateR(n);}
     else if(balance<-1&&k>n->right->key)
       {n=rotateL(n);printf("RR..");}
     else if(balance<-1&&k<=n->left->key)
       {n=rotateR(n);n=rotateL(n);printf("RL..");}

  return n;

}

node * insert(node * r,int k)
{
   if(r==NULL){node *n=create(n,k);r=n;}
   else if(k<=r->key){r->left=insert(r->left,k);r->height=r->left->height+1;r=balance(r,kk);}
   else if(k>r->key){r->right=insert(r->right,k);r->height=r->right->height+1;r=balance(r,kk);}
   return r;
}
void printAVLIO(node *r)
{
    if(!r)return;
    if(r->left)printAVLIO(r->left);
    printf("\n%d..",r->key);
    printf(".height.%d..\n",r->height);
    if(r->right)printAVLIO(r->right);
}

int main()
{
  node *root=NULL;
  int i,a[]={50,25,75,70,60},k=sizeof(a)/sizeof(int);
  printf("Inseting sequence into the AVL tree\n");
  for(i=0;i<k;i++)
     printf("%d..",a[i]);printf("\n");
  i=0;
  while(i<k)
     {root=insert(root,a[i]);i++;}
  printAVLIO(root);
return 0;
}

Sunday, September 6, 2015

Program to Build binary MaxHeap using ANSI C

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

int c,i,j;

int left(int j){return j*2+1;}
int right(int j){return j*2+2;}
int parent(int j) {return (j-1)/2;}

typedef struct node
{  int data;}node;

typedef struct heap
{  int capacity;
  int heapSize;
  node *element;
}heap;

void swap(node *x,node *y)
{ node *temp; temp=x; y=x; x=temp; }

initHeap(heap *h,int cap){h->capacity=cap;}

printHeap(heap *h){
for (i=0;i<h->capacity;i++)
    printf("\n %d",h->element[i].data);
}


insertKey(heap *h,int k)
{
   if(!(h->capacity))
     h->element=malloc(sizeof(node));
  else
     h->element=realloc(h->element,(h->capacity+1)*sizeof(node));
  node n;
  n.data=k;
  int i=(h->heapSize)++;
  while(i&&h->element[i].data>h->element[parent(i)].data)
       h->element[i]=h->element[parent(i)];
  h->element[i]=n;
}

void heapify(heap *h,int i)
{
   int max=i;
   if(h->element[left(i)].data>h->element[i].data)
     max=left(i);
  if(h->element[right(i)].data>h->element[i].data)
     max=right(i);
  if(i!=max)
    swap(&(h->element[max]),&(h->element[i]));
  heapify(h,max);
}

int extractMax(heap *h)
{
  int max=h->element[0].data;
   h->element[0]=h->element[(h->heapSize)--];
   h->element=realloc(h->element,h->heapSize*sizeof(node));
 
  return max;

}

deleteKey(heap *h,int k)
   {  if(h->heapSize)
      {  h->element[k].data=INT_MAX;
         heapify(h,k);
         int d=extractMax(h);
      }
      else printf("\n MaxHeap Is Empty");

   }

void main()
{
 int capacity=0;
 heap maxHeap;
 initHeap(&maxHeap,capacity);
char c='c';
int key;
  while (c=='c')
 { printf("\ni...insert");
  printf("\np...print");
  printf("\nm...get max");
  printf("\ne...exit");
  printf("\n\n........ENTER CHOICE::");
  c=getchar();
  switch (c)
        {
          case 'i' :
          printf("\nEnter the number to be inserted : ");
          scanf("%d",&key);
          insertKey(&maxHeap,key);
          printf("\n");
          printHeap(&maxHeap);
          break;
          case 'p':
          printf("\n");
          printHeap(&maxHeap);
          break;
          case 'm':
          printf("%d",extractMax(&maxHeap));
          default : break;
        }

 printf("\nPress....c...To Continue");
 printf("\nPress....any other key...EXIT");
 c=getchar();
printf("\n");
}
 

}