Академический Документы
Профессиональный Документы
Культура Документы
#include <iostream.h> #include<conio.h> class stack { public: int top,ele,s[10]; stack(); void push(); void pop(); void list(); void display(); }; stack::stack() { top=0; } void stack::push() { clrscr(); cout<<"\n\t\t Push Operation:"; if(top==10) { cout<<"\n\t\tThe stack is full."; } else { cout<<"\nEnter the number of element to be entered:"; int m; cin>>m; cout<<"Enter the elements:"; for(int i=0;i <top;i++) { cin>>ele; s[top]=ele; top=top+1; } display(); } } void stack::pop() { clrscr(); cout<<"\n\t\t Pop Operation."; if(top==0)
{ cout<<"\n\t\t The stack is Empty."; } else { ele=s[top]; top=top-1; cout<<"\n\t\t The pop element is :"; display(); } } void stack::list() { clrscr(); cout<<"\n\t\t List operatin."; display(); } void stack::display() { if(top==0) { cout<<"\n\t\t The Stack is Empty."; } else { int i; cout<<"\n\t\t The elements are:"; for(i=0;i<=top;i++) { cout<<"\n\t\t"; } } }
void main() { int ch; char c; clrscr(); stack x=stack(); do { cout<<"\n\t\t Stack operation."; cout<<"\n\t Menu."; cout<<"\n\t 1-->Push.";
cout<<"\n\t 2-->Pop."; cout<<"\n\t 3-->List."; cout<<"\n\t Enter your choice[1/2/3]:"; cin>>ch; switch(ch) { case 1: x.push(); break; case 2: x.pop(); break; case 3: x.list(); break; default: cout<<"\n\t your choice is wrong."; } cout<<"\n\t Do you want to continue[y/n]:"; cin>>c; } while(c=='y'||c=='Y'); }
tmp = tmp->Next(); } while ( tmp != NULL ); cout << "NULL" << endl; } } void List::Append(int data) { Node* newNode = new Node(); newNode->SetData(data); newNode->SetNext(NULL); Node *tmp = head; if ( tmp != NULL ) { while ( tmp->Next() != NULL ) { tmp = tmp->Next(); } tmp->SetNext(newNode); } else { head = newNode; } } void List::Delete(int data) { Node *tmp = head; if ( tmp == NULL ) return; if ( tmp->Next() == NULL ) { delete tmp; head = NULL; } else { // Parse thru the nodes Node *prev; do {
if ( tmp->Data() == data ) break; prev = tmp; tmp = tmp->Next(); } while ( tmp != NULL ); prev->SetNext(tmp->Next()); delete tmp; } } int main() { List list; list.Append(100); list.Print(); list.Append(200); list.Print(); list.Append(300); list.Print(); list.Append(400); list.Print(); list.Append(500); list.Print(); list.Delete(400); list.Print(); list.Delete(300); list.Print(); list.Delete(200); list.Print(); list.Delete(500); list.Print(); list.Delete(100); list.Print(); }
break; case 3: cout<<"\n\t\a\a****IN-ORDER TRAVERSAL OF A TREE****\a\a"; inorder(tree); break; case 4: cout<<"\n\t\a\a****POST-ORDER TRAVERSAL OF A TREE****\a\a"; postorder(tree); break; case 5: exit(0); } }while(ch!=5); } node *insert(node *tree,int ele) { if(tree==NULL) { tree=new node; tree->left=tree->right=NULL; tree->data=ele; count++; } else if(count%2==0) tree->left=insert(tree->left,ele); else tree->right=insert(tree->right,ele); return(tree); } void preorder(node *tree) { if(tree!=NULL) { cout<<tree->data; preorder(tree->left); preorder(tree->right); getch(); } }
void inorder(node *tree) { if(tree!=NULL) { inorder(tree->left); cout<<tree->data; inorder(tree->right); getch(); } } void postorder(node *tree) { if(tree!=NULL) { postorder(tree->left); postorder(tree->right); cout<<tree->data; getch(); }
Output:-
queue[ rear ] = start; while(front <= rear) { start = queue[front]; front + +; for( i =1; i <= n; i + + ) { if(a[ start ][ i ] = =1 && visited[ i ] = = 0) { cout<<"->"<<i; visited[ i ] =1; rear + +; queue [ rear ] = i; } } } } void main ( ) { breadth ob; int start; clrscr ( ); ob.init ( ); ob.input ( ); ob.bfs ( ); getch ( ); }
Output:-
Mergesort :#include <iostream.h> #include<conio.h> int a[50]; void merge(int,int,int); void merge_sort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; merge_sort(low,mid); merge_sort(mid+1,high); merge(low,mid,high); } } void merge(int low,int mid,int high) { int h,i,j,b[50],k; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j]; j++; } i++; } if(h>mid) { for(k=j;k<=high;k++)
{ b[i]=a[k]; i++; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i++; } } for(k=low;k<=high;k++) a[k]=b[k]; } void main() { int num,i;
cout<<endl<<endl; cout<<"ENTER THE NUMBER OF ELEMENTS YOU WANT TO SORT [THEN PRESS ENTER]:"<<endl; cin>>num; cout<<endl; cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl; for(i=1;i<=num;i++) { cin>>a[i] ; } merge_sort(1,num); cout<<endl; cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl; cout<<endl<<endl; for(i=1;i<=num;i++)
cout<<a[i]<<"
";
cout<<endl<<endl<<endl<<endl; }
Output:-
Heapsort :#include <iostream.h> #include<conio.h> const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int num ) ; void makeheap(int ) ; 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(int c) { for ( int i = 1 ; i < c ; 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] ; arr[0]=ivalue; makeheap(i); } } void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "\t" ; cout << endl ; } void main( ) { array a ; 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(10) ; cout << "\nHeap Sort.\n" ; cout << "\nBefore Sorting:\n" ; a.display( ) ; a.heapsort( ) ; cout << "\nAfter Sorting:\n" ; a.display( ) ; }
Output:-
} node* btree :: buildtree ( char *a, int *l, int *r, int index ) { node *temp = NULL ; if ( index != -1 ) { temp = new node ; temp -> left = buildtree ( a, l, r, * ( l + index ) ) ; temp -> data = * ( a + index ) ; temp -> right = buildtree ( a, l, r, * ( r + index ) ) ; } return temp ; } void btree :: display( ) { inorder ( root ) ; } void btree :: inorder ( node *sr ) { if ( sr != NULL ) { inorder ( sr -> left ) ; cout << sr -> data << "\t" ; inorder ( sr -> right ) ; } } btree :: ~btree( ) { delete arr ; delete lc ; delete rc ; del ( root ) ; } void btree :: del ( node *sr ) { if ( sr != NULL ) { del ( sr -> left ) ; del ( sr -> right ) ; }
delete sr ; } void main( ) { char a[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' } ; int l[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 } ; int r[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 } ; int sz = sizeof ( a ) ; btree bt ( a, l, r, sz ) ; bt.insert( 0 ) ; cout << "\nIn-order Traversal: " << endl ; bt.display( ) ; }
Output:-
Sparse Matrices
#include < iostream.h > #include < conio.h > #define x 25 class sparce { private: int a [ x ] [ x ], b [ x ] [ x ], c [ x ] [ x ], m, n, p, q; public: void init ( ); void input ( ); void add ( ); void mul ( ); void display ( int [25][25], int, int ); void convert( int [25][25], int, int ); }; void sparce :: init ( ) { int i, j; for(i = 0; i < x;i + + ) for( j = 0; j < x; j + +) c [ i ] [ j ] = 0; } void sparce :: input() { int i,j; cout<<"\nEnter order Of First matrix::"; cin>>m>>n; cout<<"\nEnter order Of Second matrix::"; cin>>p>>q; cout<<"\nEnter"<<m*n<<"Elements Into First Matrix\n"; for(i=0;i<m;i++) for( j = 0; j < n; j + + ) cin>> a[ i ] [ j ]; cout<<"\nEnter"<<p*q<<"Elements Into Second Matrix\n"; for(i = 0; i < p ; i + + ) for ( j = 0; j < q ; j + + ) cin>>b [ i ] [ j ]; }
void sparce :: add ( ) { int i, j; if( m = = p && n = = q ) { for( i = 0 ; i < m ; i + + ) for( j = 0; j < n; j + + ) c[ i ] [ j ] = a [ i ][ j ] + b [ i ] [ j ]; convert( c, m, n); } else cout<<"\nAddition Is Not Possible"; } void sparce :: mul ( ) { int i, j, k; if(n = = p) { for( i = 0; i < m; i + +) for( j = 0; j < q; j + + ) for( k = 0; k < n; k + + ) c[ I ] [ j ] + = a [ I ] [ k ] * b [ k ] [ j ]; convert(c, m, n); } else cout<<"\n Multiplecation Is Not Possible"; } void sparce :: display(int c[25][25], int m, int n) { int i,j; for( i = 0 ;i < m; i + + ) { for( j = 0 ; j < n ; j + + ) cout<<c [ i ] [ j ]<<"\t"; cout<<"\n"; } } void sparce :: convert(int c[25][25], int m, int n) { int i, j, k = 1,t = 0;
int sp[25][25]; for( i = 0 ; i < m ; i + +) for( j = 0 ; j < n ; j + + ) if(c [ i ] [ j ] ! = 0 ) { sp [ k ] [ 0 ] = i; sp [ k ] [ 1 ] = j; sp [ k ] [ 2 ] = c [ i ] [ j ]; k++; t++; } sp[ 0 ] [ 0 ] = m; sp[ 0 ] [ 1 ] = n; sp[ 0 ] [ 2 ] = t; display( sp, k, 3); } void main ( ) { sparce ob; clrscr ( ); ob.init ( ); ob.input ( ); cout<<"\nAddition of Two Sparce Matrix\n"; ob.add ( ); ob.init ( ); cout<<"\nMultiplecation Of Two Sparce Matrix\n"; ob.mul ( ); getch ( ); }
Output:-
Operations on Doubly Linked List And Circular List With A Test Application
#include<iostream.h> #include<conio.h> #include<process.h> #include<alloc.h> class dlist { private: struct list { int data; struct list *next,*prev; }*start,*temp,*curr,*add,*addr,*tem; public: void init(); void creat(); void display(); list *search(int); void insert(); void del(); }; void dlist::init() { start=temp=curr=NULL; } void dlist::creat() { char ch; temp=new list; cout<<"\nENTER DATA TO BE STORED :: "; cin>>temp->data; cout<<"\n STARTING NODE ADDRESS :: "<<temp<<"\n"; temp->next=NULL; temp->prev=NULL; start=curr=temp; cout<<"\n DO YOU WANT TO INSERT ANOTHER NODE (y/n) :: "; cin>>ch; while(ch=='y') { temp=new list; cout<<"\nENTER DATA TO BE STORED :: "; cin>>temp->data; temp->next=NULL; temp->prev=curr; curr->next=temp; curr=temp;
cout<<"\nDO YOU WANT TO INSERT ANOTHER NODE (y/n) :: "; cin>>ch; } } void dlist::display() { if(start==NULL) cout<<"\n LIST IS EMPTY...."; else { cout<<"\n DATA PRESENT IN A LIST\n:::"; temp=start; while(temp->next!=NULL) { cout<<"|"<<temp->prev<<"|"<<temp->data<<"|"<<temp->next<<"|-->"; temp=temp->next; } cout<<"|"<<temp->prev<<"|"<<temp->data<<"|"<<temp->next<<"|"; } } dlist::list *dlist::search(int key) { temp=start; while(temp->next!=NULL) { if(temp->data==key) return temp; else temp=temp->next; } if(temp->next==NULL) { if(temp->data==key) return temp; else return NULL; } return NULL; } void dlist::insert() { int key; cout<<"\nENTER DATA AFTER WHICH WE CAN INSERT A NEW NODE :: "; cin>>key;
add=search(key); if(add==NULL) cout<<"\n NODE IS NOT FOUND....."; else tem=new list; void main() { dlist ob; int key,ch; dlist::list *temp; clrscr(); cout<<"**********DOUBLE LINKED LIST**********"; cout<<"\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Search\n6.Exit\n"; do { cout<<"\nENTER YOUR CHOICE :: "; cin>>ch; switch(ch) { case 1:ob.creat(); break; case 2:ob.display(); break; case 3:ob.insert(); break; case 4:ob.del(); break; case 5:cout<<"\n ENTER SEARCH ELEMENT :: "; cin>>key; temp=ob.search(key); if(temp==NULL) cout<<"\n ELEMENT IS NOT FOUND...."; else cout<<"\n ELEMENT IS FOUND....."; break; case 6:exit(0); default:cout<<"\n INVALID CHOICE...."; } }while(ch!=6); getch(); }
Output:-
cout<<"\nENTER ADJACENCY MATRIX ::\n"; for( i = 0; i < n; i + + ) for( j = 0; j < n; j + + ) { cout<<"Enter "<<i+1<<" To "<<j+1<<" Node Distance"; cin>>a[ i ] [ j ]; } ob.spath(a, n); getch ( ); }
Output:-