Академический Документы
Профессиональный Документы
Культура Документы
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]); }
{ 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); }
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); }
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; } } } }
{ 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); }
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); }
*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; } }
#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; } }
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; } }
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)
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)
#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'); }
} 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(); }
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'); }
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'); }
{ 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.
#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(); }
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) {
{ 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(); }