//CSet05.cpp
//Date 05/02/2003
//Author: AOU
/*
Added the following functions:
void display(void) const;
CNode * addressOfNext(void)
friend ostream & operator << (ostream & tom, const CSet &set);
*/
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
////////////////////////////////////////////////////////////
const int UNDEFINED = -99;
const int MAX_VALUE = 99;
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
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
{
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(CNode n);
CNode* seqSearch(int x);
void sortBubble(void);
void sortSelection(void);
void sortInsertion(void);
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
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)
{
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
{
cout << "CSet[" << this->m_n << "]=";
CNode *p;
p = this->m_first;
while (p != NULL)
{
cout << *p << ' ';
p = p->addressOfNext();
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
CSet::CSet(void)
{
this->m_first = NULL;
this->m_n = 0;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
CNode * CNode::addressOfNext(void)
{
return this->m_next;
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
bool CNode::operator != (CNode node)
{
return (this->m_info != node.m_info);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
bool CNode::operator <= (CNode node)
{
return (this->m_info <= node.m_info);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
bool CNode::operator < (CNode node)
{
return (this->m_info < node.m_info);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
bool CNode::operator == (CNode node)
{
return (this->m_info == node.m_info);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
ostream & operator << (ostream & tom, CNode node)
{
tom << node.m_info;
return tom;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void CNode::set(int x)
{
this->m_info = x;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
int CNode::get(void)
{
return this->m_info;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
CNode::CNode(char ch)
{
if ('r' == ch)
this->m_info = rand()%(MAX_VALUE+1);
else
this->m_info = UNDEFINED;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
CNode::CNode(void)
{
this->m_info = UNDEFINED;
this->m_next = NULL;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void CNode::display(void) const
{
cout << this->m_info;
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
CNode::CNode(int x)
{
this->m_info = x;
this->m_next = 0;
}
/*
SAMPLE RUN:
CSet[0]=
CSet[0]=
CSet[1]=5
CSet[1]=5
CSet[2]=6 5
CSet[2]=6 5
Press any key to continue
*/
|