Vous êtes sur la page 1sur 19

1. POLYNOMIAL DIFFERENTIATION: #include<stdio.h> #include<conio.h> #include<alloc.

h> float x,y; struct node { float coeff,pow; struct node *next; }; struct node *head,*temp,*mytemp; void main() { char ans; clrscr(); head=(struct node *)malloc(sizeof(struct node)); printf("\n Enter the coefficient of the first term \n"); scanf("%f",&x); head->coeff=x; printf("\n Enter the power of 1st term \n"); scanf("%f",&y); head->pow=y; head->next=NULL; printf("\n Do u want to add a new term:"); ans=getch(); while(ans=='y') { temp=(struct node *)malloc(sizeof(struct node)); printf("\n Enter the coefficient of the new term \n");

scanf("%f",&x); temp->coeff=x; printf("\n Enter the power of new term \n"); scanf("%f",&y); temp->pow=y; temp->next=NULL; mytemp=head; while(mytemp->next!=NULL) mytemp=mytemp->next; mytemp->next=temp; printf("\n Do u want to add a new term:"); ans=getch(); } printf("\n BEFORE DIFF \n "); mytemp=head; printf("%fx%f",mytemp->coeff, mytemp->pow); mytemp=mytemp->next; while(mytemp!=NULL) { printf(" + %fx%f",mytemp->coeff, mytemp->pow); mytemp=mytemp->next; } mytemp=head; while(mytemp!=NULL) { mytemp->coeff=mytemp->coeff * mytemp->pow; mytemp->pow = mytemp->pow-1; mytemp=mytemp->next; }

printf("\n AFTER DIFF \n "); mytemp=head; printf("%fx%f",mytemp->coeff, mytemp->pow); mytemp=mytemp->next; while(mytemp!=NULL) { printf(" + %fx%f",mytemp->coeff, mytemp->pow); mytemp=mytemp->next; } getch(); } 2. LINEAR SEARCH: #include<stdio.h> #include<conio.h> void main() { int i,a[10][10],d,n,c=0,b,j; clrscr(); printf("\n Enter the the no of rows \n"); scanf("%d",&n); printf("\n Enter the the no of columns \n"); scanf("%d",&d); printf("\n Enter the elements \n"); for(i=0;i<=n-1;i++) { for(j=0;j<=d-1;j++) { scanf("%d",&a[i][j]); }

} printf("\n The elements are:\n"); for(i=0;i<=n-1;i++) { for(j=0;j<=d-1;j++) { printf(" \t%d\t ",a[i][j]); } printf("\n"); } printf("\n Enter the element to b searched \n"); scanf("%d",&b); for(i=0;i<=n;i++) { for(j=0;j<=d;j++) { if(a[i][j]==b) { c=1; }}} if(c==1) { printf("\n The Element %d is found \n",b); }else { printf("\n The Element %d is not found \n",b); } getch(); }

3. BINARY SEARCH:
#include<stdio.h> #include<conio.h> void main() { int i,a[10][10],d,n,target,top,bottom,mid,j,flag; clrscr(); printf("\n Enter the the no of rows \n"); scanf("%d",&n); printf("\n Enter the the no of columns \n"); scanf("%d",&d); top=0; bottom=((n*d)-1); printf("\n Enter the elements \n"); for(i=0;i<=n-1;i++) { for(j=0;j<=d-1;j++) { printf("\n Enter in position a[%d][%d]:",i,j); scanf("%d",&a[i][j]); } } printf("\n The elements are:\n"); for(i=0;i<=n-1;i++) { for(j=0;j<=d-1;j++) {

printf(" \t%d\t ",a[i][j]); } printf("\n"); } printf("\n Enter the element to b searched \n"); scanf("%d",&target); while(top<=bottom) { mid=(top+bottom)/2; if(target==a[mid/n][mid%d]) { flag=1; break; } else if(target> a[mid/n][mid%d]) top=mid+1; else bottom=mid+1; } if(flag==1) printf("\n The element %d is found in a[%d][%d]",target,mid/n,mid%d); else printf("\n The element %d is not found \n",target); getch(); }

4. TREE TRAVERSAL: #include<stdio.h> #include<conio.h> #include<alloc.h> struct bnode {int data; struct bnode *lc; struct bnode *rc; };

struct bnode *root,*temp,*mytemp,*oldmytemp; void preorder(struct bnode *temp); void inorder(struct bnode *temp); void postorder(struct bnode *temp);

void main() { char ans; clrscr(); root=(struct bnode *)malloc(sizeof(struct bnode)); printf("\n ENTER THE VALUE FOR THE FIRST NODE: \n"); scanf("%d",&root->data); root->lc=NULL; root->rc=NULL; printf("\n ARE YOU INTERESTED IN ADDING NEW NODE \n"); ans=getch(); while(ans=='y') { temp=(struct bnode *)malloc(sizeof(struct bnode));

printf("\n ENTER THE VALUE FOR THE NEW NODE \n"); scanf("%d",&temp->data); temp->lc=NULL; temp->rc=NULL; mytemp=root; while(mytemp!=NULL) { oldmytemp=mytemp; if(temp->data<mytemp->data) mytemp=mytemp->lc; else mytemp=mytemp->rc; } if(temp->data<oldmytemp->data) oldmytemp->lc=temp; else oldmytemp->rc=temp; printf("\n ARE YOU INTERESTED IN ADDING NEW NODE \n"); fflush(stdin); ans=getch(); } printf("\n PREORDER \n"); preorder(root); printf("\n INORDER \n"); inorder(root); printf("\n POSTORDER \n"); postorder(root); getch(); }

void preorder(struct bnode *temp) { if(temp!=NULL) { printf(" %d \t",temp->data); preorder(temp->lc); preorder(temp->rc); } } void inorder(struct bnode *temp) { if(temp!=NULL) { inorder(temp->lc); printf(" %d \t",temp->data); inorder(temp->rc); } } void postorder(struct bnode *temp) { if(temp!=NULL) { postorder(temp->lc); postorder(temp->rc); printf(" %d \t",temp->data); }}

5. BUCKET SORT: #include<stdio.h> #include<conio.h> #include<alloc.h> void sort(struct node *); struct node { int data; struct node *next; }; struct node *bucket[10]; struct node *head,*temp,*mytemp,*ourtemp; void main() { int i,n,value,pos; clrscr(); printf(" \n ENTER THE NUMBER OF NODES \n"); scanf("%d",&n); printf("\n ENTER YOUR %d VALUES \n",n); for(i=0;i<n;i++) { scanf("%d",&value); temp=(struct node*)malloc(sizeof(struct node)); temp->data=value; temp->next=NULL; pos=temp->data/10; if(bucket[pos]==NULL) bucket[pos]=temp;

else { mytemp=bucket[pos]; while(mytemp->next!=NULL) mytemp=mytemp->next; mytemp->next=temp; } } printf("\n THE SORTED ARRAY \n"); for(i=0;i<n;i++) sort(bucket[i]); for(i=0;i<10;i++) { mytemp=bucket[i]; while(mytemp!=NULL) { printf("%d\t",mytemp->data); mytemp=mytemp->next;} } getch(); } void sort(struct node *temp) { int t; if(temp!=NULL) { mytemp=temp; while(mytemp->next!=NULL)

{ ourtemp=temp; while(ourtemp->next!=NULL) { if(ourtemp->data>ourtemp->next->data) { t=ourtemp->data; ourtemp->data=ourtemp->next->data; ourtemp->next->data=t; } ourtemp=ourtemp->next; } mytemp=mytemp->next; } } }

6. HEAP SORT:
#include<stdio.h> #include<conio.h> void makeheap(int[],int); void heapsort(int[],int); void main() { int i,arr[10]; clrscr(); printf("\n ENTER ANY TEN VALUES TO BE SORTED \n"); for(i=0;i<=9;i++) {

scanf("%d",&arr[i]); } printf("\n HEAP SORT \n"); makeheap(arr,10); printf("\n BERFORE SORTING \n"); for(i=0;i<=9;i++) printf("%d\t",arr[i]); heapsort(arr,10); printf("\n AFTER SORTING \n"); for(i=0;i<=9;i++) printf("%d\t",arr[i]); getch(); } void makeheap(int x[],int n) { int i,val,s,f; for(i=1;i<n;i++) { val=x[i]; s=i; f=(s-1)/2; while(s>0&&x[f]<val) { x[s]=x[f]; s=f; f=(s-1)/2; } x[s]=val; }

} void heapsort(int x[],int n) { int i,s,f,ivalue; for(i=n-1;i>0;i--) { ivalue=x[i]; x[i]=x[0]; f=0; if(i==1) s=-1; else s=1; if(i>2&&x[2]>x[1]) s=2; while(s>0&&ivalue<x[s]) { x[f]=x[s]; f=s; s=2*f+1; if(s+1<=i-1&&x[s]<x[s+1]) s++; if(s>i-1) s=-i; } x[f]=ivalue; } }

7. QUICK SORT: #include<stdio.h> #include<conio.h> void quicksort(int[10],int,int); int main() { int x[20],size,i; clrscr(); printf("\n Enter size of array:"); scanf("%d",&size); printf("\n Enter the elements:"); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf(" \n Sorting elements:"); for(i=0;i<size;i++) printf(" %d ",x[i]); getch(); return 0; } void quicksort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first; j=last;

while(i<j) { while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp;

} } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } 8. INSERTION SORT: #include<stdio.h> #include<conio.h> void main() { int i,j,s,temp,a[20]; clrscr(); printf("\n Enter total numbers:");

scanf("%d",&s); printf("\n Enter %d elements:",s); for(i=1;i<=s;i++) { scanf("%d",&a[i]); } for(i=1;i<=s;i++) { for(j=i+1;j<=s;j++) { while(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } for(i=1;i<=s;i++) printf(" %d ", a[i]); getch(); }

9. PRINTING NODE LEVEL WISE #include<stdio.h> #include<conio.h> #include<math.h> void main()

{ int a[100],b[100],i,j,x=0,n,flag; clrscr(); printf("\n Enter the num. of Elements"); scanf("%d",&n); for(i=0;i<100;i++) b[i]=-1; printf("Enter the Values"); for(i=0;i<n;i++) scanf("%d",&a[i]); b[0]=a[0]; for(i=0;i<n;i++) { j=0; while(b[j]!=-1) { if(a[i]<b[j]) j=(2*j)+1; else j=(2*j)+2; } b[j]=a[i]; } for(i=0;i<n;i++) { flag=0; printf("\n\n"); for(j=0;j<pow(2,i);j++) {

if(b[x]!=-1) { printf("%d",b[x]); flag=1; } else printf("/t"); x++; } if(flag==0) break; } getch(); }

Vous aimerez peut-être aussi