|
//DSortedList06.cpp //date: 11/15/2002 //author: AOU //Project#6 Due 11/20/2002 //Dynamic sorted list //////////////////////////////////////////////////////////// // Include files //////////////////////////////////////////////////////////// #include<iostream.h> #include<math.h> #include<stdlib.h> #include<time.h> //////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// const int MAX_SIZE = 20; //maximum entries allowed in a list const int UNDEFINED = -9999; const int MAX_VALUE = 50; const int TEST_COUNT = 20; const int INFINITY = 32000; void testInsert(void); void testConstructorN(void); void testConstructorChar(void); void testRemoveAll(void); void testDestructor(void); void testAdd(void); //project#6 Due 11/20/2002 void testDisplayRev(void); const int sValues[] = {55, 12, 34, 71, 98, 45, 20}; const int S_SIZE = sizeof(sValues)/sizeof(sValues[0]); //////////////////////////////////////////////////////////// //class CNode //////////////////////////////////////////////////////////// class CNode { private: int m_key; // int age; // char name[20]; // char socsec[10]; CNode *m_next; public: CNode(void); //2002.10.30 void display(void); //2002.10.30 CNode(int x); //2002.11.04 CNode(char ch); //2002.11.04 //... friend class CDSortedList; friend ostream & operator << //2002.11.13 (ostream & bob, const CDSortedList &aList); friend ostream & operator << //2002.11.15 (ostream & bob, const CNode &aNode); }; //////////////////////////////////////////////////////////// //class CDSortedList //////////////////////////////////////////////////////////// class CDSortedList { private: int m_count; CNode *m_first; void init(void); //should only be called by a constructor public: CDSortedList(void); //2002.10.30 void display(void); //2002.10.30 bool insert(int x); //2002.11.04 CDSortedList(int n); //2002.11.06 CDSortedList(char ch);//2002.11.06 void removeAll(void); //2002.11.08 all except dummy values ~CDSortedList(void); //2002.11.08 //project#6 Due 11/20/2002 //Submit well-documented source code and the output //The resulting list should be sorted with unique values friend void add(const CDSortedList &aList, const CDSortedList &bList, CDSortedList &cList); friend ostream & operator << //2002.11.13 (ostream & bob, const CDSortedList &aList); CNode* addByPos(int pos); //2002.11.15 void displayRev(void); // 2002.11.15 }; //////////////////////////////////////////////////////////// //void main(void) //////////////////////////////////////////////////////////// void main(void) { srand(time(NULL)); // testInsert(); //testConstructorN(); //testConstructorChar(); //testRemoveAll(); //testDestructor(); //testAdd(); testDisplayRev(); } //////////////////////////////////////////////////////////// // void CDSortedList::displayRev(void) //////////////////////////////////////////////////////////// void CDSortedList::displayRev(void) { cout << "SortedRevr[" << this->m_count << "]= "; for (int i = this->m_count-1; i>=0; i--) { CNode *q = this->addByPos(i); cout << *q; } cout << endl; } //////////////////////////////////////////////////////////// // void testDisplayRev(void) //////////////////////////////////////////////////////////// void testDisplayRev(void) { for (int i=0; i<TEST_COUNT; i++) { CDSortedList myList('r'); cout << myList; myList.displayRev(); cout << "------------------------\n"; } } //////////////////////////////////////////////////////////// // CNode* CDSortedList::addByPos(int pos) //////////////////////////////////////////////////////////// CNode* CDSortedList::addByPos(int pos) { /* Possibilities: Empty list pos < 0 pos >= m_count pos >=0 and pos <m_count */ if (this->m_count == 0) return NULL; if (pos < 0) return NULL; if (pos >= this->m_count) return NULL; CNode *p; p = this->m_first->m_next; while (pos > 0) { pos--; p = p->m_next; } return p; } //////////////////////////////////////////////////////////// // add //////////////////////////////////////////////////////////// void add(const CDSortedList &aList, const CDSortedList &bList, CDSortedList &cList) { //put in c distinct values from a and b //a={1, 3, 5, 5}, b={3, 3, 4, 6, 6} then //c={1, 3, 4, 5, 6} cout << "NOT IMPLEMENTED\n"; } //////////////////////////////////////////////////////////// // testAdd //////////////////////////////////////////////////////////// void testAdd(void) { cout << "testAdd\n"; cout << "-------\n"; int m1 = rand()%(MAX_SIZE/2); int m2 = rand()%(MAX_SIZE/2); for (int i=0; i<TEST_COUNT; i++) { CDSortedList aL(m1), bL(m2), cL('r'); cout << "aL = " << aL; cout << "bL = " << bL; cout << "cL = " << cL; add(aL, bL, cL); cout << "After add(aL, bL, cL);\n"; cout << "aL = " << aL; cout << "bL = " << bL; cout << "cL = " << cL; cout <<"=================\n"; } } //////////////////////////////////////////////////////////// // operator << //////////////////////////////////////////////////////////// ostream & operator << (ostream & bob, const CNode &aNode) { bob << aNode.m_key << ' '; return bob; } //////////////////////////////////////////////////////////// // operator << //////////////////////////////////////////////////////////// ostream & operator << (ostream & bob, const CDSortedList &aList) { bob << "SortedList[" << aList.m_count << "]= "; CNode *p; p = aList.m_first->m_next; while (p->m_next != NULL) { bob << *p; p = p->m_next; } bob << endl; return bob; } //////////////////////////////////////////////////////////// // CDSortedList::~CDSortedList(void) //////////////////////////////////////////////////////////// CDSortedList::~CDSortedList(void) { cout << "Destructor was called\n"; CNode *cia1, *cia2; cia1 = this->m_first; while (cia1 != NULL) { cia2 = cia1->m_next; delete cia1; cia1 = cia2; } } //////////////////////////////////////////////////////////// // void testDestructor(void) //////////////////////////////////////////////////////////// void testDestructor(void) { cout << "testDestructor\n"; cout << "==============\n"; CDSortedList *mp1 = new CDSortedList('c'); mp1->display(); delete mp1; cout << "-----------------\n"; for (int i=0; i<TEST_COUNT; i++) { CDSortedList *mp1 = new CDSortedList('r'); mp1->display(); delete mp1; cout << "-----------------\n"; } } //////////////////////////////////////////////////////////// // void CDSortedList::removeAll(void) //////////////////////////////////////////////////////////// void CDSortedList::removeAll(void) { CNode *cia1, *cia2; cia1 = this->m_first; while (cia1 != NULL) { cia2 = cia1->m_next; delete cia1; cia1 = cia2; } this->init(); } //////////////////////////////////////////////////////////// // void testRemoveAll(void) //////////////////////////////////////////////////////////// void testRemoveAll(void) { cout << "testRemoveAll\n"; cout << "=============\n"; CDSortedList myList('c'); myList.display(); myList.removeAll(); myList.display(); cout << "-----------------\n"; for (int i=0; i<TEST_COUNT; i++) { CDSortedList myList('r'); myList.display(); myList.removeAll(); myList.display(); cout << "-----------------\n"; } } //////////////////////////////////////////////////////////// // void CDSortedList::init(void) //////////////////////////////////////////////////////////// void CDSortedList::init(void) { CNode *p1 = new CNode; CNode *p2 = new CNode; this->m_first = p1; p1->m_next = p2; p2->m_next = NULL; p1->m_key = -INFINITY; p2->m_key = +INFINITY; this->m_count = 0; } //////////////////////////////////////////////////////////// // CDSortedList::CDSortedList(char ch) //////////////////////////////////////////////////////////// CDSortedList::CDSortedList(char ch) { this->init(); if (('r' == ch) || ('R' == ch)) { //insert random number of random values to the list int n = rand()%(MAX_SIZE+1); for (int i=0; i<n; i++) this->insert(rand()%(MAX_VALUE+1)); } if (('c' == ch) || ('C' == ch)) { //insert random number of random values to the list for (int i=0; i<S_SIZE; i++) this->insert(sValues[i]); } } //////////////////////////////////////////////////////////// // void testConstructorChar(void) //////////////////////////////////////////////////////////// void testConstructorChar(void) { cout << "testConstructorChar\n"; cout << "===================\n"; CDSortedList myList('c'); myList.display(); cout << "-----------------\n"; for (int i=0; i<TEST_COUNT; i++) { CDSortedList myList('r'); myList.display(); cout << "-----------------\n"; } } //////////////////////////////////////////////////////////// // CDSortedList::CDSortedList(int n) //////////////////////////////////////////////////////////// CDSortedList::CDSortedList(int n) { this->init(); //insert n random values to the list for (int i=0; i<n; i++) this->insert(rand()%(MAX_VALUE+1)); } //////////////////////////////////////////////////////////// // void testConstructorN(void) //////////////////////////////////////////////////////////// void testConstructorN(void) { cout << "testConstructorN\n"; cout << "================\n"; for (int i=0; i<TEST_COUNT; i++) { CDSortedList myList(rand()%MAX_SIZE); myList.display(); } } //////////////////////////////////////////////////////////// // bool CDSortedList::insert(int x) //////////////////////////////////////////////////////////// bool CDSortedList::insert(int x) { if (this->m_count >= MAX_SIZE) return false; CNode *p, *p1, *p2; p = new CNode(x); if (NULL == p) return false; p1 = this->m_first; p2 = p1->m_next; while(true) { if ((x >= p1->m_key) && (x <= p2->m_key)) { p1->m_next = p; p->m_next = p2; this->m_count++; return true; } else { p1 = p1->m_next; p2 = p1->m_next; } } } void testInsert(void) { cout << "testInsert\n"; cout << "==========\n"; CDSortedList myList; myList.display(); for (int i=0; i<10; i++) { int x = rand()%MAX_VALUE; myList.insert(x); cout << "After inserting " << x << endl; myList.display(); } } //////////////////////////////////////////////////////////// //void CDSortedList::display(void) //////////////////////////////////////////////////////////// void CDSortedList::display(void) { cout << "List[" << this->m_count << "] = \n"; CNode *p; p = this->m_first; while (p != NULL) { p->display(); p = p->m_next; cout << endl; } cout << endl; } //////////////////////////////////////////////////////////// //CDSortedList::CDSortedList(void) //////////////////////////////////////////////////////////// CDSortedList::CDSortedList(void) { /* create two nodes one pointed to by p1, other pointed to by p2 make first point to p1 make p1 point to p2 make p2 point to nothing put -infin in p1 node put +infin in p2 node set count = 0 */ this->init(); } //////////////////////////////////////////////////////////// // CNode::CNode(char ch) //////////////////////////////////////////////////////////// CNode::CNode(char ch) { if (('r' == ch) || ('R' == ch)) { this->m_key = rand()%(MAX_VALUE+1); this->m_next = NULL; } else { this->m_key = UNDEFINED; this->m_next = NULL; } } //////////////////////////////////////////////////////////// // CNode::CNode(int x) //////////////////////////////////////////////////////////// CNode::CNode(int x) { this->m_key = x; this->m_next = NULL; } //////////////////////////////////////////////////////////// //CNode::CNode(void) //////////////////////////////////////////////////////////// CNode::CNode(void) { this->m_key = UNDEFINED; this->m_next = NULL; } //////////////////////////////////////////////////////////// //void CNode::display(void) //////////////////////////////////////////////////////////// void CNode::display(void) { //cout << "["; //cout << this << " "; cout << this->m_key << " "; //cout << this->m_next << "] "; } /* SAMPLE RUN: SortedList[17]= 0 0 2 5 10 13 13 15 17 19 26 27 31 41 44 44 49 SortedRevr[17]= 49 44 44 41 31 27 26 19 17 15 13 13 10 5 2 0 0 ------------------------ Destructor was called SortedList[10]= 7 8 19 33 36 39 40 45 47 48 SortedRevr[10]= 48 47 45 40 39 36 33 19 8 7 ------------------------ Destructor was called SortedList[0]= SortedRevr[0]= ------------------------ Destructor was called SortedList[9]= 5 14 26 27 32 43 44 48 50 SortedRevr[9]= 50 48 44 43 32 27 26 14 5 ------------------------ Destructor was called SortedList[19]= 2 6 6 7 12 12 14 20 24 25 26 35 35 38 39 41 45 46 49 SortedRevr[19]= 49 46 45 41 39 38 35 35 26 25 24 20 14 12 12 7 6 6 2 ------------------------ Destructor was called SortedList[6]= 2 26 29 37 42 48 SortedRevr[6]= 48 42 37 29 26 2 ------------------------ Destructor was called SortedList[19]= 0 0 4 5 6 10 10 13 13 13 16 20 27 33 38 46 46 48 48 SortedRevr[19]= 48 48 46 46 38 33 27 20 16 13 13 13 10 10 6 5 4 0 0 ------------------------ Destructor was called SortedList[3]= 23 24 27 SortedRevr[3]= 27 24 23 ------------------------ Destructor was called SortedList[7]= 31 31 33 40 41 43 47 SortedRevr[7]= 47 43 41 40 33 31 31 ------------------------ Destructor was called SortedList[19]= 0 7 11 16 18 21 21 22 27 29 29 29 37 43 43 46 47 47 49 SortedRevr[19]= 49 47 47 46 43 43 37 29 29 29 27 22 21 21 18 16 11 7 0 ------------------------ Destructor was called SortedList[9]= 1 3 14 18 33 39 44 45 46 SortedRevr[9]= 46 45 44 39 33 18 14 3 1 ------------------------ Destructor was called SortedList[9]= 1 16 18 24 28 29 35 46 48 SortedRevr[9]= 48 46 35 29 28 24 18 16 1 ------------------------ Destructor was called SortedList[13]= 0 5 10 12 16 26 28 35 38 44 45 45 47 SortedRevr[13]= 47 45 45 44 38 35 28 26 16 12 10 5 0 ------------------------ Destructor was called SortedList[14]= 4 4 7 11 14 23 28 30 32 34 34 39 41 46 SortedRevr[14]= 46 41 39 34 34 32 30 28 23 14 11 7 4 4 ------------------------ Destructor was called SortedList[9]= 1 3 7 17 25 31 37 45 46 SortedRevr[9]= 46 45 37 31 25 17 7 3 1 ------------------------ Destructor was called SortedList[16]= 4 5 5 5 11 12 14 20 23 25 28 31 34 39 47 49 SortedRevr[16]= 49 47 39 34 31 28 25 23 20 14 12 11 5 5 5 4 ------------------------ Destructor was called SortedList[7]= 3 9 18 22 31 39 49 SortedRevr[7]= 49 39 31 22 18 9 3 ------------------------ Destructor was called SortedList[8]= 7 9 12 18 24 31 31 35 SortedRevr[8]= 35 31 31 24 18 12 9 7 ------------------------ Destructor was called SortedList[1]= 18 SortedRevr[1]= 18 ------------------------ Destructor was called SortedList[13]= 0 7 7 8 9 10 17 17 23 35 43 45 48 SortedRevr[13]= 48 45 43 35 23 17 17 10 9 8 7 7 0 ------------------------ Destructor was called Press any key to continue */ |