C++ Programs

C++ program to implement circular queue using array

#include <iostream.h> class cqueue { private : int *arr ; int front, rear ; int MAX; public : cqueue( int maxsize = 10 ) ; void addq ( int item ) ; int delq( ) ; void display( ) ; } ; cqueue :: cqueue( int maxsize ) { MAX = maxsize ; arr = […]


C++ program to implement Heap sort Algorithm

#include <iostream.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 […]


C++ program to implement Quick sort Algorithm using class

#include <iostream.h> const int MAX = 10 ; class array { private :   int arr[MAX] ; int count ; public :   array( ) ; void add ( int item ) ; int getcount( ) ; static int split ( int *, int, int ) ; void quiksort ( int lower, int upper ) […]


C++ rogram to implement merge sort

#include <iostream.h> const int MAX = 5 ; class array { private : int *a ; int size ; int count ; public : array( ) ; array ( int sz ) ; void add ( int num ) ; void display( ) ; void merge_sort(int low,int high); void merge(int low,int mid,int high); ~array( ) […]


C++ program to implement Shell sort using class

#include<iostream.h> #include<constream.h> void read(int a[10],int n) { cout<<"reading\n"; for(int i=0;i<n;i++) cin>>a[i]; } void display(int a[10],int n) { for(int i=0;i<n;i++) cout<<a[i]<<"\t"; } void shellsort(int a[10],int n) { int gap=n/2; do { int swap; do   { swap=0; for(int i=0;i<n-gap;i++) if(a[i]>a[i+gap]) { int t=a[i]; a[i]=a[i+gap]; a[i+gap]=t; swap=1; } } while(swap); } while(gap=gap/2); } void main() { int […]


C++ program to implement Insertion sort using class

#include <iostream.h> const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int item ) ; void sort( ) ; void display( ) ; } ; array :: array( ) { count = 0 ; for ( int i = […]


C++ program to implement Selection sort using class

#include <iostream.h> const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int item ) ; void sort( ) ; void display( ) ; } ; array :: array( ) { count = 0 ; for ( int i = […]


Program to implement Binary Search Algorithm

#include <iostream.h> template<class T> int binarySearch(T a[], int n, T& x) { int left = 0; // left end of segment int right = n – 1; // right end of segment while (left <= right) { int middle = (left + right)/2; // middle of segment if (x == a[middle]) return middle; if (x […]


C++ programs to implement Graph Traversal Techniques – Depth First Search/Breadth First Search

a)Program to implement Breadth first Search algorithm. #include<iostream.h> #include<conio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,queue[10],front,rear,v,visit[10],visited[10]; void main() { int m; clrscr(); cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES \n"; for(k=1;k<=m;k++) { cin >>i>>j; cost[i][j]=1; } cout <<"enter initial vertex"; cin >>v; cout <<"Visitied vertices\n"; cout << v; visited[v]=1; […]


C++ program to implement Queue using Linked Representation

#include <iostream.h> template<class T> class Node { friend LinkedQueue<T>; private: T data; Node<T> *link; }; template<class T> class LinkedQueue { public: LinkedQueue() {front = rear = 0;} // constructor ~LinkedQueue(); // destructor int IsEmpty() const {return ((front) ? 0 : 1);} T First() const; // return first element T Last() const; // return last element […]