Practical Manual On Data Structure: Mr. Naveen Choudhary Dr. Dharm Singh
Practical Manual On Data Structure: Mr. Naveen Choudhary Dr. Dharm Singh
Practical Manual On Data Structure: Mr. Naveen Choudhary Dr. Dharm Singh
ON DATA STRUCTURE
2008
We hope that this practical manual will be helpful for students of Computer
Science & Engineering for understanding the subject from the point of view
of applied aspects
9. Write a function that will reverse a linked list while traversing it only 33
once. At the conclusion, each node should point to the node that was
previously its predecessor; the head should point to the node that was
formerly at the end, and the node that was formerly first should have a
NULL link.
10. Write an algorithm that will split a linked list into two linked lists, so 36
that successive nodes go to different lists.(the first, third, and all odd
numbered nodes to the first list, and the second, fourth, and all even-
numbered nodes go to the second.)
11. Write a function that will concatenate two circularly linked lists, 39
producing one circularly linked list.
12. Write a function that will split a circularly linked list, into two circularly 42
linked lists.
13. Write a function for manipulating a doubly linked list as follows: 45
a. add a node after p.
b. Add a node before p.
c. Delete node p.
d. traverse the list.
14. Write and test a recursive function that return the maximum among the 50
first n elements of an array.
15. Write the following function template: 51
Template <class T> void reverse(stack<T>&);
// reverse the contents of the given stack;
16. Write the following function template: 53
T& bottom (); // returns the bottom element
17. Use the stack to implement the following function: 55
Bool is palindrome (string s);
18. Write the following overloaded equality operator for queues: 57
Bool queue::operator +=(const queue&)
19. Implement the following friend function for the Integer class: 60
friend istream& operator>> (istream & istr, Integer& x);
Manual for Data Structure Page 6 of 138
// Input x from the istr input stream.
20. Write the following function that uses a binary search tree to sort an 61
array a of n elements :
void sort (type* a, int n);
Employee No Numeric
Name String
Department String
Option should be made to view the list generated. Modularize the code
to perform the above actions.
30. Write a class that has the functionality to write and read the data from a 91
file line by line.
31. Write a program that performs the matrix manipulations such as addition 93
and deletion. The matrix class will have the member functions Create,
Add, Delete, Display.
32. Write a program that will print the largest and smallest number from and 97
5 row by 5 column matrix. The matrix class will have the member
functions as Create, Display, Largest, Smallest.
33. Write a template class for sorting method. Using this class, write a test 100
program for sorting using different data types.
34. Write a program that will convert the given decimal input in to other 101
number systems Hexadecimal, Octal, and Binary. The class Conversion
will have the methods as GetNumber, Print Number, Convert To
Hexadecimal, Convert To Octal, Convert To Binary.
35. Write a class Time that will have the member functions to calculate the 105
difference and addition for given two time values. It will store the time
in hours(0-31), minutes (0-59), and seconds (0-59).
36. Write a class to handle fractions such as “1/3”. Define Addition, 108
Substraction, Multiplication, and Division operators for these fractions.
(Hint: use operator overloading).
37. Write a Date class that allows you to add, subtract, read and print simple 112
dates of the form dd/mm/yy. Considering leap year calculations will be
an added advantage.
38. Define a “string_match” base class . 115
class string_matcher
{
public :
Manual for Data Structure Page 8 of 138
// Returns TRUE if the string matches, false if not.
int match(const char string);
………….
}
Define the derived classes that match words, numbers and strings.
39. Write a base Class Number that holds a single integer value and contains 116
one member function (print it). Define three derived classes to print the
value in hex, octal, and binary.
40. Write a template min that returns the minimum of two values. Make sure 118
you handle the strings correctly.
41. Write a program to reverse a given string using stack technique. 119
42. Write a class called Clock that simulates the keeping of time. Use three 120
private class members:
hours, minutes, and seconds. Your class should be able to :
Set() that starting time.
Increment() the time by one second.
Display() the time.
The function should take an argument with a default value of zero to
imply military time. If this value is something other than zero, display
the time in standard Am and PM notation. For example, 4 minutes and
31 seconds past 7 PM should be displayed either 19:04:31 or 7:04:31
PM and 5 minutes past midnight should be displayed as either 00:05:
43. Write a class called Date that keeps track of current date. Your class 123
should be able to :
Set() the starting date.
Increment() the day by 1. If no. of days overflows then
corresponding changes in month and year should be considered.
Display() the date in MM/DD/YY format.
44. Write a program that reads in string input from the user, reverse the case 126
of the letters, and then echoes the string back to the user. The class
Reverse Case contains a character buffer up to 80 bytes in length as the
member variable and Read(), Convert() and Print() as member functions.
Define Read and print functions as inline functions.
45. Create base class media. This class has two fields one for title and the 128
other for price. Derive two specific classes called tape and book, tape
47. Implement circular link list. Include the following functions ; 133
a. Adding nodes to the list.
b. Traversing the whole list
c. Deleting a node form the list.
48-49 Write a menu-driven program which will accept an array of 10 integer 136
values and sort them with any two sorting algorithms of your choice.
//Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define INT_MAX 999
#define MAX 10
class cStack
{
int data[ MAX ];
int top;
int Empty( );
int Full( );
public:
cStack( )
{
top = 0;
}
void Push( );
void Push( int );
int Pop( );
void DispStack( );
int Peek( );
int Third( );
int Bottom( );
void Delete( int );
void Delete( );
} ;
int cStack::Empty( )
{
if( top < 0 )
return 1;
return 0;
int cStack::Full( )
{
if( top > MAX )
return 1;
return 0;
int cStack::Peek( )
{
if( !Empty( ) )
return data[ top ];
return INT_MAX;
}
int cStack::Third( )
{
{
if( data[ i ] == x )
{
for( int j = i; j <= top; j++ ) // < >
data[ j ] = data[ j + 1 ];
i--;
top--;
}
}
}
void cStack::Delete( )
{
int x;
cout << "Element to be Deleted :";
cin >> x;
for( int i = 0; i <= top; i++ ) // < >
{
if( data[ i ] == x )
{
top--;
for( int j = i; j <= top; j++ ) // < >
data[ j ] = data[ j + 1 ];
i--;
}
}
top--;
}
void cStack :: Push()
int x;
if( !Full( ) )
while( 1 )
{
cout << "Data :";
cin >> x;
if( x == 0 )
break;
else
data[ ++top ] = x;
else
cout << "Stack Overflow";
}
void cStack::Push( int x )
{
if( !Full( ) )
data[ ++top ] = x;
else
cout << "Stack Overflow";
// Example Code :
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define size 5
struct doublestack
{
int stack[ size ];
int TopA,
TopB;
} ;
void pushA( struct doublestack & s, int d )
{
s.stack[ ++( s.TopA ) ] = d;
}
void pushB( struct doublestack & s, int d )
{
s.stack[ --( s.TopB ) ] = d;
}
int popA( struct doublestack & s )
{
int popped_element = s.stack[ ( s.TopA ) ];
( s.TopA )--;
return ( popped_element );
}
int popB( struct doublestack & s )
{
int popped_element;
popped_element = s.stack[ ( s.TopB ) ];
( s.TopB )++;
return ( popped_element );
}
int empty( struct doublestack & s )
{
if( ( s.TopA == -1 ) && ( s.TopB == size - 1 ) )
return 1;
return 0;
}
int full( struct doublestack & s )
{
if( ( s.TopA + 1 == s.TopB ) || ( s.TopA >= size ) // (stack-
ptr==s+size-1)
|| ( s.TopB <= 0 ) )
return 1;
return 0;
}
void display( struct doublestack & s )
{
if( empty( s ) )
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 10
class cQueue;
class cStack
{
int data[ MAX ];
int top;
int Full( );
int Empty( );
public:
cStack( )
{
top = 0;
}
void Push( int );
int Pop( );
void DispStack( );
void Transfer( cStack & );
friend void Que_to_Stk( cStack &, cQueue & );
friend void Que_Rev( cStack &, cQueue & );
} ;
class cQueue
{
int items[ MAX ];
int front,
rear,
count;
int Full( );
int Empty( );
public:
cQueue( )
{
rear = front = count = 0;
}
void Add( int );
void DispQueue( );
int Del( );
friend void Que_to_Stk( cStack &, cQueue & );
friend void Que_Rev( cStack &, cQueue & );
} ;
int cStack::Empty( )
{
if( top < 0 )
return 1;
return 0;
}
int cStack::Full( )
{
if( top > MAX )
return 1;
return 0;
}
void cStack::Push( int x )
items[ j ] = items[ j + 1 ];
count--;
}
else
cout << "Queue is Empty ..... Delete Aborted!" << endl;
return x;
}
/* Sl.Push(14);
Sl.Push(9) ;
Sl.Push(3);
Sl.Push(2);
clrscr(); */
// Que_to_Stk(S,Q);
/* cout«"Copied Stack" « endl;
// S1.Dispstack();
// cout<< "Second Stack" << endl;
// Sl.Transfer(S); */
Que_Rev( S, Q );
cout << endl;
Q.DispQueue( );
getch( );
}
// INPUT & OUTPUT :
23 10 5 56
IN STACK REVERSE THE ORDER OF ALL THE ITEMS IN THE QUEUE
56 5 10 23
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 10
class cQueue
{
int items[ MAX ];
int front,
rear,
count;
int Full( );
int Empty( );
public:
cQueue( )
{
rear = front = count = 0;
}
void Add( );
void Add( int );
void Disp( );
int Del( );
int Peek( );
} ;
int cQueue::Full( )
{
if( count >= MAX )
return 1;
return 0;
}
int cQueue::Empty( )
{
if( count == 0 )
return 1;
return 0;
}
void cQueue::Add( )
{
int x;
if( !Full( ) )
{
count++;
cout << "Data:";
cin >> x;
items[ rear++ ] = x;
}
else
cout << "Queue Full..... Add Aborted! " << endl;
}
void cQueue::Add( int x )
{
if( !Full( ) )
{
count++;
items[ rear++ ] = x;
}
else
cout << "Queue Full..._ Add Aborted!" << endl;
}
items[ j ] = items[ j + 1 ];
count--;
}
else
cout << "Queue is Empty ..... Delete Aborted!" << endl;
return x;
}
int cQueue::Peek( )
{
if( !Empty( ) )
return items[ front ];
else
cout << "queue is Empty ..... Delete Aborted!" << endl;
return -999;
}
void cQueue::Disp( )
{
for( int i = front; i < count; i++ ) // < >
// Example Code:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 5
class cCirQueue
{
int items[ MAX ];
int front,
rear;
int Empty( );
int Full( );
public:
cCirQueue( )
{
front = 0;
rear = -1;
}
void Add( );
void Add( int );
void Disp( );
} ;
int cCirQueue::Empty( )
{
if( MAX - rear == 1 )
return 1;
return 0;
}
int cCirQueue::Full( )
{
if( MAX - rear == 2 )
return 1;
return 0;
}
void cCirQueue::Add( )
{
while( 1 )
{
int x;
cout << "Item (0-Exit) :";
cin >> x;
if( x == 0 )
break;
else
{
if( !Full( ) )
{
rear = ( rear + 1 ) % MAX;
items[ rear ] = x;
}
else
{
cout << "Queue Full my Dear ....... Aborting" <<
endl;
break;
}
Item (0-Exit) :1
Item (0-Exit) :2
Item (0-Exit) :3
Item (0-Exit) :4
Item (0-Exit) :5
Queue Full my Dear ....... Aborting
1
2
3
4
Use a queue to keep track of the left part of the line while reading the
right part
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 50
#include <stdio.h>
class cQueue
{
char items[ MAX ];
int front,
rear,
count;
int Full( );
int Empty( );
public:
cQueue( )
{
rear = front = count = 0;
}
void Add( char );
void Disp( );
char Del( );
friend char Read( cQueue & );
} ;
int cQueue::Full( )
{
if( count >= MAX )
return 1;
return 0;
}
int cQueue::Empty( )
{
if( count == 0 )
return 1;
return 0;
}
void cQueue::Add( char x )
{
if( !Full( ) )
{
count++;
items[ rear++ ] = x;
}
else
cout << "Queue Full..... Add Aborted!" << endl;
}
char cQueue::Del( )
{
char x = '!';
items[ j ] = items[ j + 1 ];
count--;
}
else
cout << "Queue is: Empty ..... Delete Aborted ! " << endl;
return x;
}
void cQueue::Disp( )
{
for( int i = front; i < count; i++ ) // < >
// right
ch = 'S';
else
{
for( ++i; str[ i ]; i++ )
if( ( i - idx - 1 ) == Q.count )
ch = 'D';
clrscr( );
char ch = Read( Q );
clrscr( );
Q.Disp( );
cout << endl << "Result : " << ch;
getch( );
}
b. Initialize
c. AddNode
d. DeleteNode
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cQueue;
class cNode
{
char name[ 15 ];
int age;
float basic;
cNode * next;
public:
cNode( )
{
strcpy( name, '\0' );
age = 0;
basic = 0.0;
next = '\0';
}
cNode( char *, int, float );
void DispNode( );
friend class cQueue;
} ;
class cQueue
{
cNode * rear;
public:
cQueue( )
{
rear = '\0';
rear->next = '\0';
}
void Add( );
void Delete( );
void DispQueue( );
}
cNode::cNode( char * n, int a, float b )
{
strcpy( name, n );
age = a;
basic = b;
next = '\0';
}
void cNode::DispNode( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( 15 ) << name;
cout.setf( ios::right, ios::adjustfield );
cout << setw( 3 ) << age << setw( 6 ) << basic << endl;
}
void cQueue::Add( )
{
char nm[ 15 ];
L.DispQueue( );
cout<<queue after deletion;
getch( );
}
// INPUT :
// OUTPUT :
enter 21 34
xyz 23 56
xyz 23 56
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 10
class cDeque
{
int items[ MAX ];
int front,
rear;
int Empty( );
int Full( );
public:
cDeque( )
{
rear = front = 0;
}
void AddRear( int );
void AddFront( int );
int DelFront( );
int DelRear( );
void Reverse( );
void Disp( );
void Split( cDeque &, cDeque & );
} ;
int cDeque::Full( )
{
if( rear > MAX )
return 1;
return 0;
}
int cDeque::Empty( )
{
if( front == rear )
return 1;
return 0;
}
void cDeque::AddRear( int x )
{
if( !Full( ) )
items[ rear++ ] = x;
else
cout << "Queue Full ....... .Aborting!" << endl;
}
void cDeque::AddFront( int x )
Manual for Data Structure Page 30 of 138
{
if( !Full( ) )
{
for( int i = rear; i >= front; i-- ) // < >
items[ i ] = items[ i - 1 ];
items[ front ] = x;
rear++;
}
else
cout << "Queue Full ......... Aborting!" << endl;
}
int cDeque::DelFront( )
{
int x;
if( !Empty( ) )
{
x = items[ front ];
for( int i = front; i < rear; i++ ) // < >
items[ i ] = items[ i + 1 ];
rear--;
}
else
{
cout << "Queue Empty .......Aborting!" << endl;
x = -9999;
}
return x;
}
int cDeque::DelRear( )
{
int x;
if( !Empty( ) )
{
x = items[ rear - 1 ];
rear--;
}
else
{
cout << "Queue Empty ....... Aborting!" << endl;
x = -9999;
}
return x;
{
if( ( i + 1 ) % 2 )
Q1.AddRear( items[ i ] );
else
Q2.AddRear( items[ i ] );
}
}
void cDeque::Disp( )
{
for( int i = front; i < rear; i++ ) // < >
1
2
3
4
5
6
List 1
1
3
5
List 2
2
4
6
//Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cNode
{
int x;
cNode * next;
public:
void MakeNode( int );
void PutNext( cNode * );
cNode * GetNext( );
void DispNode( );
} ;
class cList
{
cNode * start,
* last;
int TotNodes;
public:
cList( );
void Add( );
void Add( int );
void DispList( );
void Reverse( );
} ;
void cNode::MakeNode( int a )
{
x = a;
next = '\0';
}
void cNode::PutNext( cNode * p )
{
next = p;
}
cNode * cNode::GetNext( )
{
return next;
}
void cNode::DispNode( )
{
cout << x << endl;
}
cList::cList( )
{
start = last = '\0';
TotNodes = 0;
}
void cList::Add( )
{
int a;
while( 1 )
{
cout << "Data (0 to exit) :";
cin >> a;
ptr = ptr->GetNext( ) )
ptr->DispNode( );
}
void main( )
{
cList L;
clrscr( );
L.Add( );
L.DispList( );
L.Reverse( );
L.DispList( );
getch( );
}
// INPUT :
/*Data ! 0 to exit):1
Data ! 0 to exit):2
Data ! 0 to exit):3
// OUTPUT :
1
2
3
4
5
6
7
8
9
10
reverse list
10
9
8
7
6
5
4
3
2
1*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cNode
{
int x;
cNode * next;
public:
void MakeNode( int );
void PutNext( cNode * );
cNode * GetNext( );
int GetData( )
{
return x;
}
void DispNode( );
} ;
class cList
{
cNode * start,
* last;
int TotNodes;
public:
cList( );
void Add( );
void Add( int );
void DispList( );
void Split( cList &, cList & );
} ;
void cNode::MakeNode( int a )
{
x = a;
next = '\0';
}
void cNode::PutNext( cNode * p )
{
next = p;
}
cNode * cNode::GetNext( )
{
return next;
}
void cNode::DispNode( )
{
cout << x << endl;
}
cList::cList( )
{
start = last = '\0';
TotNodes = 0;
}
void cList::Add( )
{
int a;
while( 1 )
ptr = ptr->GetNext( ) )
ptr->DispNode( );
}
void cList::Split( cList & L1, cList & L2 )
{
int cnt = 1;
cNode * ptr;
for( ptr = start; ptr;
ptr = ptr->GetNext( ), cnt++ )
{
if( cnt % 2 )
L1.Add( ptr->GetData( ) );
else
L2.Add( ptr->GetData( ) );
}
}
void main( )
{
cList L,
L1,
L2;
clrscr( );
L.Add( );
L.DispList( );
L.Split( L1, L2 );
cout << "First List is : " << endl;
L1.DispList( );
cout << "Second List is : " << endl;
L2.DispList( );
// INPUT :
Data ! 0 to exit):1
Data ! 0 to exit):2
Data ! 0 to exit):3
Data ! 0 to exit):4
Data ! 0 to exit):5
Data ! 0 to exit):6
Data ! 0 to exit):7
Data ! 0 to exit):8
Data ! 0 to exit):9
Data ! 0 to exit):10
Data ! 0 to exit):0
// OUTPUT :
1
2
3
4
5
6
7
8
9
10
First List is :
1
3
5
7
9
Second List is :
2
4
6
8
10
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cCList;
class cNode
{
char name[ 15 ];
int age;
float basic;
cNode * next;
public:
cNode( )
{
strcpy( name, '\0' );
age = 0;
basic = 0.0;
next = '\0';
}
cNode( char *, int, float );
void DispNode( );
friend class cCList;
} ;
class cCList
{
cNode * start,
* last;
public:
cCList( )
{
start = last = '\0';
}
void Add( );
void Add( char *, int, float );
void DispList( );
cCList Concate( cCList & );
} ;
cNode::cNode( char * n, int a, float b )
{
strcpy( name, n );
age = a;
basic = b;
next = '\0';
}
void cNode::DispNode( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( 15 ) << name;
cout.setf( ios::right, ios::adjustfield );
cout << setw( 3 ) << age << setw( 6 ) << basic << endl;
}
cCList cCList::Concate( cCList & CL2 )
{
cCList tmp;
int flag = 0;
for( cNode * ptr = last->next; // < >
// INPUT :
Name (Quit) . ENTER
Age 21
Basic 34
Name (Quit) . DATA
Age 76
Basic 65
Name (Quit) . SYSTEM
Age 34
Basic 89
Name (Quit) . Quit
Name (Quit) . xyz
Age 23
Basic 45
Name (Quit) . abc
Age 56
Basic 78
Name (Quit) . Quit
// OUTPUT :
FIRST LIST
ENTER 21 34
DATA 76 65
SYSTEM 34 89
SECOND LIST
xyz 23 45
abc 56 78
ENTER 21 34
DATA 76 65
SYSTEM 34 89
xyz 23 45
abc 56 78
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cCList;
class cNode
{
char name[ 15 ];
int age;
float basic;
cNode * next;
public:
cNode( )
{
strcpy( name, '\0' );
age = 0;
basic = 0.0;
next = '\0';
}
cNode( char *, int, float );
void DispNode( );
friend class cCList;
} ;
class cCList
{
cNode * start,
* last;
public:
cCList( )
{
start = last = '\0';
}
void Add( );
void Add( char *, int, float );
void DispList( );
void Split( cCList &, cCList & );
} ;
cNode::cNode( char * n, int a, float b )
{
strcpy( name, n );
age = a;
basic = b;
next = '\0';
}
void cNode::DispNode( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( 15 ) << name;
cout.setf( ios::right, ios::adjustfield );
cout << setw( 3 ) << age << setw( 6 ) << basic << endl;
}
void cCList::Split( cCList & CL1, cCList & CL2 )
{
int cnt = 1;
int flag = 0;
for( cNode * ptr = last->next; // < >
void cCList::DispList( )
{
int flag = 0;
for( cNode * ptr = last->next; // < >
// INPUT :
Name (Quit) . GHJ
Age 4
Basic 5
Name (Quit) . ABC
Age 3
Basic 4
Name (Quit) . XYZ
Age 3
Basic 6
Name (Quit) . SYSTEM
Age 5
Basic 4
Name (Quit) . ENTER
Age 5
Basic 8
Name (Quit) . Quit
//OUTPUT :
GHJ 4 5
ABC 3 4
XYZ 3 6
SYSTEM 5 4
ENTER 5 8
GHJ 4 5
ABC 3 4
XYZ 3 6
SYSTEM 5 4
ENTER 5 8
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
class cDList;
class cNode
{
char name[ 15 ];
int age;
float basic;
cNode * next,
* prev;
public:
cNode( )
{
strcpy( name, '\0' );
age = 0;
basic = 0.0;
next = prev = '\0';
}
cNode( char *, int, float );
void DispNode( );
friend class cDList;
} ;
class cDList
{
cNode * start,
* last;
public:
cDList( )
{
start = last = '\0';
}
void AddAfter( char *, int, float, char * );
void AddBefore( char *, int, float, char * );
void DispList( );
void Delete( char * );
void Search( char * );
void Add( );
} ;
cNode::cNode( char * n, int a, float b )
{
strcpy( name, n );
age = a;
basic = b;
next = prev = '\0';
}
void cNode::DispNode( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( 15 ) << name;
cout.setf( ios::right, ios::adjustfield );
cout << setw( 3 ) << age << setw( 6 ) << setw( 7 ) << basic << endl;
}
void cDList::AddAfter( char * n, int a, float b,
char * s )
ptr->DispNode( );
}
void main( )
{
cDList D;
clrscr( );
D.Add( );
clrscr( );
D.DispList( );
// D.AddAfter("Ramesh",23,5000,"Mickey");
D.AddBefore("Diwakar",25,9000,"Ramesh");
/*
char n[15];
cout<<"Name to be Searched:";
cin>>n;
// D.Search(n);
D.Delete (n);
cout<<endl<<endl; */
D.DispList( );
getch( );
}
//INPUT :
Name (Quit) :GHJ
Age 5
Basic 6
Name (Quit) :YOO
Age 6
Basic 7
Name (Quit) :GHGJ
Age 89
Basic 6
Name (Quit) :TJ
Age 8
Basic 9
Name (Quit) :SF
Age 7
Basic 5
Name (Quit) :Quit
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
void main( )
{
int IsMax( int, int, int * );
int n,
* arr;
clrscr( );
cout << "Enter Number of Elements:";
cin >> n;
arr = new int [n];
cout << "Enter Elements : " << endl;
for( int i = 0; i < n; i++ ) // < >
// INPUT :
Enter Number of Elements:5
Enter Elements :
1
2
3
4
5
// OUTPUT :
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 10
template<class xType>class cStack
{
xType data[ MAX ];
int top;
int Empty( );
int Full( );
public:
cStack( )
{
top = 0;
}
void Push( xType & );
xType & Pop( );
void Disp( );
void Rev( cStack< xType > & );
} ;
template<class xType> void cStack< xType >::Rev(
cStack< xType > & S1 )
{
while( !Empty( ) )
S1.Push( Pop( ) );
}
template<class xType> int cStack< xType >::Empty( )
{
if( top < 0 )
return 1;
return 0;
}
template<class xType> int cStack< xType >::Full( )
{
if( top > MAX )
return 1;
return 0;
}
template<class xType> void cStack< xType >::Disp( )
{
for( int i = top; i > 0; i-- ) // < >
// INPUT :
GIVEN STACK
g n a n a
// OUTPUT :
REVERSE THE CONTENTS OF THE STACK
a n a n g
// example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define INT_MAX 999
#define MAX 10
class cStack
{
int data[ MAX ];
int top;
int Empty( );
int Full( );
public:
cStack( )
{
top = 0;
}
void Push( );
void Push( int );
void DispStack( );
int Bottom( );
} ;
int cStack::Empty( )
{
if( top < 0 )
return 1;
return 0;
}
int cStack::Full( )
{
if( top > MAX )
return 1;
return 0;
}
int cStack::Bottom( )
{
if( !Empty( ) )
return data[ 1 ];
return INT_MAX;
}
void cStack::Push( )
{
int x;
if( !Full( ) )
{
while( 1 )
{
cout << "Data : ";
cin >> x;
if( x == 0 )
break;
else
data[ ++top ] = x;
}
}
else
cout << "stack overflow";
}
// INPUT :
Data:
1
Data:
2
Data:
3
Data:
4
Data:
5
Data:
0
// OUTPUT :
stack is
5
4
3
2
1
Last ele = 1
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define ERR
#define MAX 10
class cStack
{
char data[ MAX ];
int top;
int Empty( );
int Full( );
public:
cStack( )
{
top = 0;
}
void Push( char );
int Pop( );
void DispStack( );
int IsPal( char * );
} ;
int cStack::IsPal( char * S )
{
int Flag = 1;
for( int i = 0; i < ( strlen( S ) ) / 2; i++ ) // < >
Push( S[ i ] );
for( ++i; S[ i ]; i++ )
{
if( S[ i ] != Pop( ) )
{
Flag = 0;
break;
}
}
return Flag;
}
int cStack::Empty( )
{
if( top < 0 )
return 1;
return 0;
}
int cStack::Full( )
{
if( top > MAX )
return 1;
return 0;
}
void cStack::Push( char x )
{
if( !Full( ) )
data[ ++top ] = x;
else
cout << "Stack Overflow";
}
int cStack::Pop( )
String:
POP
Pallindrome
String:
PUSH
Not
Pallindrome
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#define MAX 10
class cQueue
{
int items[ MAX ];
int front,
rear,
count;
int Full( );
int Empty( );
public:
cQueue( )
{
rear = front = count = 0;
}
void Add( );
void Add( int );
void Disp( );
int Del( );
int Peek( );
int operator ==( const cQueue & );
} ;
int cQueue::operator ==( const cQueue & Q )
{
int flag = 1;
if( count != Q.count )
flag = 0;
else
{
while( count > 0 )
{
if( Del( ) != Q.Del( ) )
{
flag = 0;
break;
}
}
}
return flag;
}
int cQueue::Full( )
{
if( count >= MAX )
return 1;
return 0;
}
int cQueue::Empty( )
{
if( count == 0 )
return 1;
return 0;
}
void cQueue::Add( )
{
int x;
if( !Full( ) )
items[ j ] = items[ j + 1 ];
count--;
}
else
cout << "Queue is Empty ..... Delete Aborted!" << endl;
return x;
}
int cQueue::Peek( )
{
if( !Empty( ) )
return items[ front ];
else
cout << "Queue is Empty ..... Delete Aborted!" << endl;
return -999;
}
void cQueue::Disp( )
{
for( int i = front; i < count; i++ ) // < >
// INPUT :
Enter elements of I Queue
Data (0-Exit) :1
Data (0-Exit) :2
Data (0-Exit) :3
Data (0-Exit) :4
Data (0-Exit) :5
Data (0-Exit) :0
Enter elements of II Queue
Data (0-Exit) :1
Data (0-Exit) :2
Data (0-Exit) :3
Data (0-Exit) :4
Data (0-Exit) :5
Data (0-Exit) :0
// OUTPUT :
1
2
3
4
5
1
2
3
4
5
Equal
// INPUT :
Enter
elements of I Queue
Data (0-Exit) :1
Data (0-Exit) :3
Data (0-Exit) :4
Data (0-Exit) :3
Data (0-Exit) :0
Enter elements of II Queue
Data (0-Exit) :9
Data (0-Exit) :5
Data (0-Exit) :4
Data (0-Exit) :4
Data (0-Exit) :0
// OUTPUT :
1
3
4
3
9
5
4
4
Different
// Example Code :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
class cInteger
{
int num;
public:
cInteger( )
{
num = 0;
}
cInteger( int a )
{
num = a;
}
friend ostream & operator <<( ostream &, cInteger & );
friend istream & operator >>( istream &, cInteger & );
friend cInteger operator *( const cInteger &, int );
} ;
ostream & operator <<( ostream & out, cInteger & x )
{
out << setw( 5 ) << x.num << endl;
return out;
}
istream & operator >>( istream & in, cInteger & x )
{
in >> x.num;
return in;
}
cInteger operator *( const cInteger & I, int x )
{
cInteger tmp;
tmp.num = I.num * x;
return tmp;
}
void main( )
{
cInteger I1,I2,I3,I4;
clrscr( );
cin >> I1 >> I2 >> I3;
cout << I1 << I2 << I3;
I4 = I1 * 3;
cout << I4;
getch( );
}
// INPUT :
2
3
4
// OUTPUT :
2
3
4
I1*3=
6
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
enum
{
LEFT,
RIGHT
} ;
class cTree;
class cNode
{
int data;
cNode * lptr,
* rptr;
public:
cNode( )
{
data = 0;
lptr = rptr = '\0';
}
cNode( int x )
{
data = x;
lptr = rptr = '\0';
}
friend class cTree;
} ;
class cTree
{
cNode * Root;
public:
cTree( )
{
Root = '\0';
}
void sortArray( int *, int );
void Insert( int );
int inOrder( cNode *, int *, int );
void Insert( );
void InOrder( cNode * );
void PostOrder( cNode * );
void PreOrder( cNode * );
int IsEmpty( )const;
cNode * GetRoot( )
{
return Root;
}
} ;
void cTree::Insert( int x )
{
int dir;
cNode * ptr,
* prev;
cNode * NewNode = new cNode( x );
if( !Root )
Insert( arr[ i ] );
inOrder( Root, arr, 0 );
}
int cTree::IsEmpty( )const
{
if( !Root )
return 1;
return 0;
}
void cTree::Insert( )
{
int x,
dir;
cNode * ptr,
* prev;
while( 1 )
{
cout << "Data : ";
cin >> x;
if( x == 0 )
break;
else
{
cNode * NewNode = new cNode( x );
if( !Root )
Root = NewNode;
else
{
{
cout << "Enter Value for " << setw( 2 ) << i + 1 << ":";
cin >> arr[ i ];
}
T.sortArray( arr, 10 );
for( i = 0; i < 10; i++ )
cout << endl << arr[ i ];
// INPUT :
Enter Value
for 1:1
Enter Value
for 2:2
Enter Value
for 3: 5
Enter Value
for 4:3
Enter Value
for 5:6
Enter Value
for 6:4
Enter Value
for 7:8
Enter Value
for 8:6
Enter Value
for 9:54
Enter Value
for 10:9
// OUTPUT :
1
2
3
4
5
6
6
8
9
54
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
enum
{
LEFT,
RIGHT
} ;
class cTree;
class cNode
{
int data;
cNode * lptr,
* rptr;
public:
cNode( )
{
data = 0;
lptr = rptr = '\0';
}
cNode( int x )
{
data = x;
lptr = rptr = '\0';
}
friend class cTree;
} ;
class cTree
{
cNode * Root;
public:
cTree( )
{
Root = '\0';
}
void Insert( );
void InOrder( cNode * );
void PostOrder( cNode * );
void PreOrder( cNode * );
int IsEmpty( )const;
cNode * GetRoot( )
{
return Root;
}
} ;
int cTree::IsEmpty( )const
{
if( !Root )
return 1;
return 0;
}
void cTree::Insert( )
{
int x,
dir;
cNode * ptr,
{
dir = LEFT;
ptr = ptr->lptr;
}
}
if( dir == LEFT )
prev->lptr = NewNode;
else
prev->rptr = NewNode;
}
}
}
}
void cTree::InOrder( cNode * Curr )
{
if( Curr )
{
InOrder( Curr->lptr );
// INPUT :
Data :
1
Data:
2
Data:
3
Data:
4
Data:
5
Data:
0
// OUTPUT :
5
4
3
2
1
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
enum
{
LEFT,
RIGHT
} ;
class cTree;
class cNode
{
int data;
cNode * lptr,
* rptr;
public:
cNode( )
{
data = 0;
lptr = rptr = '\0';
}
cNode( int x )
{
data = x;
lptr = rptr = '\0';
}
friend class cTree;
} ;
class cTree
{
cNode * Root;
int getSize( cNode *, int );
public:
cTree( )
{
Root = '\0';
}
void Insert( );
void InOrder( cNode * );
void PostOrder( cNode * );
void PreOrder( cNode * );
int IsEmpty( )const;
int Min( )const;
int Max( )const;
int size( )const;
cNode * GetRoot( )
{
return Root;
}
} ;
int cTree::Min( )const
{
cNode * ptr,
* prev;
for( ptr = Root; ptr;
prev = ptr, ptr = ptr->lptr )
;
return prev->data;
}
cNode * ptr,
* prev;
for( ptr = Root; ptr;
prev = ptr, ptr = ptr->rptr )
;
return prev->data;
}
int cTree::getSize( cNode * R, int ctr )
{
if( !R )
return ctr;
ctr = getSize( R->lptr, ctr );
ctr++;
getSize( R->rptr, ctr );
return ctr;
}
int cTree::size( )const
{
int cnt = 0;
cnt = getSize( Root, 0 );
return cnt;
}
int cTree::IsEmpty( )const
{
if( !Root )
return 1;
return 0;
}
void cTree::Insert( )
{
int x,
dir;
cNode * ptr,
* prev;
while( 1 )
{
cout << "Data : ";
cin >> x;
if( x == 0 )
break;
else
{
cNode * NewNode = new cNode( x );
if( !Root )
Root = NewNode;
else
{
for( ptr = Root; ptr; )
{
prev = ptr;
if( ( ptr->data )
< ( NewNode->data ) )
{
dir = RIGHT;
ptr = ptr->rptr;
}
else
{
dir = LEFT;
ptr = ptr->lptr;
}
// INPUT :
Data :
1
Data:
2
Data:
3
Data:
4
Data:
5
Data:
6
Data:
0
// OUTPUT :
Max =
1
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
enum
{
LEFT,RIGHT
} ;
class cTree;
class cNode
{
int data;
cNode * lptr,
* rptr;
public:
cNode( )
{
data = 0;
lptr = rptr = '\0';
}
cNode( int x )
{
data = x;
lptr = rptr = '\0';
}
friend class cTree;
} ;
class cTree
{
cNode * Root;
public:
cTree( )
{
Root = '\0';
}
int Expand( int );
int getNodesAtLeveln( cNode * R, int cnt,
int curLevei, int level );
void Insert( );
void PreOrder( cNode * );
int IsEmpty( )const;
cNode * GetRoot( )
{
return Root;
}
int Level( int );
} ;
int cTree::Expand( int level )
{
int n = getNodesAtLeveln( Root, 0, 1, level );
return n;
}
int cTree::getNodesAtLeveln( cNode * R, int cnt,
int curLevel, int level )
{
if( !R )
return cnt;
if( curLevel == level )
cnt++;
// INPUT :
Data :
1
Data:
2
Data:
3
Data:
4
Data:
5
Data:
6
Data:
0
Enter the Level to Expand :1
// OUTPUT :
No of Nodes at Level 1 is 6
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
enum
{
LEFT,
RIGHT
} ;
class cTree;
class cNode
{
int data;
cNode * lptr,
* rptr;
public:
cNode( )
{
data = 0;
lptr = rptr = '\0';
}
cNode( int x )
{
data = x;
lptr = rptr = '\0';
}
friend class cTree;
} ;
class cTree
{
cNode * Root;
cNode * Defoliate( cNode * );
public:
cTree( )
{
Root = '\0';
}
void Insert( );
void Defoliate( )const;
void InOrder( cNode * );
cNode * GetRoot( )
{
return Root;
}
} ;
void cTree::Insert( )
{
int x,
dir;
cNode * ptr,
* prev;
while( 1 )
{
cout << "Data : ";
cin >> x;
if( x == 0 )
break;
else
{
// INPUT :
Data :
1
Data:
3
Data:
5
Data:
7
Data:
9
Data:
0
// OUTPUT :
1
3
5
7
9
1
3
5
7
// Example Code :
#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 3
class cItem
{
char itcode[ 6 ];
char itname[ 20 ];
int qty;
public:
void ReadData( );
void WriteData( );
friend void Sort( cItem *, int );
} ;
void cItem::ReadData( )
{
cout << "ITEM CODE ";
cin >> itcode;
cout << "ITEM NAME ";
cin >> itname;
cout << "QUANTITY ";
cin >> qty;
}
void cItem::WriteData( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( 8 ) << itcode << setw( 20 ) << itname;
cout.setf( ios::right, ios::adjustfield );
cout << setw( 5 ) << qty << endl;
}
void Sort( cItem * T, int n )
{
for( int i = 0; i < n - 1; i++ ) // < >
{
for( int j = 0; j < n - 1 - i; j++ ) // < >
{
if( strcmp( T[ j ].itcode,
T[ j + 1 ].itcode )
> 0 )
{
cItem tmp = T[ j ];
T[ j ] = T[ j + 1 ];
T[ j + 1 ] = tmp;
}
}
}
iitm[ i ].ReadData( );
Sort( iitm, MAX );
file.open( "item.dat", ios::trunc | ios::in | ios::out | ios::binary );
file.seekg( 0, ios::beg );
file.clear( );
for( i = 0; i < MAX; i++ )
file.write( ( char * )&iitm[ i ], sizeof( iitm ) );
file.seekg( 0, ios::beg );
for( i = 0; i < MAX; i++ )
{
file.read( ( char * )&iitm[ i ], sizeof( iitm ) );
iitm[ i ].WriteData( );
}
getch( );
}
/*struct cItem
{
char itcode [6];
char itname [20];
int qty;
};
struct cItem* ReadData(int n)
{
struct cItem itm[MAX];
for(int i=0;i<n;i++)
{
cout<<" ITEM CODE ";
cin>>itm[i].itcode;
cout<<"ITEM NAME ";
cin>>itm[i].itname;
cout<<"QUANTITY ";
cin>>itm[i].qty;
}
return itm;
}
void WriteData(struct cItem*itm,int n)
{
for(int i=0;i<n;i++)
{
cout.setf(ios::left,ios::adjustfield);
cout<<setw(8)<<itm[i].itcode<<setw(20)<<itm[i].itname;
cout.setf(ios::right,ios::adjustfield);
cout<<setw(5)<<itm[i].qty<<endl;
}
}
void Sort(cItem *T,int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if (strcmp (T[j].itcode, T[j+1].itcode)>0)
{
cItem tmp=T [j];
T[j]=T[j+1];
// Input :
ITEM CODE 3
ITEM NAME c
QUANTITY 9
ITEM CODE 2
ITEM NAME b
QUANTITY 8
ITEM CODE 1
ITEM NAME a
QUANTITY 7
// output :
1 a 7
2 b 8
3 c 9
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <stdio.h>
template<class xType>class cCQueue;
template<class xType>class cNode
{
friend cCQueue< xType >;
xType item;
cNode *next;
public:
cNode( )
{
next = '\0';
}
cNode( xType x )
{
item = x;
next = '\0';
}
void DispNode( );
} ;
template<class xType>class cCQueue
{
cNode< xType > * front,
* rear;
public:
cCQueue( )
{
rear = front = '\0';
}
void Add( );
void Disp( );
void Delete( );
} ;
template<class xType> void cCQueue< xType >::Add( )
{
xType x;
char ch = 'y';
while( ch == 'y' || ch == 'Y' )
{
cout << "Item . ";
cin >> x;
cNode < xType > * NewNode = new cNode < xType >( x );
if( !front )
{
front = rear = NewNode;
NewNode->next = front;
}
else
{
NewNode->next = rear->next;
rear->next = NewNode;
rear = NewNode;
}
cout << "Want to Add More '[y/n] : ";
ch = getchar( );
fflush( stdin );
}
// INPUT :
Item . 1
Want to Add More '[y/n] : Y
Item . 2
Want to Add More '[y/n] : Y
Item . 3
Want to Add More '[y/n] : Y
Item . 4
Want to Add More '[y/n] : N
// OUTPUT :
1
2
3
4
1
// Example Code :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
class cArray
{
int num[ 5 ];
public:
cArray( );
friend ostream & operator <<( ostream &, cArray & );
friend istream & operator >>( istream &,cArray & );
void sort( );
} ;
cArray::cArray( )
{
for( int i = 0; i < 5; i++ ) // < >
num[ i ] = 0;
}
ostream & operator <<( ostream & out, cArray & arr )
{
out << "Elements of the Array are : " << endl;
for( int i = 0; i < 5; i++ ) // < >
{
out << arr.num[ i ] << endl;
}
}
istream & operator >>( istream & in, cArray & arr )
{
cout << "Enter Elements of the Array : " << endl;
for( int i = 0; i < 5; i++ ) // < >
{
in >> arr.num[ i ];
}
}
void cArray::sort( )
{
for( int i = 1; i < 5; i++ ) // < >
{
for( int j = 0; j < 5 - i; j++ ) // < >
{
if( num[ j ] > num[ j + 1 ] )
{
int tmp = num[ j ];
num[ j ] = num[ j + 1 ];
num[ j + 1 ] = tmp;
}
}
}
}
void main( )
{
cArray arr;
INPUT
Enter
Elements of the Array :
2
3
4
5
0
OUTPUT
Elements
of the Array are :
0
2
3
4
5
// Example Code :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <stdio.h>
class cString
{
char * str;
int len;
public:
cString( )
{
str = '\0';
len = 0;
}
cString( char * );
void Encript( );
void Decript( );
void GetStr( );
void DispStr( );
} ;
cString::cString( char * S )
{
for( int i = 0; S[ i ]; i++ ) // < >
;
len = i;
str = new char [len + 1];
int j = 0;
for( i = 0; S[ i ]; i++ )
str[ j++ ] = S[ i ];
str[ j ] = '\0';
}
void cString::GetStr( )
{
cout << "Length of String :";
cin >> len;
str = new char [len + 1];
gets( str );
fflush( stdin );
}
void cString::DispStr( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( len ) << str << endl;
}
void cString::Encript( )
{
for( int i = 0; i < len; i++ ) // < >
str[ i ] += ( i + 100 - ( i * 2 ) );
}
void cString::Decript( )
{
for( int i = 0; i < len; i++ ) // < >
{
str[ i ] -= ( i + 100 - ( i * 2 ) );
// INPUT :
Length
of String :4
DSDA
// OUTPUT :
Employee No Numeric
Name String
Department String
Option should be made to view the list generated. Modularize the code to
perform the above actions.
// Example Code :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <stdio.h>
#include <string.h>
class cNode
{
int eno;
char name[ 15 ];
char dept[ 10 ];
cNode * next;
public:
cNode( );
cNode( int, char *, char * );
void MakeNode( int, char *, char * );
void PutNext( cNode * );
int GetEno( )
{
return eno;
}
char * GetName( )
{
return name;
}
char * GetDept( )
{
return dept;
}
cNode * GetNext( );
void DispNode( );
} ;
class cList
{
cNode * start,
* last;
int TotNodes;
public:
cList( )
{
start = last = '\0';
TotNodes = 0;
}
void Add( );
void Add( int, char *, char * );
void Modify( );
void Modify( int );
void Delete( );
void Delete( int );
void DispList( );
} ;
cNode::cNode( )
1 Mickey Computer
2 Ajay Computer
3 Sushil Computer
4 Donald Computer
Employee Number to be Modified : 1
Enter name ä shilpa
Enter Department CSE
1 shilpa CSE
2 Ajay Computer
3 Sushil Computer
4 Donald Computer
Employee Number to be Deleted :3
3 Sushil Computer
Confirm Deletion (y/n) ?y
1 shilpa CSE
2 Ajay Computer
4 Donald Computer
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
class cWrtln
{
char str[ 80 ];
public:
cWrtln( )
{
strcpy( str, '\0' );
}
void Read( );
void Write( );
} ;
void cWrtln::Read( )
{
cout << "Line" << endl;
gets( str );
fflush( stdin );
}
void cWrtln::Write( )
{
cout << str << endl;
}
void main( )
{
clrscr( );
cWrtln S;
int cnt = 0;
char ans = 'y';
fstream F;
F.open( "data.dat", ios::in | ios::out | ios::app | ios::binary );
while( ans == 'y' || ans == 'Y' )
{
S.Read( );
F.write( ( char * )&S, sizeof( S ) );
F.seekg( ( cnt * 80 ) + 1, ios::beg );
cnt++;
cout << "Want to Continue (y/n):";
ans = getchar( );
fflush( stdin );
}
F.seekg( 0 );
for( int i = 0; i < cnt; i++ ) // < >
{
F.read( ( char * )&S, sizeof( S ) );
S.Write( );
}
F.close( );
getch( );
}
Line
FDS
Want
to Continue (y/n):Y
Line
VFBFB
Want to Continue (y/n):N
// OUTPUT :
FDS
VFBFB
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cMatrix
{
int ** mat;
int r,
c;
void CreateMat( );
void InitMat( );
public:
cMatrix( )
{
mat = '\0';
r = c = 0;
}
friend istream & operator >>( istream &,
cMatrix & );
friend ostream & operator <<( ostream &,
cMatrix & );
cMatrix Add( cMatrix & );
cMatrix operator +( cMatrix & );
cMatrix operator +( int );
cMatrix Sub( cMatrix & );
cMatrix operator -( cMatrix & );
cMatrix operator -( int );
void DispMat( );
} ;
void cMatrix::CreateMat( )
{
mat = new int*[r];
for( int i = 0; i < r; i++ ) // < >
{
cout << "Row #" << setw( 2 ) << i + 1 << endl;
for( int j = 0; j < c; j++ ) // < >
{
cout << "Col #" << setw( 2 ) << j + 1 << ":";
cin >> mat[ i ][ j ];
}
}
}
void cMatrix::DispMat( )
{
cout.setf( ios::left, ios::adjustfield );
for( int i = 0; i < r; i++ ) // < >
{
for( int j = 0; j < c; j++ ) // <
>
tmp.mat[ i ][ j ] = mat[ i ][ j ] + M2.mat[ i ][ j ];
}
}
return tmp;
}
cMatrix cMatrix::operator +( cMatrix & M2 )
{
cMatrix tmp;
tmp.r = r;
tmp.c = c;
if( r == M2.r && c == M2.c )
{
tmp.CreateMat( );
for( int i = 0; i < r; i++ ) // < >
{
for( int j = 0; j < c; j++ ) // <
>
tmp.mat[ i ][ j ] = mat[ i ][ j ] + M2.mat[ i ][ j ];
}
}
return tmp;
}
cMatrix cMatrix::Sub( cMatrix & M2 )
{
cMatrix tmp;
tmp.r = r;
tmp.c = c;
if( r == M2.r && c == M2.c )
{
tmp.CreateMat( );
for( int i = 0; i < r; i++ ) // < >
{
for( int j = 0; j < c; j++ ) // <
>
tmp.mat[ i ][ j ] = mat[ i ][ j ] - M2.mat[ i ][ j ];
}
}
return tmp;
}
cMatrix cMatrix::operator -( cMatrix & M2 )
{
cMatrix tmp;
tmp.r = r;
{
for( int j = 0; j < c; j++ ) // <
>
tmp.mat[ i ][ j ] = mat[ i ][ j ] - M2.mat[ i ][ j ];
}
}
return tmp;
}
cMatrix cMatrix::operator +( int x )
{
cMatrix tmp;
tmp.r = r;
tmp.c = c;
tmp.CreateMat( );
for( int i = 0; i < r; i++ ) // < >
{
for( int j = 0; j < c; j++ ) // < >
tmp.mat[ i ][ j ] = mat[ i ][ j ] + x;
}
return tmp;
}
cMatrix cMatrix::operator -( int x )
{
cMatrix tmp;
tmp.r = r;
tmp.c = c;
tmp.CreateMat( );
for( int i = 0; i < r; i++ ) // < >
{
for( int j = 0; j < c; j++ ) // < >
tmp.mat[ i ][ j ] = mat[ i ][ j ] - x;
}
return tmp;
}
istream & operator >>( istream & In, cMatrix & M )
{
cout << "Number of Rows .";
cin >> M.r;
cout << "Number of Cols .";
cin >> M.c;
M.CreateMat( );
M.InitMat( );
return In;
}
ostream & operator <<( ostream & Out, cMatrix & M1 )
{
for( int i = 0; i < M1.r; i++ ) // < >
{
for( int j = 0; j < M1.c; j++ ) // < >
{
cout << M1.mat[ i ][ j ] << "\t";
}
Number of Rows .3
Number of Cols .3
Row # 1
Col # 1:1
Col # 2:1
Col # 3:1
Row # 2
Col # 1:1
Col # 2:1
Col # 3:1
Row # 3
Col # 1:1
Col # 2:1
Col # 3:1
Number of Rows .3
Number of Cols .3
Row # 1
Col # 1:1
Col # 2:1
Col # 3:1
Row # 2
Col # 1:1
Col # 2:1
Col # 3:1
Row # 3
Col # 1:1
Col # 2:1
Col # 3:1
MATRIX 1
1 1 1
1 1 1
1 1 1
MATRIX 2
1 1 1
1 1 1
1 1 1
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include<conio.h> #include<string.h>
class cMatrix
{
int mat[ 3 ][ 3 ];
void InitMat( );
public:
cMatrix( )
{
} ;
friend istream & operator >>( istream &,
cMatrix & );
friend ostream & operator <<( ostream &,
cMatrix & );
int Largest( );
int Smallest( );
void DispMat( );
} ;
int cMatrix::Largest( )
{
int max = mat[ 0 ][ 0 ];
for( int i = 0; i < 3; i++ ) // < >
{
for( int j = 0; j < 3; j++ ) // < >
{
if( mat[ i ][ j ] >= max )
max = mat[ i ][ j ];
}
}
return max;
}
int cMatrix::Smallest( )
{
int min = mat[ 0 ][ 0 ];
for( int i = 0; i < 3; i++ ) // < >
{
for( int j = 0; j < 3; j++ ) // < >
{
if( mat[ i ][ j ] <= min )
min = mat[ i ][ j ];
}
}
return min;
}
void cMatrix::InitMat( )
{
for( int i = 0; i < 3; i++ ) // < >
{
cout << "Row #" << setw( 2 ) << i + 1 << endl;
{
cout << "Col #" << setw( 2 ) << j + 1 << ":";
cin >> mat[ i ][ j ];
}
}
}
void cMatrix::DispMat( )
{
cout.setf( ios::left, ios::adjustfield );
for( int i = 0; i < 3; i++ ) // < >
{
for( int j = 0; j < 3; j++ ) // < >
{
for( int j = 0; j < 3; j++ ) // < >
{
cout << M1.mat[ i ][ j ] << "\t";
}
cout << endl;
}
return Out;
}
void main( )
{
cMatrix M1;
//clrscr ();
cin >> M1;
clrscr( );
cout << M1;
int m = M1.Largest( );
cout << endl << "Maximum is . " << m;
int m1 = M1.Smallest( );
cout << endl << "Smallest is . " << m1;
getch( );
}
// INPUT :
Row # 1
Col # 1:1
Col # 2:2
Col # 3:3
Row # 2
Col # 1:4
Col # 2:5
Col # 3:6
// OUTPUT :
1 2 3
4 5 6
3 8 3
Maximum is . 8
Smallest is . 1
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
template<class cType> void Sort( cType * arr, int n )
{
cType tmp;
for( int i = 0; i < n - 1; i++ ) // < >
{
for( int j = 0; j < n - 1 - i; j++ ) // < >
{
if( arr[ j ] > arr[ j + 1 ] )
{
tmp = arr[ j ];
arr[ j ] = arr[ j + 1 ];
arr[ j + 1 ] = tmp;
}
}
}
}
void main( )
{
char * arr;
int n;
clrscr( );
cout << "enter no. of elements : ";
cin >> n;
arr = new char [n];
cout << "Enter Elements now \n";
for( int i = 0; i < n; i++ ) // < >
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cNumber
{
long num;
char con[ 30 ];
public:
cNumber( )
{
num = 0;
}
void GetNum( );
void DispNum( );
void Hex( );
void HeX( );
void Oct( );
void Bin( );
} ;
void cNumber::GetNum( )
{
cout << "Enter any decimal :";
cin >> num;
}
void cNumber::DispNum( )
{
cout << num << " in converted form is " << con << endl;
}
void cNumber::HeX( )
{
int i = 0;
long num1;
num1 = num;
while( num1 )
{
if( num1 % 16 <= 9 )
con[ i++ ] = num1 % 16 + 48;
else
switch( num1 % 16 )
{
case 10:
con[ i++ ] = 'A';
break;
case 11:
con[ i++ ] = 'B';
break;
case 12:
con[ i++ ] = 'C';
break;
case 13:
con[ i++ ] - 'D';
break;
case 14:
// INPUT :
Enter
any decimal :24
// OUTPUT :
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cTime
{
int hh,
mm,
ss;
public:
cTime( )
{
hh = ss = mm = 0;
}
cTime( int a, int b, int c )
{
hh = a;
mm = b;
ss = c;
}
int IsValid( );
friend istream & operator >>( istream &,
cTime & );
friend ostream & operator <<( ostream &,
cTime & );
cTime operator +( cTime & );
cTime operator -( cTime & );
} ;
istream & operator >>( istream & In, cTime & T )
{
cout << "Hours :";
In >> T.hh;
cout << "Minutes :";
In >> T.mm;
cout << "Seconds :";
In >> T.ss;
return In;
}
ostream & operator <<( ostream & Out, cTime & T )
{
cout.fill( '0' );
Out << setw( 2 ) << T.hh << ": " << setw( 2 ) << T.mm << ":" << setw( 2
)
<< T.ss << endl;
return Out;
}
cTime cTime::operator +( cTime & T2 )
{
cTime tmp;
tmp.hh = hh + T2.hh;
tmp.mm = mm + T2.mm;
if( tmp.mm > 59 )
{
tmp.hh++;
tmp.mm -= 60;
}
// INPUT :
Hours :
2
// OUTPUT :
time
1 - 02: 34:11
time 2 - 03: 20:33
Addition - 05: 54:44
Subtraction - 00: 00:00
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cFraction
{
int num,
den;
int LCM( int, int );
public:
cFraction( )
{
num = den = 1;
}
cFraction( int a, int b = 1 )
{
num = a;
den = b;
}
friend istream & operator >>( istream &,
cFraction & );
friend ostream & operator <<( ostream &,
cFraction & );
cFraction operator +( cFraction & );
cFraction operator -( cFraction & );
cFraction operator *( cFraction & );
cFraction operator /( cFraction & );
cFraction Simplify( );
int IsValid( );
} ;
cFraction cFraction::operator *( cFraction & F )
{
cFraction tmp;
tmp.num = num * F.num;
tmp.den = den * F.den;
return tmp.Simplify( );
}
cFraction cFraction::operator /( cFraction & F )
{
cFraction tmp;
tmp.num = num * F.den;
tmp.den = den * F.num;
return tmp.Simplify( );
}
cFraction cFraction::Simplify( )
{
cFraction tmp;
int j = 0,
tt,
k = 0,
t2;
if( num > den )
{
tt = num;
t2 = den;
}
{
if( tt % i == 0 )
{
if( t2 % i == 0 )
{
tt /= i;
t2 /= i;
}
}
}
if( num > den )
{
tmp.num = tt;
tmp.den = t2;
}
else
{
tmp.den = tt;
tmp.num = t2;
}
return tmp;
}
// output :
fraction
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <stdio.h>
#include <conio.h>
class cDate
{
int dd,
mm,
yy;
public:
cDate( )
{
dd = mm = yy = 0;
}
cDate( int a, int b, int c )
{
dd = a;
mm = b;
yy = c;
}
friend istream & operator >>( istream &, cDate & );
friend ostream & operator <<( ostream &, cDate & );
int operator -( cDate & );
void operator +( int );
} ;
istream & operator >>( istream & In, cDate & D )
{
In >> D.dd >> D.mm >> D.yy;
return In;
}
ostream & operator <<( ostream & Out, cDate & D )
{
Out << D.dd << "/" << D.mm << "/" << D.yy << endl;
return Out;
}
int cDate::operator -( cDate & D2 )
{
int doyTable[]={0,0,31,59,90,120,151,182,212,243,273,304,334,365};
int days1 = 0,
days2 = 0;
days1 += doyTable[ mm ] + dd;
days1 = days1 + ( yy - 1 ) / 4;
days1 = days1 - ( yy - 1 ) / 100;
days1 = days1 + ( yy - 1 ) / 400;
days1 += ( yy - 1 ) * 365;
// INPUT :
Enter
I Date (dd-mm-yy) .24
07
86
Enter II Date (dd-mm-yy) .12
// OUTPUT :
24/7/86
12/12/85
Diff = 225No. of Days to be added :45
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <stdio.h>
class String_Matcher
{
char * str;
int len;
public:
String_Matcher( )
{
str = '\0';
len = 0;
}
String_Matcher( char * S )
{
int j = 0;
for( int i = 0; S[ i ]; i++ ) // < >
;
len = i;
str = new char [len + 1];
for( i = 0; S[ i ]; i++ )
;
str[ j++ ] = S[ i ];
str[ j ] = '\0';
}
int match( const char * );
} ;
int String_Matcher::match( const char * string )
{
for( int i = 0; // < >
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
class cNumber
{
protected:
int num;
public:
cNumber( )
{
num = 0;
}
cNumber( int x )
{
num = x;
}
virtual void Print_it( )
{
cout << setw( 5 ) << num << endl;
}
} ;
class cHex:public cNumber
{
public:
cHex( ):cNumber( )
{
} ;
cHex( int a ):cNumber( a )
{
} ;
void Print_it( )
{
cout.setf( ios::hex, ios::basefield );
cout << setw( 5 ) << num << endl;
}
} ;
class cOct:public cNumber
{
public:
cOct( ):cNumber( )
{
} ;
cOct( int a ):cNumber( a )
{
} ;
void Print_it( )
{
cout.setf( ios::oct, ios::basefield );
cout << setw( 5 ) << num << endl;
}
} ;
class cBinary:public cNumber
{
public:
cBinary( ):cNumber( )
{
// INPUT :
Input
Number:
6
// OUTPUT :
6
6
110
// Example COde :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
template<class xType> xType Min( xType & a, xType & b )
{
if( a > b )
return b;
return a;
}
char * Min( char * S1, char * S2 )
{
for( int l1 = 0; S1[ l1 ]; l1++ ) // < >
;
for( int l2 = 0; S2[ l2 ]; l2++ ) // < >
;
if( l1 < l2 )
return S1;
return S2;
}
void main( )
{
int x = 40,
y = 60;
float a = 4.87,
b = .897;
char p = 'a',
q = 'A';
char str1[ 10 ] = "Mickey";
char str2[ 10 ] = "Don";
clrscr( );
cout << "Minimum of" << x << " " << y << " is :" << Min( x, y ) <<
endl;
cout << "Minimum of" << a << " " << b << " is :" << Min( a, b ) <<
endl;
cout << "Minimum of" << p << " " << q << " is :" << Min( p, q ) <<
endl;
cout << "Minimum of" << str1 << " " << str2 << "is :" << Min( str1,
str2 )
<< endl;
getch( );
}
// OUTPUT :
Minimum
of 40 60 is :40
Minimum of 4.87 0.897 is :0.897
Minimum of a A is :A
Minimum of Mickey Donis :Don
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
void main( )
{
clrscr( );
void reverse( );
cout << "Enter a String : ";
reverse( );
getch( );
}
void reverse( )
{
char x;
x = getchar( );
if( x == '\n' )
{
cout << endl << "Reverse is:";
return;
}
reverse( );
cout << x;
}
// INPUT :
Enter
a String : system
// OUTPUT :
Reverse
is:
metsys
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cClock
{
int hh,
mm,
ss;
public:
cClass( )
{
hh = mm = ss = 0;
}
void Set( int a = 0, int b = 0, int c = 0 );
void Increment( );
void Display( );
int IsValid( );
} ;
int cClock::IsValid( )
{
int flag = 0;
if( ( hh <= 23 && hh >= 0 )
&& ( mm <= 59 && mm >= 0 )
&& ( ss <= 59 && ss >= 0 ) )
flag = 1;
return flag;
}
void cClock::Set( int a, int b, int c )
{
hh = a;
mm = b;
ss = c;
}
void cClock::Increment( )
{
ss++;
if( ss >= 60 )
{
ss = 0;
mm = 0;
hh++;
}
if( hh > 23 )
hh = 0;
}
void cClock::Display( )
/*
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
class cClock
{
int hh,mm, ss;
public:
cClass ()
{hh=mm=ss=0;}
void Set(int a=0,int b=0,int c=0);
void Increment();
void Display();
int.IsValid();
};
int cClock :: IsValid()
{
int.flag=0;
if ((hh<=23 && hh>=0)&&(mm<=59 && mm>=0)&&(ss<=59 &&ss>=0))
flag=1;
return flag;
}
void cClock::Set(int a,int b,int c)
{
hh=a;
mm=b;
ss=c;
}
void cClock::Increment()
{
// OUTPUT :
INITIAL
DATE - 11 : 59:59PM
Incremented date -
00:00:00AM
00:00:01AM
00:00:02AM
00:00:03AM
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#include <dos.h>
#include <process.h>
class cDate
{
unsigned int yy,
dd,
mm;
public:
cDate( )
{
dd = mm = yy = 0;
}
cDate( int a, int b, int c )
{
dd = a;
mm = b;
yy = c;
}
void Set( );
void Increment( );
friend ostream & operator <<( ostream &,
cDate & );
} ;
ostream & operator <<( ostream & Out, cDate & D )
{
cout << setiosflags( ios::left );
Out << D.dd << "/" << D.mm << ";" << D.yy << endl;
return Out;
}
void cDate::Set( )
{
char buff[ 9 ];
_strdate( buff );
cout << "\nBuffer : " << buff << endl;
dd = ( buff[ 3 ] - 48 ) * 10 + buff[ 4 ] - 48;
mm = ( buff[ 0 ] - 48 ) * 10 + buff[ 1 ] - 48;
yy = ( buff[ 6 ] - 48 ) * 10 + buff[ 7 ] - 48;
buff[ 8 ] = '\0';
}
void cDate::Increment( )
{
int NoDays = 0;
dd++;
switch( mm )
{
case 1:
case 3:
case 5:
case 7:
// INPUT :
Buffer :
04/17/08
The Date is : 17/4;
8
Current date is Thu 04/17/2008
Enter new date (mm-dd-yy): 7-24-86
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <stdio.h>
#include <conio.h>
class cRevCase
{
char * str;
int len;
public:
cRevCase( )
{
str = '\0';
len = 0;
}
cRevCase( char * S )
{
;
len = i;
str = new char [len + 1];
int j = 0;
for( i = 0; S[ i ]; i++ )
str[ j++ ] = S[ i ];
str[ j ] = '\0';
}
inline void Read( )
{
cout << "Length of String : ";
cin >> len;
str = new char [len + 1];
cout << "Enter String :";
cin >> str;
}
inline void Print( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( len ) << str << endl;
}
cRevCase Convert( );
} ;
cRevCase cRevCase::Convert( )
{
cRevCase tmp;
tmp.len = len;
int j = 0;
tmp.str = new char [tmp.len + 1];
for( int i = 0; str[ i ]; i++ ) // < >
{
{
if( str[ i ] >= 'a' && str[ i ] <= 'z' )
tmp.str[ j++ ] = str[ i ] - 32;
else if( str[ i ] >= 'A'
&& str[ i ] <= 'Z' )
// INPUT :
Length
of String : 5
Enter String :SAGAR
// OUTPUT :
string
1 :MiCkEyl23
string 2 :mIcKeYL23
string 3 :SAGAR
string 4 :sagar
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
class cMedia
{
protected:
char title[ 10 ];
float price;
public:
cMedia( )
{
strcpy( title, '\0' );
price = 0.0;
}
cMedia( char * t, float p )
{
strcpy( title, t );
price = p;
}
void GetData( );
virtual void DispData( );
} ;
class cBook:public cMedia
{
int pages;
public:
cBook( ):cMedia( )
{
pages = 0;
}
cBook( char * t, float pr, int p )
:cMedia( t, pr )
{
pages = p;
}
void GetData( );
void DispData( );
} ;
class cTape:public cMedia
{
int len;
public:
cTape( ):cMedia( )
{
len = 0;
}
cTape( char * t, float pr, int p )
:cMedia( t, pr )
{
len = 1;
}
void GetData( );
void cMedia::GetData( )
{
cout << "Title:";
gets( title );
fflush( stdin );
cout << "Price:";
cin >> price;
}
void cMedia::DispData( )
{
cout.setf( ios::left, ios::adjustfield );
cout.precision( 3 );
cout << setw( 20 ) << title << "Rs. ";
cout.setf( ios::right, ios::adjustfield );
cout.setf( ios::showpoint );
cout << setw( 10 ) << price;
}
void cBook::GetData( )
{
cMedia::GetData( );
cout << "Pages : ";
Title:
khnh
Price:
Title:
Networking
Price:
442
Length . 120
Networking Rs. 442.000 120
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
class cElection
{
int votes[ 5 ];
int dis;
public:
cElection( );
void Cast( );
void Disp( );
} ;
cElection::cElection( )
{
for( int i = 0; i < 5; i++ ) // < >
votes[ i ] = 0;
dis = 0;
}
void cElection::Cast( )
{
int x;
cout << "Candidate Number : (1-5) :";
cin >> x;
if( x <= 5 && x > 0 )
votes[ x - 1 ]++;
else
dis++;
}
void cElection::Disp( )
{
for( int i = 0; i < 5; i++ ) // < >
Candidate
Number:
(1-5) :2
Want to cast more :Y
Candidate Number : (1-5) :2
Want to cast more :Y
Candidate Number : (1-5) :
4
Want to cast more :N
// OUTPUT :
1 0
2 2
3 0
4 1
5 0
Discarded votes =0
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
class cCList;
class cNode
{
char name[ 15 ];
int age;
float basic;
cNode * next;
public:
cNode( )
{
strcpy( name, '\0' );
age = 0;
basic = 0.0;
next = '\0';
}
cNode( char *, int, float );
void DispNode( );
friend class cCList;
} ;
class cCList
{
cNode * start,
* last;
public:
cCList( )
{
start = last = '\0';
}
void Add( );
void Add( char *, int, float );
void Search( char * );
void Delete( char * );
void DispList( );
} ;
cNode::cNode( char * n, int a, float b )
{
strcpy( name, n );
age = a;
basic = b;
next = '\0';
}
void cNode::DispNode( )
{
cout.setf( ios::left, ios::adjustfield );
cout << setw( 15 ) << name;
cout.setf( ios::right, ios::adjustfield );
cout << setw( 3 ) << age << setw( 6 ) << basic << endl;
}
void cCList::Add( )
{
char nm[ 15 ];
int a;
// OUTPUT :
ABC
12 1200
XYZ 24 2400
QUIT 32 13000
quit 22 3444
Name to be Deleted :QUIT
Found at Rec # 3
QUIT 32 13000
// Example Code :
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
void main( )
{
int * arr;
int n;
void Bubble( int *, int );
void Quick( int *, int, int );
void Insert( int *, int );
void Selection( int *, int );
clrscr( );
cout << "Enter No. of Elements :";
cin >> n;
arr = new int [n];
cout << "Enter the elemnents now " << endl;
for( int i = 0; i < n; i++ ) // < >
{
cout << "arr[" << i + 1 << "] :";
cin >> arr[ i ];
}
//clrscr();
cout << "Select Ur Choice " << endl;
cout << " 1 Bubble Sort " << endl;
cout << " 2 Insertion Sort " << endl;
cout << " 3 Selection Sort " << endl;
cout << " 4 Quick Sort " << endl;
cout << "Enter ur Choice (1-4) :";
int ch;
cin >> ch;
switch( ch )
{
case 1:
Bubble( arr, n );
break;
case 2:
Insert( arr, n );
break;
case 3:
Selection( arr, n );
break;
case 4:
Quick( arr, 0, n );
break;
default:
cout << "I N V A L I D C H 0 I C E E N T E R E D";
break;
}
cout << "Sorted List is " << endl;
cout.width( 4 );
for( i = 0; i < n; i++ )
cout << "arr[" << i + 1 << "] :" << arr[ i ] << endl;
getch( );
}
void Bubble( int * arr, int n )
{
for( int j = 0; j < n - 1 - i; j++ ) // < >
{
if( arr[ j ] > arr[ j + 1 ] )
{
int tmp = arr[ j ];
arr[ j ] = arr[ j + 1 ];
arr[ j + 1 ] = tmp;
}
}
}
}
void Insert( int * arr, int n )
{
int tmp;
for( int i = 1; i < n; i++ ) // < >
{
if( arr[ i - 1 ] > arr[ i ] )
tmp = arr[ i ];
for( int j = i - 1; // < >
{
int min = GetMin( arr, i, n );
tmp = arr[ min ];
arr[ min ] = arr[ i ];
arr[ i ] = tmp;
}
}
int GetMin( int * arr, int pos, int size )
{
int tmp,
min = pos++;
while( pos < size )
{
if( arr[ pos ] < arr[ min ] )
min = pos;
pos++;
}
return min;
}
void Quick( int * arr, int lo, int hi )
{
int Divide( int *, int, int );
int pivotloc;
if( lo < hi )
{
pivotloc = Divide( arr, lo, hi );
{
if( arr[ i ] <= pivotval )
{
int tmp = arr[ ++pivotloc ];
arr[ pivotloc ] = arr[ i ];
arr[ i ] = tmp;
}
}
arr[ lo ] = arr[ pivotloc ];
arr[ pivotloc ] = pivotval;
return pivotloc;
}
.// INPUT :
Enter
No. of Elements :6
Enter the elemnents now
arr[1] :23
arr[2] :87
arr[3] :65
arr[4] :12
arr[5] :99
arr[6] :43
Select Ur Choice
1 Bubble Sort
2 Insertion Sort
3 Selection Sort
4 Quick Sort
Enter ur Choice (1-4) :1
// OUTPUT :
Sorted
List is
arr[1] :12
arr[2] :23
arr[3] :43
arr[4] :65
arr[5] :87
arr[6] :99