Sunteți pe pagina 1din 38

Operations On Stacks

#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'); }

Linear Linked List Implementation


#include <iostream.h> #include<conio.h> class Node { int data; Node* next; public: Node() {}; void SetData(int aData) { data = aData; }; void SetNext(Node* aNext) { next = aNext; }; int Data() { return data; }; Node* Next() { return next; }; }; class List { Node *head; public: List() { head = NULL; }; void Print(); void Append(int data); void Delete(int data); }; void List::Print() { Node *tmp = head; if ( tmp == NULL ) { cout << "EMPTY" << endl; return; } if ( tmp->Next() == NULL ) { cout << tmp->Data(); cout << " --> "; cout << "NULL" << endl; } else { do { cout << tmp->Data(); cout << " --> ";

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(); }

Operations On Ordered Binary Trees


#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *left; node *right; }; node *tree=NULL; node *insert(node *tree,int ele); void preorder(node *tree); void inorder(node *tree); void postorder(node *tree); int count=1; void main() { clrscr(); int ch,ele; do { clrscr(); cout<<"\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a"; cout<<"\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a5----EXIT.\a\a"; cout<<"\n\t\a\aENTER CHOICE::\a\a"; cin>>ch; switch(ch) { case 1: cout<<"\n\t\a\aENTER THE ELEMENT::\a\a"; cin>>ele; tree=insert(tree,ele); break; case 2: cout<<"\n\t\a\a****PRE-ORDER TRAVERSAL OF A TREE****\a\a"; preorder(tree);

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(); }

To Implement Depth First Search


#include < iostream.h > #include < conio.h > #define MAX 20 class depth { private: int a[MAX][MAX], visited[MAX]; int n, top; public: void init ( ); void input ( ); void dfs ( int ); }; void depth::init ( ) { int i, j; for( i = 0; i < MAX; i + + ) { visited[ i ] = 0; for( j =0; j < MAX ; j + + ) a[ i ] [ j ] = 0; } top = - 1; } void depth::input ( ) { int i, j; cout<<"\nENTER NUMBER OF NODES IN A GRAPH :: "; cin>>n; cout<<"\nENTER ADJACENCY MATRIX FOR A GRAPH :: \n"; for( i = 1; i <= n; i + +) for( j = 1; j <= n; j + + ) cin>>a[ i ][ j ]; } void depth::dfs ( int v) { int i; visited[v] = 1; cout<<v<<"->"; for( i = 1; i <= n; iI + + ) if( a [ v ] [ i ] = = 1 && visited [ i ] = = 0) dfs ( i ); } void main ( ) { depth ob; int start; clrscr ( ); ob.init ( ); ob.input ( ); cout<<"\nSTARTING NODE FOR DFS TRAVERSING :: ";

cin>>start; cout<<"\nDEPTH FIRST SEARCH TRAVERSING IS ::\n\n"; ob.dfs ( start ); getch ( ); }

Output:-

To Implement Breadth First Search


#include < iostream.h > #include < conio.h > #define MAX 20 class breadth { private: int a[MAX][MAX], visited[MAX], queue[50]; int n, front, rear; public: void init ( ); void input ( ); void bfs ( ); }; void breadth::init ( ) { int i, j; for( i = 0; i < MAX; i + + ) { visited [ i ] = 0; for( j = 0; j < MAX; j + + ) a[ i ] [ j ] = 0; } front = rear = - 1; } void breadth::input ( ) { int i, j; cout<<"\nENTER NUMBER OF NODES IN A GRAPH :: "; cin>>n; cout<<"\nENTER ADJACENCY MATRIX FOR A GRAPH :: \n"; for( i = 1;i <= n; i + + ) for( j = 1; j <= n; j + + ) cin>>a[ i ][ j ]; } void breadth::bfs ( ) { int i, start; cout<<"\nSTARTING NODE FOR BFS TRAVERSING :: "; cin>>start; cout<<"\n BREADTH FIRST SEARCH TRAVERSING IS:: \n \t"; cout<<start; visited[ start ] = 1; rear + +; front + +;

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<<"MERGE SORT PROGRAM"<<endl;

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:-

Operations On Binary Trees


#include <iostream.h> #include<conio.h> class btree { private : struct node { node *left ; char data ; node *right ; } *root ; char *arr ; int *lc ; int *rc ; public : btree ( char *a, int *l, int *r, int size ) ; void insert ( int index ) ; static node* buildtree ( char *a, int *l, int *r, int index ) ; void display( ) ; static void inorder ( node *sr ) ; ~btree( ) ; static void del ( node *sr ) ; }; btree :: btree ( char *a, int *l, int *r, int size ) { root = NULL ; arr = new char[size] ; lc = new int[size] ; rc = new int[size] ; for ( int i = 0 ; i < size ; i++ ) { * ( arr + i ) = * ( a + i ) ; * ( lc + i ) = * ( l + i ) ; * ( rc + i ) = * ( r + i ) ; } } void btree :: insert ( int index ) { root = buildtree ( arr, lc, rc, index ) ;

} 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:-

Shortest Path Problem


#include < iostream.h > #include < conio.h > #define INF 9999 class stpath { private: int i, j, k; public: void spath(int [ ][20], int ); void display(int [ ][20], int ); }; void stpath::spath(int a[ ][20], int n) { for( i = 0 ;i < n; I + + ) for( j = 0; j < n; j + + ) if(a[ i ] [ j ] = = 0) a[ i ][ j ] = INF; cout<<"\nADJACENCY MATRIX OF COST OF EDGES ARE ::"; display( a, n ); for( k = 0; k < n; k + + ) for( i = 0; i < n; i + + ) for( j = 0; j < n; j + + ) if( a[ i ][ j ] > a[ i ] [ k] + a[ k ][ i ]) a[ i ][ j ] = a[ i ][ k ] + a[ k ][ j ]; cout<<"\nADJACENCY MATRIX OF LOWEST COST OF EDGES ARE ::\n"; display(a, n); } void stpath::display(int a[ ] [20], int n) { for( i = 0; i < n; i + + ) { for( j = 0; j < n; j + + ) cout<<a[ i ][ j ]<<"\t"; cout<<"\n"; } } void main() { int i, j , n , a[20][20]; stpath ob; clrscr(); cout<<"\nENTER NUMBER OF NODES IN A GRAPH :: "; cin>>n;

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:-

S-ar putea să vă placă și