Data Structure Shorting Programs
Data Structure Shorting Programs
Binary search
#include <stdio.h> int binary_search(int array[ ],int value, int size) { int found = 0; int high = size, low = 0 mid; mid = ( high + low) / 2; printf(\n\nLooking for %d\n,value); while ((!found)&&(high>=low)) { printf(Low% dMid% High%d \n, low, mid, high); if(value == array[mid]) found = 1; else if ( value < array [mid]) high = mid 1; else low = mid 1; mid = (high + low) / 2; } return ((found)? Mid:-1); } void main ( void) { int array [100], i; for (I = 0; I < 100; i++) array[i] = I + 20; printf(Result of search %d\n, binary_search(array,33, 100)); printf(Result of search %d\n, binary_search(array,75, 100)); printf(Result of search %d\n, binary_search(array,1, 100)); printf(Result of search %d\n, binary_search(array,1001, 100)); }
scanf(%d,&position); addafter(m,position); break; case4: exit(); default: printf(enter wrong choice); create_list (int data) { struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=null; tmp=link=null; if(start==null) start=tmp; else { q=start; while(q=link!=null) q=q->link; } addbeg(int data) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=start; start=tmp; } add after (int data,int pos) { struct node *tmp,*q; int I; q=start; for(i=0;i<pos-1;i++) {
q=q->link; if(q==null) { printf(there are less than %d elements,pos); return; } tmp=malloc(sizeof(struct node)); tmp->link=q->link; tmp->info=data; q->link=tmp; }
break; case4: exit(); default: printf(enter wrong choice); create_list (int data) { struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=null; tmp=link=null; if(start==null) start=tmp; else { q=start; while(q=link!=null) q=q->link; } if(q->link->info==data) { tmp=q->link; q->link=tmp->link; free() return; }
Insertation in double linked list #include<stdio> #include<malloc.h> sturct node { struct node*prev; int info; struct node*next; }start; main() { int choice,n,m,po,I; start= NULL; while(1) {printf(1.CreateList\n); printf{2.Add at beginning\n); printf{3.Add after\n printf{4.exit\n); { case 1: printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); } break; case2: printf(enter the element); scanf(%d,&m); addbeg(m);
break; case3: printf(enter the element); scanf(%d,&m); printf(enter the position after which this element is inserted ); scanf(%d,&position); addafter(m,position); break; case4: exit(); default: printf(enter wrong choice); } } } create_list (int data) { struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=null; tmp=link=null; if(start==null) { tmp==prev=null; start=prev=tmp; start=tmp; }
tmp==prev=null; tmp==info=num; tmp==next=start; start=prev=tmp; start=tmp } add after(int num,int c) { struct node*tmp,*q; int i; for(i=0;I<n;i++) { q=q->next; if(q==null) { print(there are less than %d elements); } } tmp=malloc(sizeof(struct node)); tmp==info=num; q->nextprev=tmp; tmp=next=q=start; tmp=prev=q q=next=tmp; } return(); }
Delete an element in doubly link list #include<stdio> #include<malloc.h> sturct node { struct node*prev; int info; struct node*next; }start; main() { int choice,n,m,po,I; start= NULL; while(1) { printf(1.CreateList\n); printf(2 delete); printf{3.exit\n); { case 1: printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); } break; case2: if(start==null) { printf(list is empty) continue; } printf(enter the elementfor delete ); scanf(%d,&m); addbeg(m);
break; case3: exit(); default: printf(enter wrong choice); printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); } break; void delete_end(node*h,node*t) } if(head==(node*)null) { printf(list is empty) getch(); } if(head==tail) { free(h); head=tail=(node*)null; return; } if(t->prev==h) { head->next=null; tail=head; } else tail=tail->prev; tail->next=null;}
Search an element in doubly link list #include<studio> #include<malloc.h> sturct node { struct node*prev; int info; struct node*next; }start; main() { int choice,n,m,po,I; start= NULL; while(1) { printf(1.CreateList\n); printf(2 search); printf{3.exit\n); { case 1: printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); break; } case2: printf(enter the element for search ); scanf(%d,&m); search(m); break;
case3: exit(); default: printf(enter wrong choice); void delete_end(node*h,node*t) } if(head==(node*)null) { printf(list is empty) getch(); } if(head==tail) { free(h); head=tail=(node*)null; return; } if(t->prev==h) { head->next=null; tail=head; } else tail=tail->prev; tail->next=null; }