Вы находитесь на странице: 1из 47

RAJ KUMAR GOEL ENGINEERING COLLEGE

DATA STRUCTURE FILE


Submitted To:Submitted By:-

Mr. ASHWANI KUMAR SANKET GUPTA MISHRA EC - 0730931047

INDEX
1. Traversing a Linear Array 2. Insertion into a Linear Array 3. Deletion from a Linear Array 4. Implementation of Bubble Sort 5. Implementation of Linear Search 6. Implementation of Binary Search 7. Traversing a Linked List 8. Inserting at the beginning of a Linked List 9. Inserting at the end of a Linked List 10. Deletion from a Linked List 11. Implementation of Polynomial 12. Implementation of Stack

13. Implementation 14. Implementation 15. Implementation Queue 16. Implementation Search Tree 17. Implementation 18. Implementation Sort 19. Implementation Sort 20. Implementation Sort 1. TRAVERSING A LINEAR ARRAY

of Quick Sort of Queue of Circular of Binary of Heap Sort of Insertion of Selection of Merge

#include<stdio.h> #include<conio.h> # define S 20 void creat(int*,int); void dis(int*,int); void main() { int arr[S],n; clrscr(); printf("Enter the size of array under 20: "); scanf("%d",&n); creat(arr,n); dis(arr,n); getch();

} void creat (int arr[],int n) { int i; printf("Enter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; printf("\n The given element are :"); for (i=0;i<n;i++) printf("%d\t",arr[i]); }

2. INSERTION INTO A LINEAR ARRAY


#include<process.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # define S 20 void creat(int*,int); void dis(int*,int); int insert(int*,int); void main() { int arr[S],n,res; clrscr(); printf("Enter the size of array under 20: "); scanf("%d",&n); if(n>20) { printf("\n pleas enter the size less then 20:"); exit(0); } else

{ creat(arr,n); dis(arr,n); res=insert(arr,n); dis(arr,res); getch(); } } void creat (int arr[],int n) { int i; printf("Enter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; printf("\n The given element are :"); for (i=0;i<n;i++) printf("%d\t",arr[i]); } int insert(int arr[],int n) { int pos,item,t; t=n; printf("\n Enter the position:"); scanf("%d",&pos); pos=pos-1; while (pos<=t) { arr[t+1]=arr[t]; t=t-1; } printf("Enter the value to be inserted:"); scanf("%d",&item); arr[pos]=item; n=n+1; return(n); }

3. DELETION FROM A LINEAR ARRAY


#include<process.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # define S 20 void creat(int*,int); void dis(int*,int); int delet(int*,int,int); void main() { int arr[S],n,res,pos; clrscr(); printf("\n\nEnter the size of array under 20: "); scanf("%d",&n); if(n>20) { printf("\n\n pleas enter the size less then 20:"); exit(0); } else { creat(arr,n); printf("\n\n The given element are :"); dis(arr,n); printf("\n\n Enter the position for delete a item:"); scanf("%d",&pos); res=delet(arr,n,pos); printf("\n\n After deletion the element is :"); dis(arr,res); getch(); } }

void creat (int arr[],int n) { int i; printf("\n\nEnter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; for (i=0;i<n;i++) printf("%d\t",arr[i]); } int delet(int arr[],int n,int pos) { int r; r=arr[pos-1]; printf("\n\ndeleted item is=%d",r); while (pos<n) { arr[pos-1]=arr[pos]; pos=pos+1; } n=n-1; return(n); }

4. IMPLEMENTATION OF BUBBLE SORT


#include<process.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # define S 20 void creat(int*,int); void dis(int*,int); void bul(int*,int); void main() { int arr[S],i,n; clrscr(); dk: printf("\n\nEnter the size of array under 20: "); scanf("%d",&n); if(n>20) { printf("\n\n pleas enter the size less then 20:"); goto dk; } else { creat(arr,n); printf("\n\n The given element are :\n\n"); dis(arr,n); printf("\n\n After sorting the element is"); for(i=0;i<5;i++) {delay(2); printf("..."); } bul(arr,n); dis(arr,n); getch(); } } void creat (int arr[],int n) { int i; printf("\n\nEnter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) {

int i; for (i=0;i<n;i++) printf("%d\t",arr[i]); } void bul(int arr[],int n) { int i,j,temp; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } }

5. IMPLEMENTATION OF LINEAR SEARCH


#include<process.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # define S 20 void creat(int*,int); void dis(int*,int); int lis(int*,int,int); void main()

{ int loc, arr[S],n,item; clrscr(); printf("\n\nEnter the size of array under 20: "); scanf("%d",&n); if(n>20) { printf("\n\n pleas enter the size less then 20:"); exit(0); } else { creat(arr,n); printf("\n\n The given element are :"); dis(arr,n); printf("\n\n Enter the item that to be search :"); scanf("%d",&item); loc=lis(arr,n,item); printf("\n\n the location of searched element is :%d",loc+1); getch(); } } void creat (int arr[],int n) { int i; printf("\n\nEnter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; for (i=0;i<n;i++) printf("%d\t",arr[i]); } int lis(int arr[],int n,int item) {int r; arr[n+1]=item; r=0; while (arr[r]!=item) { r=r+1; } if (r==n+1) {

printf("\n item is not found."); r=-1; } else printf("\n item is found."); return(r); }

6. IMPLEMENTATION OF BINARY SEARCH


#include<process.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # define S 20 void creat(int*,int); void dis(int*,int); int bis(int*,int,int); void main() { int loc, arr[S],n,item; clrscr(); printf("\n\nEnter the size of array under 20: "); scanf("%d",&n); if(n>20) { printf("\n\n pleas enter the size less then 20:"); exit(0); } else {

creat(arr,n); printf("\n\n The given element are :"); dis(arr,n); printf("\n\n Enter the item that to be search :"); scanf("%d",&item); loc=bis(arr,n,item); printf("\n\n the location of searched element is :%d",loc+1); getch(); } } void creat (int arr[],int n) { int i; printf("\n\nEnter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; for (i=0;i<n;i++) printf("%d\t",arr[i]); } int bis(int arr[],int n,int item) { int beg=0,end=n-1,mid,r; mid=(beg+end)/2; while (beg<=end && arr[mid]!= item) { if(item<arr[mid]) end=mid-1; else beg=mid+1; mid=(beg+end)/2; } if (arr[mid]==item) { r=mid; printf("\n item is found."); } else { r=-1; printf("\nitem is not found."); } return(r); }

7. TRAVERSING A LINKED LIST


#include<stdio.h> #include<conio.h> void creat(struct node**q,int num); void dis(struct node*q); struct node { int info; struct node*link; }; void main() { struct node*q; int i,item; clrscr(); q=NULL; printf("enter the 5 element:"); for(i=0;i<5;i++) { scanf("%d",&item); creat(&q,item); } printf("\n The given list is :"); dis(q); getch(); } void creat(struct node**q,int num) { struct node *temp,*r; if(*q==NULL) { temp=(struct node*)malloc(sizeof(struct node)); temp->info=num; temp->link=NULL;

*q=temp; } else { temp=*q; while(temp->link!=NULL) { temp=temp->link; } r=(struct node*)malloc(sizeof(struct node)) ; r->info=num; r->link=NULL; temp->link=r; } } void dis(struct node*q) { while(q!=NULL) { printf("\t %d",q->info); q=q->link; } }

8. INSERTION AT THE BEGINNING OF A LINKED LIST

#include<stdio.h> #include<malloc.h> #include<conio.h> void creat(struct node**q,int num); void addbeg(struct node**q,int num); void dis(struct node*q); struct node { int info; struct node*link; }; void main() { int item; struct node*q; q=NULL; clrscr(); printf("Enter the number:"); scanf("%d",&item); creat(&q,18); dis(q); addbeg(&q,item); dis(q); getch(); } void creat(struct node**q,int num) { struct node*temp; temp=(struct node*)malloc(sizeof(struct node)); temp->info=num; temp->link=NULL; *q=temp; } void addbeg(struct node**q,int num) { struct node*temp,*r; if (*q!=NULL) { temp=*q; r=(struct node*)malloc(sizeof(struct node)); r->info=num; r->link=temp->link; temp->link=r; } else

return; } void dis(struct node*q) { printf("\n\nThe element of list is:"); while(q!=NULL) { printf("\n\n%d",q->info); q=q->link; } }

9. INSERTION AT THE END OF A LINKED LIST


#include<stdio.h> #include<malloc.h> #include<conio.h> void creat(struct node**q,int num); void addend(struct node**q,int num); void dis(struct node*q); struct node { int info; struct node*link; };

void main() { struct node*q; int i,item,n; clrscr(); q=NULL; printf("enter the element for creating of list:"); for (i=0;i<5;i++) { scanf("%d",&item) ; creat(&q,item); } dis(q); addend(&q,45); dis(q); getch(); } void creat(struct node**q,int num) { struct node*temp,*r; if(*q==NULL) { temp=(struct node*) malloc(sizeof(struct node)); temp->info=num; temp->link=NULL; *q=temp; } else { temp=*q; while(temp->link!=NULL) { temp=temp->link; } r=(struct nod*)malloc(sizeof(struct node)); r->info=num; r->link=NULL; temp->link=r; } } void addend(struct node**q,int num) { struct node*r,*temp; temp=*q; while(temp->link!=NULL) {

temp=temp->link; } r=(struct nod*)malloc(sizeof(struct node)); r->info=num; r->link=NULL; temp->link=r; printf("\n\nThe list after add a new node:\n"); } void dis(struct node*q) { printf("\n\nThe list element is:\n\n"); while(q!=NULL) { printf("\n%d",q->info); q=q->link; } }

10. DELETION FROM A LINKED LIST


#include<stdio.h> #include<conio.h> #include<malloc.h> int delend(struct node*q); void creat(struct node**q,int num); void dis(struct node*q); struct node { int info; struct node*link; }; void main() { struct node*q; int i,item,res=0; clrscr(); q=NULL; printf("Enter the 5 element:"); for(i=0;i<5;i++) { scanf("%d",&item); creat(&q,item); }

dis(q); res=delend(q); printf("\n\nDeleted item is =%d\n\n",res); dis(q); getch(); } void creat(struct node**q,int num) { struct node*temp,*r; if (*q==NULL) { temp=(struct node*)malloc(sizeof(struct node)); temp->info=num; temp->link=NULL; *q=temp; else { temp=*q; while(temp->link!=NULL) { temp=temp->link; } r=(struct node*)malloc(sizeof(struct node)); r->info=num; r->link=NULL; temp->link=r; } int delend(struct node*q) { struct node*temp,*locp; int old=0; temp=q; if (temp==NULL) printf("undreflow"); else { while(temp->link!=NULL) { locp=temp; temp=temp->link; } old=temp->info; locp->link=temp->link; return old; } void dis (struct node*q)

{ while(q!=NULL) { printf("\t%d",q->info); q=q->link; } }

11. IMPLEMENTATION OF POLYNOMIAL


#include<stdio.h> #include<conio.h> #include <process.h> #include<stdlib.h> #define T 1 #define F 0 typedef struct node { int exp; float coeff; struct node *link; }p; void main() { p *p1; char ans; p *getpoly(); void dis(p *); printf("\n\n -:IMPLEMENTATION OF POLYNOMIAL EXPRESSION:-"); do { printf("\n\nEnter the plynomial Coefficient and Exponent of a term in desending order of exponent:-\n\n"); p1=getpoly(); clrscr(); printf("\n\nThe polynomial is:-\n\n"); dis(p1); printf("\n\nDO U WANT TO CONTINUE?(N/Y):"); ans=getche(); } while (ans=='Y'||ans=='y'); } p *getpoly() { p *temp,*new,*last;

int e,flag; float c; p *getnode(); char ans='y'; temp=NULL; flag=T; do { scanf("%f%d",&c,&e); new=getnode(); if(new==NULL) printf("\n\nmemory can not be allocate"); new->coeff=c; new->exp=e; if(flag==1) { temp=new; last=temp; flag=F; } else { last->link=new; last=new; } printf("\n\nDO U WANT TO ADD MORE TERM ?(y/n)"); ans=getche(); } while(ans=='y'); return(temp); } p *getnode() { p *temp; temp=(p *)malloc(sizeof(p)); temp->link=NULL; return(temp); } void dis( p *q) { if(q==NULL) { printf("\n\nThe polynomial is empty\n"); } printf("\n"); while(q->link!=NULL)

{ printf("%0.1fx^%d+",q->coeff,q->exp); q=q->link; } printf(" %0.1f=0",q->coeff,q->exp); getch(); }

12. IMPLEMENTATION OF STACK


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

#define SIZE 5 struct stack { int a[SIZE]; int top; }st; int stfull() { if (st.top>=SIZE-1) return 1; else return 0; } void push(int item) { st.top++; st.a[st.top]=item; } int stempty() { if (st.top==-1) return 1; else return 0; } int pop() { int item; item=st.a[st.top]; st.top--; return (item); } void dis() { int i; if(stempty()) printf("\n\nSTACK IS EMPTY!"); else { for (i=st.top;i>=0;i--) printf("\n\n%d",st.a[i]); } } void main () { int c,item;

char ans; st.top=-1; clrscr(); printf("\n\t\t-:IMPLEMENT OF STACK:-"); do { printf("\n\n -:OPTIONS:-\n\n1.PUSH \n2.POP\n3.DISPLAY\n\nENTER YOUR CHOICE:\t"); scanf("%d",&c); switch(c) { case 1: clrscr(); printf("\n\nEnter the item to be insert:-"); scanf("%d",&item); if (stfull()) printf("\n\nStack is full!"); else push(item); break; case 2: clrscr(); if (stempty()) printf("\n\nstack underflow."); else { item=pop(); printf("\n\nThe deleted item is:- %d",item); } break; case 3: clrscr(); dis(); break; default: printf("\n\nwrong choice?"); break; } printf("\n\nDO U WANT TO CONTINUE?(N/Y):"); ans=getche(); } while (ans=='Y'||ans=='y'); }

13. IMPLEMENTATION OF QUICK SORT


#include<stdio.h> #include<conio.h> # define S 20 int n,i; void creat(int a[],int n) { printf("\nEnter the elements to create a list:-\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); } void dis(int a[],int n) { for(i=0;i<n;i++) printf("%d\t",a[i]); } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp;

} int keypo(int i,int j) { return ((i+j)/2); } void qsort(int list[],int m,int n) { int key,i,j,k; if(m<n) { k=keypo(m,n); swap(&list[m],&list[k]); key=list[m]; i=m+1; j=n; while(i<=j) { while((i<=n) &&(list[i]<=key)) i++; while((j>=m) && (list[j]>key)) j--; if(i<j) swap(&list[i],&list[j]); } swap(&list[m],&list[j]); qsort(list,m,j-1); qsort(list,j+1,n); } } void main() { int a[S]; clrscr(); printf("\n\nEnter the size of list is lessthen 20:-"); scanf("%d",&n); creat(a,n); printf("\n\nThe list befor sorting is:-\n\n"); dis(a,n); qsort(a,0,n-1); printf("\n\nThe list after sorting is:-\n\n"); dis(a,n); getch(); }

14. IMPLEMENTATION OF QUEUE


#include<stdio.h> #include<conio.h> #include<stdlib.h> #define SIZE 5 struct queue { int q[SIZE]; int front,rear; }q; qfull() { if (q.rear>=SIZE-1) return 1; else return 0; } int insert(int item) { clrscr(); if (q.front==-1) q.front++; q.q[++q.rear]=item; return q.rear; } int qempty() { if ((q.front==-1)||(q.front>q.rear)) return 1; else return 0; } int delete() {

int item; item=q.q[q.front]; q.front++; printf("\n\nthe deleted item is %d",item); return q.front; } void dis() { int i; for(i=q.front ;i<=q.rear;i++) printf(" %d",q.q[i]); } void main () { int c,item; char ans; clrscr(); q.front=-1; q.rear=-1; do { printf("\n\n -:OPTIONS:-\n\n1.INSERTION \n2.DELETION\n3.DISPLAY\n\nENTER YOUR CHOICE:\t"); scanf("%d",&c); switch(c) { case 1: if(qfull()) printf("\n\nU can not insert the item."); else { printf("\n\nEnter the number to be insert:\t"); scanf("%d",&item); insert(item); } break; case 2: if (qempty()) printf("\n\nQueue underflow."); else clrscr(); delete(); break; case 3: if (qempty()) printf("\n\nQueue is empty.");

else dis(); break; default: printf("\n\nwrong choice?"); break; } printf("\n\nDO U WANT TO CONTINUE?(N/Y):"); ans=getche(); } while (ans=='Y'||ans=='y'); }

15. IMPLEMENTATION OF CIRCULAR QUEUE


#include<stdio.h> #include<conio.h> #include<stdlib.h> #define SIZE 5 int q[SIZE];

int front=-1; int rear=0; int qfull() { if (front==(rear+1)%SIZE) return 1; else return 0; } int qempty() { if (front==-1) return 1; else return 0; } void add(int item ) { if (qfull()) printf("\n\nThe circular queue is full."); else { if (front==-1) front =rear=0; else rear=(rear+1)%SIZE; q[rear]=item; } } void delete() { int item; if (qempty()) printf("\n\nQUEUE IS EMPTY."); else item=q[front]; if (front==rear) { front=rear=-1; } else front=(front+1)%SIZE; printf("\n\nthe deleted item is %d",item); } void dis() {

int i; if(qempty()) { printf("\n\nThe Queue is empty."); return; } i=front; while(i!=rear) { printf(" %d",q[i]); i=(i+1)%SIZE; } printf("\n\n%d",q[i]); } void main () { int c,item; char ans; clrscr(); do { printf("\n\n -:OPTIONS:-\n\n1.INSERTION \n2.DELETION\n3.DISPLAY\n\nENTER YOUR CHOICE:\t"); scanf("%d",&c); switch(c) { case 1: clrscr(); printf("\n\nENTER THE ELEMENT:\t"); scanf("%d",&item); add(item); break; case 2: clrscr(); delete(); break; case 3: clrscr(); dis(); break; default: printf("\n\nwrong choice?"); break; } printf("\n\nDO U WANT TO CONTINUE?(N/Y):"); ans=getche();

} while (ans=='Y'||ans=='y'); }

16. IMPLEMENTATION OF BINARY SEARCH TREE


#include<stdio.h> #include<conio.h> #include <process.h> struct node { int info; struct node *lchild,*rchild; }; int count(struct node *q) { if(q==NULL) return(0); else if(q->lchild==NULL && q->rchild==NULL) return(1); else return(1+(count(q->lchild)+count(q->rchild))); } struct node *search(struct node *q,int key) { struct node *temp; temp=q; while(temp!=NULL)

{ if(temp->info==key) return(temp); else if(temp->info>key) temp=temp->lchild; else temp=temp->rchild; } return(NULL); } struct node *find(struct node *q,int key,struct node **y) { struct node *temp; if(q==NULL) return (NULL); temp=q; *y=NULL; while(temp!=NULL) { if(temp->info==key) { printf("\n\nDeletion successful."); return (temp); } else { *y=temp; if(temp->info>key) temp=temp->lchild; else temp=temp->rchild; } } return (NULL); } struct node *del(struct node *q,int num) { struct node *x,*y,*temp; x=find(q,num,&y); if(x==NULL) { printf("\n\nThe node does not exist in tree.\n"); return(q); } else

{ if(x==q) { temp=x->lchild; y=x->rchild; q=temp; while(temp->rchild!=NULL) temp=temp->rchild; temp->rchild=y; free(x); return(q); } if(x->lchild!=NULL && x->rchild!=NULL) { if(y->lchild==x) { temp=x->lchild; y->lchild=x->lchild; while(temp->rchild!=NULL) temp=temp->rchild; temp->rchild=x->rchild; x->lchild=x->rchild=NULL; } else { temp=x->rchild; y->rchild=x->rchild; while(temp->lchild!=NULL) temp=temp->lchild; temp->lchild=x->lchild; x->lchild=x->rchild=NULL; } free(x); return(q); } if(x->lchild==NULL && x->rchild!=NULL) { if(y->lchild==x) y->lchild=x->rchild; else y->rchild=x->rchild; x->rchild=NULL; free(x); return(q); } if(x->lchild!=NULL && x->rchild==NULL)

{ if(y->lchild==x) y->lchild=x->lchild; else y->rchild=x->lchild; x->lchild=NULL; free(x); return(q); } if(x->lchild==NULL && x->rchild==NULL) { if(y->lchild==x) y->lchild=NULL; else y->rchild=NULL; free(x); return(q); } } } struct node *insert(struct node *q,int num) { struct node *temp,*r; if(q==NULL) { q=(struct node*)malloc (sizeof(struct node)); if (q==NULL) { printf("\n\nmemory not alxate"); exit (0); } q->info=num; q->lchild=q->rchild=NULL; } else { temp=q; while(temp!=NULL) { r=temp; if(temp->info>num) temp=temp->lchild; else temp=temp->rchild; } if(r->info>num)

{ r->lchild=(struct node*)malloc(sizeof(struct node)); r=r->lchild; if (r==NULL) { printf("\n\nmemory not alxate"); exit (0); } r->info=num; r->lchild=r->rchild=NULL; } else { r->rchild=(struct node*)malloc(sizeof(struct node)); r=r->rchild; if (r==NULL) { printf("\n\nmemory not allocate"); exit (0); } r->info=num; r->lchild=r->rchild=NULL; } } return (q); } void inorder(struct node*q) { if(q!=NULL) { inorder(q->lchild); printf("%d\t",q->info); inorder(q->rchild); } } void preorder(struct node*q) { if(q!=NULL) { printf("%d\t",q->info); preorder(q->lchild); preorder(q->rchild); } } void postorder(struct node*q) {

if(q!=NULL) { postorder(q->lchild); postorder(q->rchild); printf("%d\t",q->info); } } void main() { struct node *root=NULL,*res=NULL; int n,item; char ans; clrscr(); printf("IMPLEMENTATION OF BST"); do { printf(" -:OPTION:-\n\n1.DO U WANT TO CREAT A BST.\n\n2.INSERT TO A NEW ITEM IN BST.\n\n3.INORDER TRAVERSING\n\n4.PREORDER TRAVERSING\n\n5.POSTORDER TRAVERSING\n\n6.COUNT THE NUMBER OF NODES\n\n7.SEARCH TO AN ITEM\n\n8.DELETE THE NODE \n\nENTER YOUR CHOICE:-"); scanf("%d",&n); switch(n) { case 1: clrscr(); printf("\n\nEnter the number of nodes:-"); scanf("%d",&n); printf("\n\nEnter the data value:-\n\n"); while(n>0) { scanf("%d",&item); root=insert(root,item); n=n-1; } break; case 2: clrscr(); printf("\n\nEnter the number of nodes:-"); scanf("%d",&n); printf("\n\nEnter the data value:-\n\n"); while(n>0) { scanf("%d",&item); root=insert(root,item); n=n-1;

} break; case 3: clrscr(); printf("\n\nINORDER TRAVERSING (ORDER:- LEFT->> ROOT->> RIGHT -) IS:-\n\n"); inorder(root); break; case 4: clrscr(); printf("\n\nPREORDER TRAVERSING (ORDER:- ROOT->> LEFT->> RIGHT -) IS:-\n\n"); preorder(root); break; case 5: clrscr(); printf("\n\nPOSTORDER TRAVERSING (ORDER:- LEFT->> RIGHT>> ROOT -) IS:-\n\n"); postorder(root); break; case 6: clrscr(); n=count(root); printf("The number of nodes in tree is=%d",n); break; case 7: clrscr(); printf("\n\nEnter the item that is to be search:- "); scanf("%d",&item); res=search(root,item); if(res!=NULL) printf("\n\nThe item is found:-\n\n"); else printf("\n\nThe item is not found:-\n\n"); break; case 8: clrscr(); printf("\n\n Enter the item that to be delete:-"); scanf("%d",&item); root= del(root,item); break; default: printf("\n\nwrong choice?"); break; } printf("\n\nDO U WANT TO CONTINUE?(N/Y):"); ans=getche();

} while (ans=='Y'||ans=='y'); }

17.

IMPLEMENTATION OF HEAP SORT

#include<stdio.h> #include<conio.h> # define S 20 int n,i; void creat(int a[],int n) { printf("\nEnter the elements to create a list:-\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); } void dis(int a[],int n) { for(i=0;i<n;i++) printf("%d\t",a[i]); } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; } void adjust(int list[],int i,int n) { int j,k,flag; k=list[i];

flag=1; j=2*i; while(j<=n && flag) { if (j<n && list[j] < list[j+1]) j++; if (k>=list[j]) flag=0; else { list[j/2]=list[j]; j=j*2; } } list[j/2]=k; } void build_initial_heap(int list[],int n) { for(i=(n/2);i>=0;i--) adjust(list,i,n-1); } void heapsort(int list[],int n) { build_initial_heap(list,n); for(i=(n-2);i>=0;i--) { swap(&list[0],&list[i+1]); adjust(list ,0,i); } } void main() { int a[S]; clrscr(); printf("\n\nEnter the size of list is lessthen 20:-"); scanf("%d",&n); creat(a,n); printf("\n\nThe list befor sorting is:-\n\n"); dis(a,n); heapsort(a,n); printf("\n\nThe list after sorting is:-\n\n"); dis(a,n); getch(); }

18. IMPLEMENTATION OF INSERTION SORT


#include<stdio.h> #include<conio.h> #define S 20 void creat(int*,int); void dis(int*,int); void insertionsort(int*,int); void main() { int arr[S],n; clrscr(); printf("enter the size of array under 20:"); scanf("%d",&n); creat(arr,n); printf("\n\n"); dis(arr,n); insertionsort(arr,n); printf("\n\nafter insertionsort "); dis(arr,n); getch(); } void creat(int arr[],int n) { int i; printf("\n\nEnter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; printf("the given number are:\n\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); } void insertionsort(int arr[],int n) { int k,temp,j; for(k=1;k<=n-1;k++)

{ temp=arr[k]; j=k-1; while(temp<arr[j]&& j>=0) { arr[j+1]=arr[j]; j=j-1; } arr[j+1]=temp; } }

19. IMPLEMENTATION OF SELECTION SORT


#include<stdio.h> #include<conio.h> #define S 20 void creat(int*,int);

void dis(int*,int); void selsort(int*,int); void main() { int arr[S],n; clrscr(); printf("enter the size of array under 20:"); scanf("%d",&n); creat(arr,n); dis(arr,n); selsort(arr,n); printf("\n\nAFTER SELECTION SORT LIST IS:-"); dis(arr,n); getch(); } void creat(int arr[],int n) { int i; printf("\n\nEnter the element:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void dis(int arr[],int n) { int i; printf("\n\nthe given number are:\n\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); } void selsort(int arr[],int n) { int temp,sm,loc,i,j; for(i=1;i<=n-1;i++) { sm=arr[i-1]; loc=i-1; for(j=i;j<=n-1;j++) { if(arr[j]<sm) { sm=arr[j]; loc=j; } } if(loc!=i-1) {

temp=arr[i-1]; arr[i-1]=arr[loc]; arr[loc]=temp; } } }

20. IMPLEMENTATION OF MERGE SORT


#include<stdio.h> #include<conio.h> # define S 20 int l,i,j,n; void creat(int a[],int n) { printf("\nEnter the elements to create a list:-\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); } void dis(int a[],int n) { for(i=0;i<n;i++) printf("%d\t",a[i]); } void merge(int a[],int b[],int k,int m,int n)

{ i=k; j=m+1; while(i<= m && j<=n) { if (a[i]<=a[j]) { b[k]=a[i]; i++; k++; } else { b[k]=a[j]; k++; j++; } } while(i<=m) { b[k]=a[i]; i++; k++; } while(j<=n) { b[k]=a[j]; j++; k++; } } void mpass(int a[],int b[],int l,int n) { int i; i=0; while(i<=(n-2*l+1)) { merge(a,b,i,(i+l-1),(i+2*l-1)); i=i+2*l; } if((i+l-1)<n) merge(a,b,i,(i+l-1),n); else while(i<=n) { b[i]=a[i];

i++; } } void msort(int a[],int n) { int b[S]; l=1; while(l<=n) { mpass(a,b,l,n); l=l*2; mpass(b,a,l,n); l=l*2; } } void main() { int a[S]; clrscr(); printf("\n\nEnter the size of list is lessthen 20:-"); scanf("%d",&n); creat(a,n); printf("\n\nThe list befor sorting is:-\n\n"); dis(a,n); msort(a,n-1); printf("\n\nThe list after sorting is:-\n\n"); dis(a,n); getch(); }

Sanket Gupta Email:- sanket.kinghrt@gmail.com

Social Networking Website:- www.facebook.com/sanketkinghrt

Вам также может понравиться