|
//file: CODL04.cpp //Author: AOU //Date 10/20/2004 // Ordered Dynamic List //////////////////////////////////////////////////////// // includes //////////////////////////////////////////////////////// #include <iostream.h> #include <math.h> #include <stdlib.h> #include <time.h> #include <string.h> //////////////////////////////////////////////////////// // constants //////////////////////////////////////////////////////// int const MAX_SIZE = 10; char * PLUS_INFINITY = "zzz"; char * MINUS_INFINITY = "$$$"; int const TEST_COUNT = 10; int const MAX_VALUE = 10; int const MAX_LENGTH = 20+1; //////////////////////////////////////////////////////// //////////////////////////////////////////////////////// char *test_data[]= { "John", "James", "Bob", "Karen", "Zack", "Ann", "Camry", "Alero" }; int TEST_SIZE = sizeof(test_data)/sizeof(test_data[0]); //////////////////////////////////////////////////////// // class CNode //////////////////////////////////////////////////////// class CNode { private: char key[MAX_LENGTH]; CNode *next; public: CNode(void); CNode(char value[]); void display(void); friend class CODL; }; //////////////////////////////////////////////////////// // class CODL //////////////////////////////////////////////////////// class CODL { private: CNode *head; int n; void init(void); public: CODL(void); void display(void); bool insert(char x[]); CODL(char ch); ~CODL(void); bool isSorted(void)const; CODL(int m); CNode * search(char x[]) const; }; //////////////////////////////////////////////////////// // test functions prototypes //////////////////////////////////////////////////////// void test_insert(void); void test_isSorted(void); void test_constructorM(void); void test_search(void); void test_constructorR(void ); //////////////////////////////////////////////////////// //void main(void) //////////////////////////////////////////////////////// void main(void) { test_constructorR(); test_isSorted(); test_constructorM(); test_search(); } //////////////////////////////////////////////////////// // CNode * CODL::search(char x[]) const //////////////////////////////////////////////////////// CNode * CODL::search(char x[]) const { for (CNode *p=head->next; p->next!=NULL; p=p->next) { if (strcmp(x, p->key)==0) return p; if (strcmp(x, p->key)<0) break; } return NULL; } //////////////////////////////////////////////////////// // void test_search(void) //////////////////////////////////////////////////////// void test_search(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_search\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; CODL myList(m); myList.display(); char x[MAX_LENGTH]; strcpy(x, test_data[rand()%TEST_SIZE]); cout << "Searching for " << x << endl; cout << "Found at " << myList.search(x) << endl; } } //////////////////////////////////////////////////////// // CODL::CODL(int m) //////////////////////////////////////////////////////// //create a list with m values CODL::CODL(int m) { init(); for (int i=1; i<=m; i++) { int r = rand()%TEST_SIZE; this->insert(test_data[r]); } } //////////////////////////////////////////////////////// // void test_constructorM(void) //////////////////////////////////////////////////////// void test_constructorM(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_constructorM\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int m = rand()%MAX_SIZE; CODL myList(m); cout << "After CODL myList(" << m << ");\n"; myList.display();// display(myArray, m); } } //////////////////////////////////////////////////////// // bool COOL::isSorted(void) //////////////////////////////////////////////////////// /* Description: checks if the list is in order Example: Algorithm: */ bool CODL::isSorted(void) const { for (CNode *p=head; p->next!=NULL; p=p->next) { cout << "(" << p->key << ", " << p->next->key << ")\n"; if (strcmp(p->key, p->next->key)>0) return false; } return true; } //////////////////////////////////////////////////////// // void test_isSorted(void) //////////////////////////////////////////////////////// void test_isSorted(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_isSorted\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CODL *myListPtr; myListPtr = new CODL('r'); (*myListPtr).display(); if(myListPtr->isSorted()) cout << "List is sorted\n"; else cout << "List is NOT sorted\n"; delete myListPtr; } } //////////////////////////////////////////////////////// // CODL::~CODL(void) //////////////////////////////////////////////////////// CODL::~CODL(void) { cout << "Destructor\n"; CNode *q, *p = head; while (p!= NULL) { // eating pizza q = p->next; delete p; p = q; } } //////////////////////////////////////////////////////// // CODL::CODL(char ch) //////////////////////////////////////////////////////// CODL::CODL(char ch) { init(); if ('r' == ch) { int m = rand()%MAX_SIZE; for (int i=1; i<=m; i++) { int r = rand()%TEST_SIZE; this->insert(test_data[r]); } } } //////////////////////////////////////////////////////// // void test_constructorR(void) //////////////////////////////////////////////////////// void test_constructorR(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_constructorR\n"; cout << "+++++++++++++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CODL *myListPtr; myListPtr = new CODL('r'); //myListPtr->display(); delete myListPtr; //myList.display(); } } //////////////////////////////////////////////////////// // bool CODL::insert(char x[]) //////////////////////////////////////////////////////// bool CODL::insert(char x[]) { if (n >= MAX_SIZE) return false; CNode *p; p = new CNode(x); CNode *q; q = head; //while (q->next->key <= x) while (strcmp(q->next->key, x) <= 0) q = q->next; p->next = q->next; q->next = p; n++; return true; } //////////////////////////////////////////////////////// // void test_insert(void) //////////////////////////////////////////////////////// void test_insert(void) { cout << "+++++++++++++++++++++++++++++++\n"; cout << "test_insert\n"; cout << "+++++++++++++++++++++++++++++++\n"; CODL myList; myList.display(); for (int i=1; i<=TEST_COUNT; i++) { int r = rand()%TEST_SIZE; myList.insert(test_data[r]); myList.display(); } } //////////////////////////////////////////////////////// // CODL::CODL(void) //////////////////////////////////////////////////////// CODL::CODL(void) { init(); } //////////////////////////////////////////////////////// // void CODL::init(void) //////////////////////////////////////////////////////// void CODL::init(void) { CNode *p, *q; p = new CNode(MINUS_INFINITY); q = new CNode(PLUS_INFINITY); head = p; p->next = q; q->next = NULL; n = 0; } //////////////////////////////////////////////////////// // void CODL::display(void) //////////////////////////////////////////////////////// void CODL::display(void) { cout << "List[" << n << "] = "; CNode *p = head; while (p != NULL) { p->display(); cout << ' '; p = p->next; } cout << endl; } //////////////////////////////////////////////////////// //CNode::CNode(void) //////////////////////////////////////////////////////// CNode::CNode(void) { strcpy(key, ""); next = NULL; }; //////////////////////////////////////////////////////// // CNode::CNode(char value[]) //////////////////////////////////////////////////////// CNode::CNode(char value[]) { strcpy(key, value); next = NULL; }; //////////////////////////////////////////////////////// // void CNode::display(void) //////////////////////////////////////////////////////// void CNode::display(void) { cout << key; } |