/*Program for binary search*/
#include <stdio.h> main() { int arr[20],start,end,middle,n,i,item; printf("How many elements you want to enter in the array : "); scanf("%d",&n); for(i=0; i < n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } printf("Enter the element to be searched : "); scanf("%d",&item); start=0; end=n-1; middle=(start+end)/2; while(item != arr[middle] && start <= end) { if(item > arr[middle]) start=middle+1; else end=middle-1; middle=(start+end)/2; } if(item==arr[middle]) printf("%d found at position %d\n",item,middle+1); if(start>end) printf("%d not found in array\n",item); }/*End of main()*/
/* Program of sorting using bubble sort */
#include <stdio.h> #define MAX 20 main() { int arr[MAX],i,j,k,temp,n,xchanges; printf("Enter the number of elements : "); scanf("%d",&n);//5 for (i = 0; i < n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } printf("Unsorted list is :\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); /* Bubble sort*/ for (i = 0; i < n-1 ; i++) { //xchanges=0; for (j = 0; j <n-1-i; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; //xchanges++; }/*End of if*/ }/*End of inner for loop*/ /*if(xchanges==0) If list is sorted break; printf("After Pass %d elements are : for (k = 0; k < n; k++) printf("%d ", arr[k]); printf("\n"); }*/ printf("Sorted list is :\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); }/*End of main()*/
",i+1);
/* Program of queue using circular linked list*/
# include <stdio.h> # include <malloc.h> struct node { int info; struct node *link; }*rear=NULL; main() { int choice; while(1) { printf("1.Insert \n"); printf("2.Delete \n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ insert() { int num; struct node *q,*tmp; printf("Enter the element for insertion : "); scanf("%d",&num); tmp= malloc(sizeof(struct node)); tmp->info = num; if(rear == NULL) /*If queue is empty */ { rear = tmp;
tmp->link = rear; } else { tmp->link = rear->link; rear->link = tmp; rear = tmp; } }/*End of insert()*/ del() { struct node *tmp,*q; if(rear==NULL) { printf("Queue underflow\n"); return; } if( rear->link == rear ) /*If only one element*/ { tmp = rear; rear = NULL; free(tmp); return; } q=rear->link; tmp=q; rear->link = q->link; printf("Deleted element is %d\n",tmp->info); free(tmp); }/*End of del()*/ display() { struct node *q; if(rear == NULL) { printf("Queue is empty\n"); return; } q = rear->link; printf("Queue is :\n"); while(q != rear) { printf("%d ", q->info); q = q->link; } printf("%d\n",rear->info); }/*End of display(
/* Program of queue using linked list*/
# include<stdio.h> # include<malloc.h> struct node { int info; struct node *link; }; *front=NULL,*rear=NULL; main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d", &choice); switch(choice) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ insert() { struct node *tmp; int added_item; tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the element for adding in queue : "); scanf("%d",&added_item);//11 tmp->info = added_item;
tmp->link=NULL; if(front==NULL) front=tmp; else rear->link=tmp; rear=tmp; }/*End of insert()*/ del() {
/*If Queue is empty*/
struct node *tmp; if(front == NULL) printf("Queue Underflow\n"); else { tmp=front; printf("Deleted element is %d\n",tmp->info); front=front->link; free(tmp); } }/*End of del()*/ display() { struct node *ptr; ptr = front; if(front == NULL) printf("Queue is empty\n"); else { printf("Queue elements :\n"); while(ptr != NULL) { printf("%d ",ptr->info); ptr = ptr->link; } printf("\n"); }/*End of else*/ }/*End of display()*/
/* Program of stack using array*/
#include<stdio.h> #define MAX 5 int top = -1; int stack_arr[MAX]; main() { int choice; while(56) { printf("1.Push\n"); printf("2.Pop\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ push() { int pushed_item; if(top == (MAX-1)) printf("Stack Overflow\n"); else { printf("Enter the item to be pushed in stack : "); scanf("%d",&pushed_item); top=top+1; stack_arr[top] = pushed_item; } }/*End of push()*/ pop() { if(top == -1)
printf("Stack Underflow\n"); else { printf("Popped element is : %d\n",stack_arr[top]); top=top-1; } }/*End of pop()*/ display() { int i; if(top == -1) printf("Stack is empty\n"); else { printf("Stack elements :\n"); for(i = top; i >=0; i--) printf("%d\n", stack_arr[i] ); } }/*End of display()*/
Program of Link List
#include <stdio.h> #include <conio.h> #include<malloc.h> struct node { int info; struct node *link; }*front=NULL,*rear=NULL; main() { int choice; while(1) { printf("1.insert\n"); printf("2.del\n"); printf("3.dis\n "); printf("4.quit\n"); printf("enter your choice"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: del(); break; case 3: dis(); break; case 4: exit(1); default : printf("wrong choice\n"); }}} insert() { struct node *tmp; int add_item; tmp=(struct node *)malloc(sizeof(struct node)); printf("input the element for adding in queue"); scanf("%d",&add_item); tmp->info=add_item; tmp->link=NULL; if(front==NULL) front=tmp;
else rear->link=tmp; rear=tmp; } del() { struct node *tmp; if(front==NULL) printf("under flow\n"); else { tmp=front; printf("delete element%d\n",tmp->info); front=front->link; free(tmp); } } dis() { struct node *ptr; ptr=front; if(front==NULL) printf("empty\n"); else { printf("que element\n"); while(ptr!=NULL) { printf("%d",ptr->info); ptr=ptr->link; } printf("\n"); } }
/* Program of circular linked list*/
# include <stdio.h> # include <malloc.h> struct node { int info; struct node *link; }*last; main() { int choice,n,m,po,i; last=NULL; while(1) { printf("1.Create List\n"); printf("2.Add at begining\n"); printf("3.Add after \n"); printf("4.Delete\n"); printf("5.Display\n"); printf("6.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { 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; 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 after which this element is inserted : "); scanf("%d",&po); addafter(m,po); break; case 4:
if(last == NULL) { printf("List underflow\n"); continue; } printf("Enter the number for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: exit(); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ create_list(int num) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info = num; if(last == NULL) { last = tmp; tmp->link = last; } else { tmp->link = last->link; /*added at the end of list*/ last->link = tmp; last = tmp; } }/*End of create_list()*/ addatbeg(int num) { struct node *tmp; tmp = malloc(sizeof(struct node)); tmp->info = num; tmp->link = last->link; last->link = tmp; }/*End of addatbeg()*/ addafter(int num,int pos) { struct node *tmp,*q; int i; q = last->link;
for(i=0; i < pos-1; i++) { q = q->link; if(q == last->link) { printf("There are less than %d elements\n",pos); return; } }/*End of for*/ tmp = malloc(sizeof(struct node) ); tmp->link = q->link; tmp->info = num; q->link = tmp; if(q==last) /*Element inserted at the end*/ last=tmp; }/*End of addafter()*/ del(int num) { struct node *tmp,*q; if( last->link == last && last->info == num) /*Only one element*/ { tmp = last; last = NULL; free(tmp); return; } q = last->link; if(q->info == num) { tmp = q; last->link = q->link; free(tmp); return; } while(q->link != last) { if(q->link->info == num) /*Element deleted in between*/ { tmp = q->link; q->link = tmp->link; free(tmp); printf("%d deleted\n",num); return; } q = q->link; }/*End of while*/ if(q->link->info == num) /*Last element deleted q->link=last*/ { tmp = q->link; q->link = last->link; free(tmp); last = q; return;
} printf("Element %d not found\n",num); }/*End of del()*/ display() { struct node *q; if(last == NULL) { printf("List is empty\n"); return; } q = last->link; printf("List is :\n"); while(q != last) { printf("%d ", q->info); q = q->link; } printf("%d\n",last->info); }/*End of display()*/
/* Program of double linked list*/
# include <stdio.h> # include <malloc.h> struct node { struct node *prev; int info; struct node *next; }*start; main() { int choice,n,m,po,i; start=NULL; while(1) { printf("1.Create List\n"); printf("2.Add at begining\n"); printf("3.Add after\n"); printf("4.Delete\n"); printf("5.Display\n"); printf("6.Count\n"); printf("7.Reverse\n"); printf("8.exit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { 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; 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 after which this element is inserted : "); scanf("%d",&po); addafter(m,po); break;
case 4: printf("Enter the element for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: count(); break; case 7: rev(); break; case 8: exit(); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ create_list(int num) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info=num; tmp->next=NULL; if(start==NULL) { tmp->prev=NULL; start->prev=tmp; start=tmp; } else { q=start; while(q->next!=NULL) q=q->next; q->next=tmp; tmp->prev=q; } }/*End of create_list()*/ addatbeg(int num) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp->prev=NULL; tmp->info=num; tmp->next=start; start->prev=tmp; start=tmp; }/*End of addatbeg()*/
addafter(int num,int c) { struct node *tmp,*q; int i; q=start; for(i=0;i<c-1;i++) { q=q->next; if(q==NULL) { printf("There are less than %d elements\n",c); return; } } tmp=malloc(sizeof(struct node) ); tmp->info=num; q->next->prev=tmp; tmp->next=q->next; tmp->prev=q; q->next=tmp; }/*End of addafter() */ del(int num) { struct node *tmp,*q; if(start->info==num) { tmp=start; start=start->next; /*first element deleted*/ start->prev = NULL; free(tmp); return; } q=start; while(q->next->next!=NULL) { if(q->next->info==num) /*Element deleted in between*/ { tmp=q->next; q->next=tmp->next; tmp->next->prev=q; free(tmp); return; } q=q->next; } if(q->next->info==num) /*last element deleted*/ { tmp=q->next; free(tmp); q->next=NULL; return; } printf("Element %d not found\n",num);
}/*End of del()*/ display() { struct node *q; if(start==NULL) { printf("List is empty\n"); return; } q=start; printf("List is :\n"); while(q!=NULL) { printf("%d ", q->info); q=q->next; } printf("\n"); }/*End of display() */ count() { struct node *q=start; int cnt=0; while(q!=NULL) { q=q->next; cnt++; } printf("Number of elements are %d\n",cnt); }/*End of count()*/ rev() { struct node *p1,*p2; p1=start; p2=p1->next; p1->next=NULL; p1->prev=p2; while(p2!=NULL) { p2->prev=p2->next; p2->next=p1; p1=p2; p2=p2->prev; /*next of p2 changed to prev */ } start=p1; }/*End of rev(
Write a program to add the two matrix .
#include<stdio.h> #include<conio.h> void main() { int m1[3][3],m2[3][3],m3[3][3]; int r,c; clrscr(); printf("\nEnter elements of matrix\n"); for(r=0;r<3;r++) //for rows { for(c=0;c<3;c++) //for col { printf("Enter a number:"); scanf("%d",&m1[r][c]); } } printf("\nEnter elements second of matrix\n"); for(r=0;r<3;r++) //for rows { for(c=0;c<3;c++) //for col { printf("Enter a number:"); scanf("%d",&m2[r][c]); } } for(r=0;r<3;r++) { for(c=0;c<3;c++) { m3[r][c]=m1[r][c]+m2[r][c]; } } //for displaying the third matrix printf("\nMatrix\n"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { printf(" %d",m3[r][c]); } printf("\n"); } getch(); }
//Write a program to multiply two of the matrix.
#include<stdio.h> #include<conio.h> void main() { int m1[3][3],m2[3][3],m3[3][3],k,t=0; int r,c; clrscr(); printf("\nEnter elements of matrix\n"); for(r=0;r<3;r++) //for rows { for(c=0;c<3;c++) //for col { printf("Enter a number:"); scanf("%d",&m1[r][c]); } } printf("\nEnter elements second of matrix\n"); for(r=0;r<3;r++) //for rows { for(c=0;c<3;c++) //for col { printf("Enter a number:"); scanf("%d",&m2[r][c]); } } for(r=0;r<3;r++) { for(c=0;c<3;c++) { for (k=0;k<3;k++) { t=t+m1[r][k]*m2[k][c]; } m3[r][c]=t; t=0; } } printf("\nMatrix\n"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { printf(" %d",m3[r][c]); } printf("\n");
} getch(); }
/* Program of merging two sorted arrays into a third sorted array*/
#include<stdio.h> main() { int arr1[20],arr2[20],arr3[40]; int i,j,k; int max1,max2; printf("Enter the number of elements in list1 : "); scanf("%d",&max1); printf("Take the elements in sorted order :\n"); for(i=0;i<max1;i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr1[i]); } printf("Enter the number of elements in list2 : "); scanf("%d",&max2); printf("Take the elements in sorted order :\n"); for(i=0;i<max2;i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr2[i]); } /* Merging */ i=0; /*Index for first array*/ j=0; /*Index for second array*/ k=0; /*Index for merged array*/ while( (i < max1) && (j < max2) ) { if( arr1[i] < arr2[j] ) arr3[k++]=arr1[i++]; else arr3[k++]=arr2[j++]; }/*End of while*/ /*Put remaining elements of arr1 into arr3*/ while( i < max1 ) arr3[k++]=arr1[i++]; /*Put remaining elements of arr2 into arr3*/ while( j < max2 ) arr3[k++]=arr2[j++]; /*Merging completed*/ printf("List 1 : "); for(i=0;i<max1;i++) printf("%d ",arr1[i]); printf("\nList 2 : "); for(i=0;i<max2;i++) printf("%d ",arr2[i]); printf("\nMerged list : ");
for(i=0;i<max1+max2;i++) printf("%d ",arr3[i]); printf("\n"); }/*End of main()*/
/*Program of sorting using quick sort through recursion*/
#include<stdio.h> #define MAX 30 enum bool { FALSE,TRUE }; main() { int array[MAX],n,i; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&array[i]); } printf("Unsorted list is :\n"); display(array,0,n-1); printf("\n"); quick(array,0,n-1); printf("Sorted list is :\n"); display(array,0,n-1); printf("\n"); }/*End of main() */ quick(int arr[],int low,int up) { int piv,temp,left,right; enum bool pivot_placed=FALSE; left=low; right=up; piv=low; /*Take the first element of sublist as piv */ if(low>=up) return; printf("Sublist : "); display(arr,low,up); /*Loop till pivot is placed at proper place in the sublist*/ while(pivot_placed==FALSE) { /*Compare from right to left */ while( arr[piv]<=arr[right] && piv!=right ) right=right-1; if( piv==right )
pivot_placed=TRUE; if( arr[piv] > arr[right] ) { temp=arr[piv]; arr[piv]=arr[right]; arr[right]=temp; piv=right; } /*Compare from left to right */ while( arr[piv]>=arr[left] && left!=piv ) left=left+1; if(piv==left) pivot_placed=TRUE; if( arr[piv] < arr[left] ) { temp=arr[piv]; arr[piv]=arr[left]; arr[left]=temp; piv=left; } }/*End of while */ printf("-> Pivot Placed is %d -> ",arr[piv]); display(arr,low,up); printf("\n"); quick(arr,low,piv-1); quick(arr,piv+1,up); }/*End of quick()*/ display(int arr[],int low,int up) { int i; for(i=low;i<=up;i++) printf("%d ",arr[i]); }