You are on page 1of 40

LINEAR SEARCH

/*LINEAR SEARCH*/ #include<stdio.h> #include<conio.h> main() { int a[20],n,i,key,c=0; clrscr(); printf("Enter array size: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter element: "); scanf("%d",&a[i]); } printf("Enter element for search: "); scanf("%d",&key); for(i=0;i<n;i++) { if(key==a[i]) { c++; } } if(c==0)

printf("Element not found"); else printf("Element found"); getch(); } Output Enter size of array: 5 Enter element: 5 Enter element: 65 Enter element: 6 Enter element: 2 Enter element: 19 Enter element for search: 19 Element found

BINARY SEARCH
/*BINARY SEARCH*/ #include<stdio.h> #include<conio.h> main() { int value,temp,j,i,n,a[20],key,l=1,p; clrscr(); printf("Enter size of array: "); scanf("%d",&n); for(i=0;i<=n;i++)

{ printf("Enter element: "); scanf("%d",&a[i]); } flushall(); for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } flushall(); printf("The sorted order is \n"); for(i=0;i<=n;i++) { printf(" %d ",a[i]); } printf("Enter Search value: "); scanf("%d",&key);

p=key; value=binarysearch(a,p,l,n); if(value!=-1) printf("Element found "); else printf("Element not found "); getch(); } binarysearch(int a[],int key,int low,int high) { int midpos; if(low>high) return(-1); midpos=(low+high)/2; if(key==a[midpos]) return(midpos); else if(key<a[midpos]) binarysearch(a,key,low,midpos-1); else binarysearch(a,key,midpos+1,high); } Output Enter size of array: 5 Enter element: 5

Enter element: 65 Enter element: 6 Enter element: 2 Enter element: 19 The sorted order is 2 5 6 19 65 Enter search value: 5 Element found

FIBANOCCI SERIES

/*FIBANOCII SERIES*/ #include<stdio.h> #include<conio.h> main() { int n; clrscr(); printf("Enter range for fibanocii series to be printed: "); scanf("%d",&n); printf("Fibanocii series \n: "); fibanocii(n); getch(); } fibanocii(int x) {

static int f1=0,f2=1,fb; if(x==1) return; else { fb=f1+f2; f1=f2; f2=fb; printf(" %d ",fb); fibanocii(x-1); } } output Enter range for fibanocii series to be printed: 10 1 2 3 5 8 13 21 34 55 89

QUICK SORT
/*QUICKSORT*/

#include<stdio.h> #include<conio.h> main() { int i,n,a[20]; clrscr(); printf("Enter size of array: ");

scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter element: "); scanf("%d",&a[i]); } quicksort(a,0,n-1); printf("The sorted order is "); for(i=0;i<n;i++) { printf(" %d ",a[i]); } getch(); } quicksort(int a[],int lb,int ub) { int temp,i,pivot,j; if(lb<ub) { pivot=lb; i=lb; j=ub; while(i<j) { while(a[i]<=a[pivot]&&i<ub)

i++; while(a[j]>a[pivot]) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[pivot]; a[pivot]=a[j]; a[j]=temp; quicksort(a,lb,j-1); quicksort(a,j+1,ub); } return; } Output Enter size of array: 5 Enter element : 10 Enter element : 9 Enter element : 19 Enter element : 80 Enter element : 7

The sorted order is 7 9 10 19 80

BINARY SEARCH TREE

/*BINARY SEARCH TREE*/ #include<stdio.h> #include<conio.h> #define NULL 0 struct node { int data; struct node *left; struct node *right; }; struct node *root; void main() { int choice,no,x; clrscr(); init(); do { printf("\n1.Insertion\n2.Deletion"); printf("Enter choice: "); scanf("%d",&choice);

fflush(stdin); switch(choice) { case 1: printf("Enter the element to be inserted: "); scanf("%d",&no); x=insert(no); if(x==1) printf("\nElement is inserted into the tree"); else printf("\nError in insertion \n"); break; case 2: printf("\nEnter the element to be deleted \n"); scanf("%d",&no); dele(no); break; default: printf("\nIncorrect value entered"); } } while(choice<=2); getch(); } init();

empty(); insert(); dele(); init() { root=(struct node *)malloc(sizeof(struct node)); root=NULL; } int empty() { return(root==NULL); } int insert(int x) { struct node *p,*previous,*current; p=(struct node*)malloc(sizeof(struct node)); if(p==NULL) { printf("\nOut of Memory "); return 0; } if(root==NULL) { root=p; return 1;

} previous=NULL; current=root; while(current!=NULL) { previous=current; if(p->data<current->data) current=current->left; else current=current->right; } if(p->data<previous->data) previous->left=p; else previous->right=p; return 1; } dele(int x) { struct node *ptr=root,*parent=NULL,*t2,*t1,*temp; while(ptr!=NULL&&ptr->data!=x) { parent=ptr; if(x<ptr->data) ptr=ptr->left;

else ptr=ptr->right; } if(ptr==NULL) { printf("\n element is deleted "); return; } if(ptr->left==NULL) t1=ptr->right; else if(ptr->right==NULL) t1=ptr->left; else { t2=ptr; t1=ptr->right; temp=t1->left; while(temp!=NULL) { t2=t1; t1=temp; temp=t1->left; } if(t2!=ptr) {

t2->left=t1->right; t1->right=ptr->right; } t1->left=ptr->left; } if(parent==NULL) root=t1; else { if(parent->left==ptr) parent->left=t1; else parent->right=t1; } free(ptr); }

OUTPUT: 1.Insertion 2.Deletion Enter choice: 1 Enter element to be inserted: 5 Element inserted into the tree 1.Insertion 2.Deletion Enter choice: 2 Enter element to be deleted: 5

Element deleted 1.Insertion 2.Deletion Enter choice: 3 Incorrect value entered

INFIX TO POSTFIX CONVERSION USING STACK


/*INFIX TO POSTFIX CONVERSION USING STACK*/ #include<stdio.h> #include<string.h> #define size 10 char stack[size]; int tos=0,ele; void push(); char pop(); void show(); int isempty(); int isfull(); char infix[20],output[20]; int prec(char); main() { int i=0,j=0,k=0,length; char temp; clrscr(); printf("Enter infix expression: "); scanf("%s",infix);

printf("\nThe infix expression is %s",infix); length=strlen(infix); for(i=0;i<length;i++) { if(infix[i]!='+'&&infix[i]!=''&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='^'&&infix[i]!=')'&&infix[i]!='(') { output[j++]=infix[i]; printf("\nThe element added is %d",infix[i]); } else { if(tos==0) { push(infix[i]); printf("\nPushed element is %c",infix[i]); } else { if(infix[i]!=')'&&infix[i]!='(') { if(prec(infix[i]<=prec(stack[tos-1]))) { temp=pop(); printf("\nThe poped element is %c",temp); output[j++]=temp;

push(infix[i]); printf("\nThe pushed element is %c",infix[i]); show(); } else { push(infix[i]); printf("\nThe pushed element is %c",infix[i]); show(); } } else { if(infix[i]=='(') { push(infix[i]); printf("\nThe pushed element is %c",infix[i]); } if(infix[i]==')') { temp=pop(); while(temp!='(') { output[j++]=temp; printf("\nThe element added is %c",temp);

temp=pop(); printf("\nThe poped element is %d",temp); temp=pop(); } } } } } printf("\nThe infix expression is %s",output); } while(tos!=0) { output[j++]=pop(); } printf("The postfix expression is %s",output); getch(); } void push(int ele) { stack[tos]=ele; tos++; } char pop() { tos--;

return(stack[tos]); } void show() { int x=tos; printf("\nThe Stack elements are: "); while(x!=0) printf(" %c ",stack[--x]); } int prec(char symbol) { if(symbol=='(') return 0; if(symbol==')') return 0; if(symbol=='+'||symbol=='-') return 1; if(symbol=='*'||symbol=='/') return 2; if(symbol=='^') return 3; } OUTPUT: Enter infix expression:a+b*c/d The infix expression is a+b*c/d

The element added is a Pushed element is + The infix expression is a The element added is ab The pushed element is * The Stack elements are * + The infix expression is ab The element added is c The infix expression is abc The pushed element is / The stack elements are / * + The infix expression is abc The element added is d The infix expression is abcd The postfix expression is abcd/*+

BINARY SEARCH TREE

/*BINARY SEARCH TREE*/ #include<stdio.h> #include<conio.h> #define NULL 0 struct node { int data; struct node *left;

struct node *right; }; struct node *root; void main() { int choice,no,x; clrscr(); init(); do { printf("\n1.Insertion\n2.Deletion"); printf("Enter choice: "); scanf("%d",&choice); fflush(stdin); switch(choice) { case 1: printf("Enter the element to be inserted: "); scanf("%d",&no); x=insert(no); if(x==1) printf("\nElement is inserted into the tree"); else printf("\nError in insertion \n"); break;

case 2: printf("\nEnter the element to be deleted \n"); scanf("%d",&no); dele(no); break; default: printf("\nIncorrect value entered"); } } while(choice<=2); getch(); } init(); empty(); insert(); dele(); init() { root=(struct node *)malloc(sizeof(struct node)); root=NULL; } int empty() { return(root==NULL); }

int insert(int x) { struct node *p,*previous,*current; p=(struct node*)malloc(sizeof(struct node)); if(p==NULL) { printf("\nOut of Memory "); return 0; } if(root==NULL) { root=p; return 1; } previous=NULL; current=root; while(current!=NULL) { previous=current; if(p->data<current->data) current=current->left; else current=current->right; } if(p->data<previous->data)

previous->left=p; else previous->right=p; return 1; } dele(int x) { struct node *ptr=root,*parent=NULL,*t2,*t1,*temp; while(ptr!=NULL&&ptr->data!=x) { parent=ptr; if(x<ptr->data) ptr=ptr->left; else ptr=ptr->right; } if(ptr==NULL) { printf("\n element is deleted "); return; } if(ptr->left==NULL) t1=ptr->right; else if(ptr->right==NULL) t1=ptr->left;

else { t2=ptr; t1=ptr->right; temp=t1->left; while(temp!=NULL) { t2=t1; t1=temp; temp=t1->left; } if(t2!=ptr) { t2->left=t1->right; t1->right=ptr->right; } t1->left=ptr->left; } if(parent==NULL) root=t1; else { if(parent->left==ptr) parent->left=t1; else

parent->right=t1; } free(ptr); }

OUTPUT: 1.Insertion 2.Deletion Enter choice: 1 Enter element to be inserted: 5 Element inserted into the tree 1.Insertion 2.Deletion Enter choice: 2 Enter element to be deleted: 5 Element deleted 1.Insertion 2.Deletion Enter choice: 3 Incorrect value entered

linked list

#include<stdio.h> #include<malloc.h> struct node { int info; struct node*link;

}*start; main() { int ch,n,m,pos,i; start=NULL; while(1) { printf("\n 1.createlist\n2.add at beg\n3.add at a pos\n4.delete\n5.display\n6.count\n7.reverse\n8.search\n9.exit\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("how many nodes you want"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the element"); scanf("%d",&m); createlist(m); } break; case 2:printf("enter the element"); scanf("%d",&m); addatbeg(m); break;

case 3:printf("enter the element"); scanf("%d",&m); printf("enter the position"); scanf("%d",&pos); addatpos(m,pos); break; case 4:if(start==NULL) { printf("list is empty"); continue; } printf("enter the element for del"); scanf("%d",&m); del(m); break; case 5:display(); break; case 6:count(); break; case 7:reverse(); break; case 8:printf("enter searched element"); scanf("%d",&m); search(m); break;

default:exit(0); } } } createlist(int data) { struct node*q,*temp; temp=malloc(sizeof(struct node)); temp->info=data; temp->link=NULL; if(start==NULL) { start=temp; } else { q=start; while(q->link!=NULL) q=q->link; q->link=temp; } } addatbeg(int data) { struct node*temp;

temp=malloc(sizeof(struct node)); temp->info=data; temp->link=start; start=temp; } addatpos(int data,int pos) { struct node*temp,*q; int i; q=start; for(i=0;i<pos-1;i++) { q=q->link; if(q==NULL) { printf("there are less elements in the list"); return; } } temp=malloc(sizeof(struct node)); temp->link=q->link; temp->info=data; q->link=temp; } del(int data)

{ struct node*temp,*q; if(start->info==data) { temp=start; start=start->link; free(temp); return; } q=start; /*deleting a specific node*/ while(q->link->link!=NULL) { if(q->link->info==data) { temp=q->link; q->link=temp->link; free(temp); return; } q=q->link; } /*deleting a last node*/ if(q->link->info==data) {

temp=q->link; free(temp); q->link=NULL; return; } } display() { struct node*q; if(start==NULL) { printf("list is empty"); return; } q=start; printf("list is"); while(q!=NULL) { printf("%d\n",q->info); q=q->link; } } count() { int count=0;

struct node*q; q=start; if(start==NULL) { printf("list is empty"); } else while(q!=NULL) { count=count+1; q=q->link; } printf("count is"); printf("%d",count); } reverse() { struct node*p1,*p2,*p3; if(start->link==NULL) { return; } p1=start; p2=p1->link; p3=p2->link;

p1->link=NULL; p2->link=p1; while(p3!=NULL) { p1=p2; p2=p3; p3=p3->link; p2->link=p1; } start=p2; } search(int m) { int pos=0; struct node*q; q=start; while(q!=NULL) { pos=pos+1; if(m==q->info) { printf("search element is found"); return; } q=q->link;

} }

Output: 1.create list 2.add at beg 3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice: 1 How many nodes you want : 4 Enter the element 1 Enter the element 10 Enter the element 100 Enter the element 1000 Enter the element 10000 1.create list 2.add at beg 3.add at pos 4.delete 5.display

6.count 7.reverse 8.search 9.exit Enter your choice 2 Enter element 5 1.create list 2.add at beg 3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 5 5 1 10 100 1000 10000 1.create list 2.add at beg 3.add at pos

4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 3 Enter element 19 Enter position 2 1.create list 2.add at beg 3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 5 5 1 19 10 100 1000

10000 1.create list 2.add at beg 3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 8 Enter searched element 19 Search Element found 1.create list 2.add at beg 3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 7 1.create list 2.add at beg

3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 5 10000 1000 100 10 19 1 5 1.create list 2.add at beg 3.add at pos 4.delete 5.display 6.count 7.reverse 8.search 9.exit Enter your choice 9

You might also like