Sunday, 7 December 2014

AVL Tree program in C

#include <stdio.h> #include<malloc.h> typedef enum{FALSE,TRUE}booleanx; struct anode *avlinsert(int,struct anode*,booleanx*); struct anode *avlsearch(struct anode*,int); void avldisplay(struct anode*,int); void avlinorder(struct anode*); struct anode { int info; //data int balance;//balance factor struct anode *lchild; struct anode *rchild; }; void main() { booleanx ht_inc; int info; int choice; system("cls"); struct anode *root=(struct anode*)malloc(sizeof(struct anode)); root=NULL; while(1) { printf("\n1. Insert\n"); printf("\n2. Display\n"); printf("\n3. Return to tree menu\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n Enter the value to be inserted:"); scanf("%d",&info); if(avlsearch(root,info)==NULL) root=avlinsert(info,root,&ht_inc); else printf("\n Duplicate value ignored\n"); break; case 2: if(root=NULL) { printf("\n Tree is empty\n"); continue; } printf("\n Tree is:\n"); avldisplay(root,1); printf("\n\n"); printf("\n Inorder traversal is:"); avlinorder(root); printf("\n"); break; case 3: return; default : printf("\nWrong choice\n"); }//end of switch }//end of while }//end of main struct anode *avlsearch(struct anode*ptr,int info) { if(ptr!=NULL) if(info<ptr->info) ptr=avlsearch(ptr->lchild,info); else if(info>ptr->info) ptr=avlsearch(ptr->rchild,info); return ptr; }//end of search struct anode *avlinsert(int info,struct anode*pptr,booleanx*ht_inc) { struct anode *aptr; struct anode *bptr; if(pptr==NULL) { pptr=(struct anode*)malloc(sizeof(struct anode)); pptr->info=info; pptr->lchild=NULL; pptr->rchild=NULL; pptr->balance=0 *ht_inc=true; return pptr; } if (info<pptr->info) { pptr->lchild=avlinsert(info,pptr->lchild,ht_inc); if(*ht_inc==true) { switch(pptr->balance) { case 1://right heavy; pptr->balance=0 *ht_inc=false; break; case 0: //balanced pptr->balance=1; break; case -1://left heavy aptr=pptr->lchild; if(aptr->balance==-1) { printf("left to left rotation\n"); pptr->lchild=aptr->rchild; aptr->rchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("left to right rotation\n"); bptr=aptr->rchild; aptr->rchild=bptr->lchild; bptr->lchild=aptr; pptr->lchild=bptr->rchild; bptr->rchild=pptr; if (bptr->balance==1) pptr->balance=-1; else pptr->balance=0; if(bptr->balance==-1) aptr->balance=1; else aptr->balance=0; bptr->balance=0; pptr=bptr; } *ht_inc=false; }//end of switch }//end of if }//end of if if(info>pptr->info) { pptr->rchild=avlinsert(info,pptr->rchild,ht_inc); if (*ht_inc==true) { switch (pptr->balance) { case 1://left heavy pptr->balance=0; *ht_inc=false; break; case 0://balanced pptr->balance=-1; break; case -1: //right heavy aptr=pptr->rchild; if (aptr->balance==-1) { printf("Right to right rotation\n"); pptr->rchild=aptr->lchild; aptr->lchild=pptr; pptr->balance=0; aptr->balance=0; pptr=aptr; } else { printf("Right to left rotation\n"); bptr=aptr->lchild; aptr->lchild=bptr->rchild; bptr->rchild=aptr; pptr->rchild=bptr->lchild; bptr->lchild=pptr; if(bptr->balance==-1) pptr->balance=1; else pptr->balance=0; if(bptr->balance==1) aptr->balance=-1; else aptr->balance=0; bptr->balance=0; pptr=bptr; }//end of else *ht_inc=false; }//end of switch }//end of if }//end of if return pptr; }//end of the function void avldisplay(struct anode*ptr,int level) { int i; if (ptr!=NULL) { avldisplay(ptr->rchild,level+1); printf("\n"); for (i=0; i<level;i++ ) printf(" "): printf("%d",ptr->info); avldisplay(ptr->lchild,level+1); }//end of if }//end of function void avlinorder(struct anode*ptr) { if (ptr!=NULL) { avlinorder(ptr->lchild); printf("%d",ptr->info); avlinorder(ptr->rchild); } }

Wednesday, 5 November 2014

How to find the sizeof Data type and Display in Decemial,Octal and HexaDecimal format in C

void main() { int i; float r; printf("welcome to kmit\n"); printf("welcome to Telengana\n"); i=15; //i=35.756; r=105643.7564; printf("%d\n",i); printf("%0.4f\n",r); printf("%d\n",sizeof(i)); printf("%d\n",sizeof(r)); printf("%d\n",i);//display in decimal printf("%o\n",i);//display in Octal printf("%0x\n",i);//display in Hexadecimal }

Radix sort program in C

#include <stdio.h> struct radix { struct radix *link; int info; }*start; void display(); int ndig(); int kdig(int,int); void radixsort(); void main() { struct radix *temp,*q; int i,j,k,item; printf("enter no of items : "); scanf("%d",&item); for(i=0;i<item;i++) { printf("enter the item %d :\n",i+1); scanf("%d",&j); temp=(struct radix*)malloc(sizeof(struct radix)); temp->info=j; temp->link=NULL; if(start==NULL) start =temp; else { q=start; while(q->link!=NULL) q=q->link; q->link=temp; } } printf("unsorted list is: \n"); display(); radixsort(); printf("\nsorted list : \n"); display(); } void display() { struct radix *temp; temp=start; while(temp!=NULL) { printf("%d\t",temp->info); temp=temp->link; } } int ndig() { struct radix *temp; int large=0,ndig=0; temp=start; while(temp!=NULL) { if(temp->info>large) large=temp->info; temp=temp->link; } while(large>0) { ndig++; large=large/10; } return(ndig); } int kdig(int num,int k) { int i,dig; for(i=1;i<=k;i++) { dig=num%10; num=num/10; } return dig; } void radixsort() { struct radix *p,*front[10],*rear[10]; int mindig,maxdig,lstsig,maxsig,k,i,dig; lstsig=1; maxsig=ndig(); for(k=lstsig;k<=maxsig;k++) { for(i=0;i<=9;i++) { rear[i]=NULL; front[i]=NULL; } mindig=9; maxdig=0; p=start; while(p!=NULL) { dig=kdig(p->info,k); if(dig>maxdig) maxdig=dig; if(dig<mindig) mindig=dig; if(front[dig]==NULL) front[dig]=p; else rear[dig]->link=p; rear[dig]=p; p=p->link; } printf("\nmindig %d maxdig %d",mindig,maxdig); start=front[mindig]; for(i=mindig;i<maxdig;i++) { if(rear[i+1]!=NULL) rear[i]->link=front[i+1]; else rear[i+1]=rear[i]; rear[maxdig]->link=NULL; } printf("\nnew list\n"); display(); } }

Monday, 27 October 2014

write a C program for Selection sort

#include <stdio.h> #include <conio.h> #define MAXSIZE 50 int arr[MAXSIZE],n; void selectionsort(int arr[],int n); void main() { int i; printf("Enter the size of array\n"); scanf("%d",&n); printf("\nEnter the values \n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("\nArray before sorting \n"); for(i=0;i<n;i++) printf("\t%d",arr[i]); printf("\n\n"); selectionsort(arr,n); printf("\nArray after sorting \n"); for(i=0;i<n;i++) printf("\t%d",arr[i]); } void selectionsort(int arr[],int n) { int i,j,temp,min; for(i=0;i<n-1;i++) { min=i; for(j=i+1;j<n;j++) { if(arr[min]>arr[j]) { min=j; } temp=arr[i]; arr[i]=arr[min]; arr[min]=temp; } } }

Write a C program for Insertion sort

#include<stdio.h> int main(){ int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=1;i<s;i++){ temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)){ a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; }

Bubble sort program using scratch


Friday, 10 October 2014

Linear Search program in C

#include <stdio.h> void main() { int array[10]; int i, num, keynum, found = 0; printf("Enter the value of num \n"); scanf("%d", &num); printf("Enter the elements one by one \n"); for (i = 0; i < num; i++) { scanf("%d", &array[i]); } printf("Input array is \n"); for (i = 0; i < num; i++) { printf("%d\n", array[i]); } printf("Enter the element to be searched \n"); scanf("%d", &keynum); /* Linear search begins */ for (i = 0; i < num ; i++) { if (keynum == array[i] ) { found = 1; break; } } if (found == 1) printf("Element is present in the array\n"); else printf("Element is not present in the array\n"); } Efficiency of Linear Search: To find the number of key comparisons for a successful match, we can add the number required for each comparison and divide by the total number of elements in the list: (1+2+3+4+....+n)/n=n(n+1)/2n

Binary search with out using Recursion

#include <stdio.h> int b_search(int *a,int size,int key) { int first=0,last=size-1,mid; while(first<=last) { mid=(first+last)/2; if(a[mid]==key) return mid; if(a[mid]<key) first=mid+1; else last=mid-1; } return -1; } void main() { int arr[]={11,18,52,95,101,107,135,148,170,185}; printf("%d",b_search(arr,10,185)); }