//Name: Mohd Majid //Class:M.Sc.I(Sem I) // Program to implement breadth first search algorithm. #include #include <stdlib.h> #include #define TRUE 1 #define FALSE 0 const int MAX =8 ; struct node { int data ; node *next ; } ; class graph { private : int visited[MAX] ; int q[8] ; int front, rear ; public : graph( ) ; void bfs ( int v, node **p ) ; node * getnode_write ( int val ) ; static void addqueue ( int *a, int vertex, int *f, int *r ) ; static int deletequeue ( int *q, int *f, int *r ) ; static int isempty ( int *f ) ; void del ( node *n ) ; } ; graph :: graph( ) { for ( int i = 0 ; i < MAX ; i++ ) visited[i] = FALSE ; front = rear = -1 ; } void graph :: bfs ( int v, node **p ) { node *u ; visited[v - 1] = TRUE ; cout << v << "\t" ; addqueue ( q, v, &front, &rear ) ; while ( isempty ( &front ) == FALSE ) { v = deletequeue ( q, &front, &rear ) ; u = * ( p + v - 1 ) ; while ( u != NULL ) { if ( visited[u -> data - 1] == FALSE ) { addqueue ( q, u -> data, &front, &rear ) ; visited[u -> data - 1] = TRUE ; cout << u -> data << "\t" ; }
u = u -> next ; } } } node* graph :: getnode_write ( int val ) { node *newnode = new node ; newnode -> data = val ; return newnode ; } void graph :: addqueue ( int *a, int vertex, int *f, int *r ) { if ( *r == MAX - 1 ) { cout << "\nQueue Overflow." ; exit ( 0 ) ; } ( *r )++ ; a[*r] = vertex ; if ( *f == -1 ) *f = 0 ; } int graph :: deletequeue ( int *a, int *f, int *r ) { int data ; if ( *f == -1 ) { cout << "\nQueue Underflow." ; exit ( 0 ) ; } data = a[*f] ; if ( *f == *r ) *f = *r = -1 ; else ( *f )++ ; return data ; } int graph :: isempty ( int *f ) { if ( *f == -1 ) return TRUE ; return FALSE ; } void graph :: del ( node *n ) { node *temp ; while ( n != NULL ) { temp = n -> next ; delete n ; n = temp ; } } main( ) { node *arr[MAX] ; clrscr();
node *v1, *v2, *v3, *v4 ; graph g ; v1 = g.getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = v3 = g.getnode_write v3 -> next = NULL ; v1 = g.getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = v3 = g.getnode_write v3 -> next = NULL ; v1 = g.getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 3 ) ; arr[6] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = v3 = g.getnode_write v3 -> next = v4 = g.getnode_write v4 -> next = NULL ; cout << endl ; g.bfs ( 1, arr ) ; for ( int i = 0 ; i < MAX ; i++ ) g.del ( arr[i] ) ; getch(); return(0);
( 3 ) ;
( 4 ) ; ( 5 ) ;
( 6 ) ; ( 7 ) ;
( 8 ) ;
( 8 ) ;
( 8 ) ;
( 8 ) ;
( 5 ) ; ( 6 ) ; ( 7 ) ;
} //
Output:
1
2
3
4
5
6
7
8
/* NAME: Mohd Majid */ /* CLASS:M.Sc.I (Sem I) */ /* Program to search an element in an array using Binary search.*/ # include # include int binary_search(int [],int,int); main( ) { clrscr( ); const int array_size=10; int array[array_size]={0,6,9,12,20,23,29,32,47,79}; cout<<" ******************************* Binary Search ******************************"<<endl; gotoxy(1,24); gotoxy(1,5); cout<<"\n The contents of the array are : "<<endl; cout<<"\n Elements :"<<"\t\t Value:"<<endl; for(int count=0;count<array_size;count++) { cout<<"\t"<<" array ["<array[middle]) start=middle+1; middle=(start+end)/2;
} while(start<=end && array[middle]!=element); if(array[middle]==element) position=middle; return position;
} // Output: ******************************* Binary Search The contents of the array are : Elements : Value: array [0] 0 array [1] 6 array [2] 9 array [3] 12 array [4] 20 array [5] 23 array [6] 29 array [7] 32 array [8] 47 array [9] 79 Enter the element you want to find
=
******************************
79
The given element is found at the position
array[9]
//Name: Mohd Majid //Class:M.Sc.I(Sem I) // Program to implement the depth first search algorithm. #include #include #include<stdio.h> #define TRUE 1 #define FALSE 0 const int MAX = 8 ; struct node { int data ; node *next ; } ; class graph { private : int visited[MAX] ; public : graph( ) ; void dfs ( int v, node **p ) ; node* getnode_write ( int val ) ; void del ( node *n ) ; } ; graph :: graph( ) { for ( int i = 0 ; i < MAX ; i++ ) visited[i] = FALSE ; } void graph :: dfs ( int v, node **p ) { node *t ; visited[v - 1] = TRUE ; cout << v << "\t" ; t = * ( p + v - 1 ) ; while ( t != NULL ) { if ( visited[t -> data - 1] == FALSE ) dfs ( t -> data, p ) ; else t = t -> next ; } } node* graph :: getnode_write ( int val ) { node *newnode = new node ; newnode -> data = val ; return newnode ; } void graph :: del ( node *n ) { node *temp ; while ( n != NULL ) {
temp = n -> next ; delete n ; n = temp ; } } void main( ) { clrscr(); node *arr[MAX] ; node *v1, *v2, *v3, *v4 ; graph g ; v1 = g.getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = v3 = g.getnode_write v3 -> next = NULL ; v1 = g.getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = v3 = g.getnode_write v3 -> next = NULL ; v1 = g.getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 3 ) ; arr[6] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = NULL ; v1 = g.getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = g.getnode_write v2 -> next = v3 = g.getnode_write v3 -> next = v4 = g.getnode_write v4 -> next = NULL ; cout << endl ; g.dfs ( 1, arr ) ; for ( int i = 0 ; i < MAX ; i++ ) g.del ( arr[i] ) ; }
( 3 ) ;
( 4 ) ; ( 5 ) ;
( 6 ) ; ( 7 ) ;
( 8 ) ;
( 8 ) ;
( 8 ) ;
( 8 ) ;
( 5 ) ; ( 6 ) ; ( 7 ) ;
// Output: 1
2
4
8
5
6
3
7
/* NAME:Mohd Majid */ /* CLASS: M.Sc.I (Sem I)*/ /* Program that sorts an array using Heap Sort method*/ #include #include #include <stdio.h> const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int num ) ; void makeheap( ) ; void heapsort( ) ; void display( ) ; } ; array :: array( ) { count = 0 ; for ( int i = 0 ; i < MAX ; i++ ) arr[MAX] = 0 ; } void array :: add ( int num ) { if ( count < MAX ) { arr[count] = num ; count++ ; } else cout << "\nArray is full" << endl ; } void array :: makeheap( ) { for ( int i = 1 ; i < count ; i++ ) { int val = arr[i] ; int s = i ; int f = ( s - 1 ) / 2 ; while ( s > 0 && arr[f] < val ) { arr[s] = arr[f] ; s = f ; f = ( s - 1 ) / 2 ; } arr[s] = val ; } } void array :: heapsort( ) { for ( int i = count - 1 ; i > 0 ; i-- ) { int ivalue = arr[i] ;
arr[i] = arr[0] ; int f = 0 ; int s ; if ( i == 1 ) s = -1 ; else s = 1 ; if ( i > 2 && arr[2] > arr[1] ) s = 2 ; while ( s >= 0 && ivalue < arr[s] ) { arr[f] = arr[s] ; f = s ; s = 2 * f + 1 ; if ( s + 1 <= i - 1 && arr[s] < arr[s + 1] ) s++ ; if ( s > i - 1 ) s = -1 ; } arr[f] = ivalue ; } } void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "\t" ; cout << endl ; } void main( ) { array a ; clrscr(); a.add ( 11 ) ; a.add ( 2 ) ; a.add ( 9 ) ; a.add ( 13 ) ; a.add ( 57 ) ; a.add ( 25 ) ; a.add ( 17 ) ; a.add ( 1 ) ; a.add ( 90 ) ; a.add ( 3 ) ; a.makeheap( ) ; cout << "\nHeap Sort.\n" ; cout << "\nBefore Sorting:\n" ; a.display( ) ; a.heapsort( ) ; cout << "\nAfter Sorting:\n" ; a.display( ) ; }
// Output: Heap Sort. Before Sorting: 90 57 25
13
11
9
17
1
2
3
After Sorting: 1 2
9
11
13
17
25
57
90
3
NAME:Mohd Majid */ /*CLASS:M.Sc.I (SemI)*/ /* Program for merge sort*/ # include # include void merge(long [],int,int); void merge_sort(long [],int); main( ) { clrscr( ); const int array_size=10; long array[array_size]={0}; cout<<" ********************************* Merge Sort *******************************"<<endl; cout<<"\n * Array size = 10"<<endl; cout<<" * Data Type = long"<<endl; gotoxy(1,24); gotoxy(1,25); gotoxy(1,10); cout<<" Enter the array : "<<endl<<endl; for(int count_1=0;count_1<array_size;count_1++) { cout<<"\t Element["<>array[count_1]; } merge_sort(array,array_size); gotoxy(40,10); cout<<" Sorted Array : "; for(int count_2=0;count_2<array_size;count_2++) { gotoxy(50,12+count_2); cout<<"Element["<
/*
count_1++; } while(count_2<array_size_1) { temp_array[count_1]=array[count_2]; count_1++; count_2++; } while(count_3<array_size_2) { temp_array[count_1]=array[array_size_1+count_3]; count_3++; count_1++; } for(int count_4=0;count_4<array_size_1+array_size_2;count_4++) array[count_4]=temp_array[count_4]; delete temp_array;
} void merge_sort(long array[],int array_size) { if(array_size>1) { int sub_array_size_1=array_size/2; int sub_array_size_2=array_size-sub_array_size_1; merge_sort(array,sub_array_size_1); merge_sort(array+sub_array_size_1,sub_array_size_2); merge(array,sub_array_size_1,sub_array_size_2); } }
//
Output:
********************************* * Array size = 10 * Data Type = long Enter the array : Element[0] Element[1] Element[2] Element[3] Element[4] Element[5] Element[6] Element[7] Element[8] Element[9]
= = = = = = = = = =
1439 21231 44654 55465 3231 21313 6 5654654 4544 13131
Merge Sort
*******************************
Sorted Array : Element[0] = 6 Element[1] = 1439 Element[2] = 3231 Element[3] = 4544 Element[4] = 13131 Element[5] = 21231 Element[6] = 21313 Element[7] = 44654 Element[8] = 55465 Element[9] = 5654654
/* NAME: Mohd Majid */ /* CLASS:M.Sc.I(Sem I) */ /* Program to illustrate the Quick Sort.*/ # include # include void swap(long &,long &); void quick_sort(long [],int,int); main( ) { clrscr( ); const int array_size=10; long array[array_size]={0}; cout<<" ******************************** Quick Sort ******************************"<<endl; cout<<"\n * Array size = 10"<<endl; cout<<" * Data Type = long"<<endl; gotoxy(1,24); gotoxy(1,25); gotoxy(1,10); cout<<" Enter the array : "<<endl<<endl; for(int count_1=0;count_1<array_size;count_1++) { cout<<"\t Element["<>array[count_1]; } quick_sort(array,0,array_size-1); gotoxy(40,10); cout<<" Sorted Array : "; for(int count_2=0;count_2<array_size;count_2++) { gotoxy(50,12+count_2); cout<<"Element["<=last) { } else { int middle=array[last]; int count_1=first-1; int count_2=last; while(count_1
count_1++; } while(array[count_1]<middle); do { count_2--; } while(count_2>=0 && array[count_2]>middle); if(count_1
}
// Output: ******************************** * Array size = 10 * Data Type = long Enter the array : Element[0] Element[1] Element[2] Element[3] Element[4] Element[5] Element[6] Element[7] Element[8] Element[9]
= = = = = = = = = =
121 456 1212 4578 7895 4568 44464 1444 1230 1124
Quick Sort
******************************
Sorted Array : Element[0] Element[1] Element[2] Element[3] Element[4] Element[5] Element[6] Element[7] Element[8] Element[9]
= = = = = = = = = =
121 456 1124 1212 1230 1444 4568 4578 44464 7895
//Name: Mohd Majid //Class:M.Sc.I(Sem I) /* Program to search an element in an array using Linear search (or Sequential Search)*/ # include # include int linear_search(int [],int,int); main( ) { clrscr( ); const int array_size=10; int array[array_size]={25,36,2,48,0,69,14,22,7,19}; cout<<" ******************************* Linear Search ******************************"<<endl; gotoxy(1,24); gotoxy(1,5); cout<<" The contents of the array are : "<<endl; cout<<"\n Elements :"<<"\t\t Value:"<<endl; for(int count=0;count<array_size;count++) { cout<<"\t"<<" array ["<
// Output: ******************************* Linear Search The contents of the array are : Elements : Value: array [0] 25 array [1] 36 array [2] 2 array [3] 48 array [4] 0 array [5] 69 array [6] 14 array [7] 22 array [8] 7 array [9] 19
******************************
Enter the element you want to find = 2 The given element is found at the position : array[2]
//Name: Mohd Majid //Class:M.Sc.I(Sem I) //Stack implementation as a class # # # #
include include<process.h> include define SIZE 20
class stack { int a[SIZE]; int tos; public: stack(); void push(int); int pop(); int isempty(); int isfull(); }; stack::stack() { tos=0; } int stack::isempty() { return (tos==0?1:0); } int stack::isfull() { return (tos==SIZE?1:0); } void stack::push(int i) { if(!isfull()) { a[tos]=i; tos++; } else { cerr<<"Stack overflow error ! Possible Data Loss !"; } } int stack::pop() { if(!isempty()) { return(a[--tos]); } else { cerr<<"Stack is empty! What to pop...!"; }
return 0; } void main() { stack s; clrscr(); int ch=1,num; while(ch!=0) { cout<<"Stack Operations Main Menu:\n"; cout<<" 1.Push"; cout<<" \n 2.Pop"; cout<<" \n 3.IsEmpty"; cout<<"\n 4.IsFull"; cout<<"\n 0.Exit"; cout<<"\nEnter Your Choice:" ; cin>>ch; switch(ch) { case 0: exit(1); case 1: cout<<"Enter the number to push"; cin>>num; s.push(num); break; case 2: cout<<"Number popped from the stack is: "<<s.pop()<<endl; break; case 3: (s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not empty."); break; case 4: (s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full."); break; default: cout<<"Illegal Option.Please try again"; } } getch(); }
// Output: Stack Operations Main Menu: 1.Push 2.Pop 3.IsEmpty 4.IsFull 0.Exit Enter Your Choice:1 Enter the number to push34 Stack Operations Main Menu: 1.Push 2.Pop 3.IsEmpty 4.IsFull 0.Exit Enter Your Choice:2 Number popped from the stack is: 34 Stack Operations Main Menu: 1.Push 2.Pop 3.IsEmpty 4.IsFull 0.Exit Enter Your Choice:3 Stack is empty.Stack Operations Main Menu: 1.Push 2.Pop 3.IsEmpty 4.IsFull 0.Exit