//CSet06.cpp
//Date 05/05/2003
//Author: AOU
/*
Added the following functions:
CSet(char ch);
~CSet(void);
*/
////////////////////////////////////////////////////////////
// include files
////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
////////////////////////////////////////////////////////////
// constants
////////////////////////////////////////////////////////////
const int UNDEFINED = -99;
const int MAX_VALUE = 99;
const int MAX_COUNT = 20;
////////////////////////////////////////////////////////////
// class CNode
////////////////////////////////////////////////////////////
class CNode
{
private:
int m_info;
CNode *m_next;
public:
CNode(void);
CNode(int x);
void display(void) const;
CNode(char ch);
int get(void);
void set(int x);
friend ostream & operator << (ostream & tom, CNode node);
bool operator == (CNode node);
bool operator < (CNode node);
bool operator <= (CNode node);
bool operator != (CNode node);
friend class CSet;
CNode * addressOfNext(void);
};
////////////////////////////////////////////////////////////
// class CSet
////////////////////////////////////////////////////////////
class CSet
{
private:
CNode *m_first;
int m_n;
public:
CSet(void);
void display(void) const;
void insertAtHead(int x);
friend ostream & operator << (ostream & tom, const CSet &set);
CSet(char ch);
~CSet(void);
CSet(CNode n);
CNode* seqSearch(int x);
void sortBubble(void);
void sortSelection(void);
void sortInsertion(void);
};
////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////
void main(void)
{
/*
CNode myNode;
myNode.display();
cout << endl;
CNode myNode1(55);
myNode1.display();
cout << endl;
CNode myNode2('r');
myNode2.display();
cout << endl;
cout << "info=" << myNode2.get() << endl;
myNode2.set(32);
cout << "info=" << myNode2.get() << endl;
cout << myNode2 << endl;
if (myNode1 == myNode2)
cout << "myNode1 is equal to myNode2\n";
else
cout << "myNode1 is NOT equal to myNode2\n";
myNode1 = myNode2;
if (myNode1 == myNode2)
cout << "myNode1 is equal to myNode2\n";
else
cout << "myNode1 is NOT equal to myNode2\n";
*/
/*
{
cout << "Testing operator < \n";
cout << "-------------------\n";
CNode n1('r'), n2('r');
cout << n1 << endl << n2 << endl;
if (n1 < n2)
cout << "n1 is less than n2\n";
else
cout << "n1 is NOT less than n2\n";
CNode m1('r'), m2('r');
cout << m1 << endl << m2 << endl;
if (m1 < m2)
cout << "m1 is less than m2\n";
else
cout << "m1 is NOT less than m2\n";
}
{
cout << "Testing operator <= \n";
cout << "--------------------\n";
CNode n1('r'), n2('r');
cout << "n1=" << n1 << endl << "n2=" << n2 << endl;
if (n1 <= n2)
cout << "n1 is <= n2\n";
else
cout << "n1 is NOT <= n2\n";
if (n2 <= n1)
cout << "n2 is <= n1\n";
else
cout << "n2 is NOT <= n1\n";
}
{
cout << "Testing operator != \n";
cout << "--------------------\n";
CNode n1('r'), n2('r');
cout << "n1=" << n1 << endl << "n2=" << n2 << endl;
if (n1 != n2)
cout << "n1 is != n2\n";
else
cout << "n1 is = n2\n";
n1 = n2;
if (n1 != n2)
cout << "n1 is != n2\n";
else
cout << "n1 is = n2\n";
}
*/
/*
{
CSet set1;
set1.display();
cout << endl;
cout << set1;
cout << endl;
set1.insertAtHead(5);
set1.display();
cout << endl;
cout << set1;
cout << endl;
set1.insertAtHead(6);
set1.display();
cout << endl;
cout << set1;
cout << endl;
}
*/
{
for (int i=1; i<=10; i++)
{
CSet set1('r');
cout << set1 << endl;
}
}
}
CSet::~CSet(void)
{
cout << "Destructor working\n";
CNode *p, *q;
p = this->m_first;
if (NULL == p) return;
do
{
q = p->addressOfNext();
cout << "about to eliminate " << *p << " at " << p << endl;
delete p;
p = q;
}
while (p != NULL);
this->m_first = NULL;
this->m_n = 0;
}
CSet::CSet(char ch)
{
if (('r' == ch) || ('R' == ch))
{
int n = rand()%(MAX_COUNT+1);
this->m_n = 0;
this->m_first = NULL;
for (int i=1; i<=n; i++)
{
int x = rand()%(MAX_VALUE+1);
this->insertAtHead(x);
}
}
}
////////////////////////////////////////////////////////////
// void CSet::insertAtHead(int x)
////////////////////////////////////////////////////////////
void CSet::insertAtHead(int x)
{
CNode *p;
p = new CNode;
p->m_info = x;
p->m_next = this->m_first;
this->m_first = p;
this->m_n++;
}
////////////////////////////////////////////////////////////
// ostream & operator << (ostream & tom, const CSet &set)
////////////////////////////////////////////////////////////
ostream & operator << (ostream & tom, const CSet &set)
{
cout << "CSet[" << set.m_n << "]=";
CNode *p;
p = set.m_first;
while (p != NULL)
{
cout << *p << ' ';
p = p->addressOfNext();
}
return tom;
}
////////////////////////////////////////////////////////////
// void CSet::display(void) const
////////////////////////////////////////////////////////////
void CSet::display(void) const
{
cout << "CSet[" << this->m_n << "]=";
CNode *p;
p = this->m_first;
while (p != NULL)
{
cout << *p << ' ';
p = p->addressOfNext();
}
}
////////////////////////////////////////////////////////////
// CSet::CSet(void)
////////////////////////////////////////////////////////////
CSet::CSet(void)
{
this->m_first = NULL;
this->m_n = 0;
}
////////////////////////////////////////////////////////////
// CNode * CNode::addressOfNext(void)
////////////////////////////////////////////////////////////
CNode * CNode::addressOfNext(void)
{
return this->m_next;
};
////////////////////////////////////////////////////////////
// bool CNode::operator != (CNode node)
////////////////////////////////////////////////////////////
bool CNode::operator != (CNode node)
{
return (this->m_info != node.m_info);
}
////////////////////////////////////////////////////////////
// bool CNode::operator <= (CNode node)
////////////////////////////////////////////////////////////
bool CNode::operator <= (CNode node)
{
return (this->m_info <= node.m_info);
}
////////////////////////////////////////////////////////////
// bool CNode::operator < (CNode node)
////////////////////////////////////////////////////////////
bool CNode::operator < (CNode node)
{
return (this->m_info < node.m_info);
}
////////////////////////////////////////////////////////////
// bool CNode::operator == (CNode node)
////////////////////////////////////////////////////////////
bool CNode::operator == (CNode node)
{
return (this->m_info == node.m_info);
}
////////////////////////////////////////////////////////////
// ostream & operator << (ostream & tom, CNode node)
////////////////////////////////////////////////////////////
ostream & operator << (ostream & tom, CNode node)
{
tom << node.m_info;
return tom;
}
////////////////////////////////////////////////////////////
// void CNode::set(int x)
////////////////////////////////////////////////////////////
void CNode::set(int x)
{
this->m_info = x;
}
////////////////////////////////////////////////////////////
// int CNode::get(void)
////////////////////////////////////////////////////////////
int CNode::get(void)
{
return this->m_info;
}
////////////////////////////////////////////////////////////
// CNode::CNode(char ch)
////////////////////////////////////////////////////////////
CNode::CNode(char ch)
{
if ('r' == ch)
this->m_info = rand()%(MAX_VALUE+1);
else
this->m_info = UNDEFINED;
}
////////////////////////////////////////////////////////////
// CNode::CNode(void)
////////////////////////////////////////////////////////////
CNode::CNode(void)
{
this->m_info = UNDEFINED;
this->m_next = NULL;
}
////////////////////////////////////////////////////////////
// void CNode::display(void) const
////////////////////////////////////////////////////////////
void CNode::display(void) const
{
cout << this->m_info;
}
////////////////////////////////////////////////////////////
// CNode::CNode(int x)
////////////////////////////////////////////////////////////
CNode::CNode(int x)
{
this->m_info = x;
this->m_next = 0;
}
/*
SAMPLE RUN:
CSet[20]=91 36 27 42 95 91 61 27 81 45 5 64 62 58 78 24 69 0 34 67
Destructor working
about to eliminate 91 at 0x00431170
about to eliminate 36 at 0x004311B0
about to eliminate 27 at 0x004311F0
about to eliminate 42 at 0x00431230
about to eliminate 95 at 0x00431270
about to eliminate 91 at 0x004312B0
about to eliminate 61 at 0x004312F0
about to eliminate 27 at 0x00431330
about to eliminate 81 at 0x00431370
about to eliminate 45 at 0x004313B0
about to eliminate 5 at 0x004313F0
about to eliminate 64 at 0x00431430
about to eliminate 62 at 0x00431470
about to eliminate 58 at 0x004314B0
about to eliminate 78 at 0x004314F0
about to eliminate 24 at 0x00431530
about to eliminate 69 at 0x00431570
about to eliminate 0 at 0x004315B0
about to eliminate 34 at 0x004315F0
about to eliminate 67 at 0x00431630
CSet[9]=47 95 18 16 21 82 92 53 2
Destructor working
about to eliminate 47 at 0x00431430
about to eliminate 95 at 0x00431470
about to eliminate 18 at 0x004314B0
about to eliminate 16 at 0x004314F0
about to eliminate 21 at 0x00431530
about to eliminate 82 at 0x00431570
about to eliminate 92 at 0x004315B0
about to eliminate 53 at 0x004315F0
about to eliminate 2 at 0x00431630
CSet[12]=33 22 11 3 94 35 99 67 12 69 38 71
Destructor working
about to eliminate 33 at 0x00431370
about to eliminate 22 at 0x004313B0
about to eliminate 11 at 0x004313F0
about to eliminate 3 at 0x00431430
about to eliminate 94 at 0x00431470
about to eliminate 35 at 0x004314B0
about to eliminate 99 at 0x004314F0
about to eliminate 67 at 0x00431530
about to eliminate 12 at 0x00431570
about to eliminate 69 at 0x004315B0
about to eliminate 38 at 0x004315F0
about to eliminate 71 at 0x00431630
CSet[12]=23 59 37 57 62 44 47 68 53 11 41 64
Destructor working
about to eliminate 23 at 0x00431370
about to eliminate 59 at 0x004313B0
about to eliminate 37 at 0x004313F0
about to eliminate 57 at 0x00431430
about to eliminate 62 at 0x00431470
about to eliminate 44 at 0x004314B0
about to eliminate 47 at 0x004314F0
about to eliminate 68 at 0x00431530
about to eliminate 53 at 0x00431570
about to eliminate 11 at 0x004315B0
about to eliminate 41 at 0x004315F0
about to eliminate 64 at 0x00431630
CSet[18]=50 70 29 90 5 46 48 64 42 40 6 88 42 90 35 16 78 29
Destructor working
about to eliminate 50 at 0x004311F0
about to eliminate 70 at 0x00431230
about to eliminate 29 at 0x00431270
about to eliminate 90 at 0x004312B0
about to eliminate 5 at 0x004312F0
about to eliminate 46 at 0x00431330
about to eliminate 48 at 0x00431370
about to eliminate 64 at 0x004313B0
about to eliminate 42 at 0x004313F0
about to eliminate 40 at 0x00431430
about to eliminate 6 at 0x00431470
about to eliminate 88 at 0x004314B0
about to eliminate 42 at 0x004314F0
about to eliminate 90 at 0x00431530
about to eliminate 35 at 0x00431570
about to eliminate 16 at 0x004315B0
about to eliminate 78 at 0x004315F0
about to eliminate 29 at 0x00431630
CSet[12]=31 76 66 40 56 54 84 23 29 48 93 1
Destructor working
about to eliminate 31 at 0x00431370
about to eliminate 76 at 0x004313B0
about to eliminate 66 at 0x004313F0
about to eliminate 40 at 0x00431430
about to eliminate 56 at 0x00431470
about to eliminate 54 at 0x004314B0
about to eliminate 84 at 0x004314F0
about to eliminate 23 at 0x00431530
about to eliminate 29 at 0x00431570
about to eliminate 48 at 0x004315B0
about to eliminate 93 at 0x004315F0
about to eliminate 1 at 0x00431630
CSet[16]=30 4 58 39 15 33 41 29 82 18 38 37 23 26 39 44
Destructor working
about to eliminate 30 at 0x00431270
about to eliminate 4 at 0x004312B0
about to eliminate 58 at 0x004312F0
about to eliminate 39 at 0x00431330
about to eliminate 15 at 0x00431370
about to eliminate 33 at 0x004313B0
about to eliminate 41 at 0x004313F0
about to eliminate 29 at 0x00431430
about to eliminate 82 at 0x00431470
about to eliminate 18 at 0x004314B0
about to eliminate 38 at 0x004314F0
about to eliminate 37 at 0x00431530
about to eliminate 23 at 0x00431570
about to eliminate 26 at 0x004315B0
about to eliminate 39 at 0x004315F0
about to eliminate 44 at 0x00431630
CSet[12]=97 73 77 29 70 72 24 45 21 86 73 6
Destructor working
about to eliminate 97 at 0x00431370
about to eliminate 73 at 0x004313B0
about to eliminate 77 at 0x004313F0
about to eliminate 29 at 0x00431430
about to eliminate 70 at 0x00431470
about to eliminate 72 at 0x004314B0
about to eliminate 24 at 0x004314F0
about to eliminate 45 at 0x00431530
about to eliminate 21 at 0x00431570
about to eliminate 86 at 0x004315B0
about to eliminate 73 at 0x004315F0
about to eliminate 6 at 0x00431630
CSet[6]=67 55 36 61 90 86
Destructor working
about to eliminate 67 at 0x004314F0
about to eliminate 55 at 0x00431530
about to eliminate 36 at 0x00431570
about to eliminate 61 at 0x004315B0
about to eliminate 90 at 0x004315F0
about to eliminate 86 at 0x00431630
CSet[9]=30 66 24 41 50 50 52 31 74
Destructor working
about to eliminate 30 at 0x00431430
about to eliminate 66 at 0x00431470
about to eliminate 24 at 0x004314B0
about to eliminate 41 at 0x004314F0
about to eliminate 50 at 0x00431530
about to eliminate 50 at 0x00431570
about to eliminate 52 at 0x004315B0
about to eliminate 31 at 0x004315F0
about to eliminate 74 at 0x00431630
Press any key to continue
*/
|