|
//file SLL03.cpp //date 04/27/2005 //author aou //csis250.tripod.com /////////////////////////////////////////////////////////// //includes /////////////////////////////////////////////////////////// #include <iostream> using namespace std; class CInfo { private: int value; CInfo *next; friend class CSLL; friend ostream & operator << (ostream & bob, const CSLL & list); }; class CSLL { private: int n; CInfo *first; CInfo *last; void initialize(void); public: CSLL(void); //default constructor/////////////////////////////// bool insert(int x);///////////////////////////////////////////////// friend ostream & operator << (ostream & bob, const CSLL & list);///// CSLL(char ch); //constructs based on value of ch /////////////////// CSLL(int low, int high); CSLL(int low, int high, int n); CSLL(const CSLL &list); void input(void); bool insert(const CSLL & list2); bool operator == (const CSLL &list2); }; void main(void) { /* CSLL myList; cout << myList << endl; myList.insert(666); cout << myList << endl; myList.insert(555); cout << myList << endl; */ /* for (int i=1; i<=10; i++) { CSLL myList('r'); cout << myList << endl; } */ for (int i=1; i<=10; i++) { CSLL myList(10, 20); cout << myList << endl; } } CSLL::CSLL(int low, int high) { this->initialize(); if (low > high) return; int m = rand()%20; for (int i=1; i<=m; i++) { int x = low + rand()%(high-low+1); this->insert(x); } } CSLL::CSLL(char ch) { this->initialize(); if (ch == 'r') { int m = rand()%20; for (int i=1; i<=m; i++) { int x = rand()%100; this->insert(x); } } } bool CSLL::insert(int x) { /* set agent to available block of type CInfo copy x to value part of new block set next part of new block to NULL set first to agent set last to agent */ CInfo *agent; agent = new CInfo; agent->value = x; agent ->next = NULL; if (n == 0) { first = agent; last = agent; } else { last->next = agent; last = agent; } this->n++; return true; } ostream & operator << (ostream & bob, const CSLL & list) { bob << "SLL(" << list.n << "): "; CInfo *agent; agent = list.first; while (agent != NULL) { bob << agent->value << ' '; agent = agent->next; } return bob; } CSLL::CSLL(void) { this->initialize(); } void CSLL::initialize(void) { //n = 0; //(*this).n = 0; this->n = 0; this->first = NULL; this->last = NULL; } |