//csll.cpp
//CSLL Singly linked list class project
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
const int MAX_LEN = 10;
const int MAX_SIZE = 10; const char *testData[] =
{"Bob", "Tom", "Ann", "Ron", "Ben", "Ken", "Rob", "Sam", "Don", "Dan"}; const int MAX_DATA = sizeof(testData)/sizeof(testData[0]); struct NodeType
{
char name[MAX_LEN+1];
NodeType *next;
}; class CSLL
{
private:
NodeType *head;
int count;
public:
CSLL(void);
CSLL(char ch);
CSLL(int n);
CSLL(const CSLL &list);
~CSLL(void);
CSLL &operator = (const CSLL &list);
void display(void) const;
void displayDistinct(void) const;
void displayPhysical(void) const;
void insertAtHead(const char s[]);
void insertAtTail(const char s[]);
void removeAll(void);
bool has(const char[]);
}; void testConstructorIntN(void);
void testConstructorCharD(void);
void testConstructorCharR(void);
void testConstructorDefault(void);
void testConstructorCopy(void);
void testDestructor(void);
void testDisplayDistinct(void);
void testDisplayPhysical(void);
void testHas(void);
void testInsertAtHead(void);
void testInsertAtTail(void);
void testOperatorAssign(void);
void testRemoveAll(void); void main(void)
{
char ch;
srand(time(NULL)); do
{
cout << "TEST CENTER\n";
cout << "===========\n";
cout << "1 constructorDefault\n";
cout << "2 constructorCharR\n";
cout << "3 constructorIntN\n";
cout << "4 constructorCopy\n";
cout << "5 constructorCharD\n";
cout << "6 destructor\n";
cout << "7 insertAtHead(name)\n";
cout << "8 insertAtTail(name)\n";
cout << "9 displayPhysical()\n";
cout << "a removeAll()\n";
cout << "b operator = \n";
cout << "c displayDistinct()\n";
cout << "d has(name)\n";
cout << "Enter your choice (0 to quit): ";
cin >> ch;
cout << ch << endl; switch (ch)
{
case '1': testConstructorDefault(); break;
case '2': testConstructorCharR(); break;
case '3': testConstructorIntN(); break;
case '4': testConstructorCopy(); break;
case '5': testConstructorCharD(); break;
case '6': testDestructor(); break;
case '7': testInsertAtHead(); break;
case '8': testInsertAtTail(); break;
case '9': testDisplayPhysical(); break;
case 'a': testRemoveAll(); break;
case 'b': testOperatorAssign(); break;
case 'c': testDisplayDistinct(); break;
case 'd': testHas(); break;
default:;
}
}
while (ch != '0'); } CSLL::CSLL(int n)
{
head = NULL;
count = 0;
while (n>0)
{
insertAtHead(testData[rand()%MAX_DATA]);
n--;
}
} void testConstructorIntN(void)
{
cout << "Testing CSLL myList(n);\n";
cout << "-----------------------\n";
char ch;
do
{
int n = rand()%(MAX_SIZE+1);
CSLL myList(n);
myList.display();
cin >> ch;
}
while (ch != '0');
} CSLL::CSLL(char ch)
{
head = NULL;
count = 0;
if ('r' == ch)
{
int m = rand()%MAX_SIZE;
while (m>0)
{
insertAtHead(testData[rand()%MAX_DATA]);
m--;
}
} if ('d' == ch)
{
char tName[MAX_LEN+1];
int m = rand()%MAX_SIZE;
while (m>0)
{
strcpy(tName, testData[rand()%MAX_DATA]);
if (!has(tName)) // if (!this->has(tName))
insertAtHead(tName);
m--;
}
}
} void testConstructorCharD(void)
{
cout << "Testing CSLL myList('d');\n";
cout << "-------------------------\n";
char ch;
do
{
CSLL myList('d');
myList.display();
cin >> ch;
}
while (ch != '0');
} void testConstructorCharR(void)
{
cout << "Testing CSLL myList('r');\n";
cout << "-------------------------\n";
char ch;
do
{
CSLL myList('r');
myList.display();
cin >> ch;
}
while (ch != '0');
} CSLL::CSLL(void)
{
head = NULL;
count = 0;
} void testConstructorDefault(void)
{
cout << "Testing CSLL myList;\n";
cout << "--------------------\n";
char ch;
do
{
CSLL myList;
myList.display();
cin >> ch;
}
while (ch != '0');
} CSLL::CSLL(const CSLL &list)
{
count = 0;
head = NULL; NodeType *p;
p = list.head;
while (p != NULL)
{
insertAtTail(p->name);
p = p->next;
}
} void testConstructorCopy(void)
{
cout << "Testing CSLL myList(yourList);\n";
cout << "------------------------------\n";
char ch;
do
{
CSLL myList('r');
myList.display();
CSLL yourList(myList);
yourList.display();
cin >> ch;
}
while (ch != '0');
} CSLL::~CSLL(void)
{
removeAll(); // this->removeAll();
} void testDestructor(void)
{
cout << "Testing destructor\n";
cout << "------------------\n";
char ch;
do
{
CSLL *myListP;
myListP = new CSLL('r');
myListP->display();
delete myListP;
cin >> ch;
}
while (ch != '0');
} void CSLL::display(void) const
{
NodeType *ptr; cout << "List[" << count << "]: "; ptr = head;
while (ptr != NULL)
{
cout << ptr->name << ' ';
ptr = ptr->next;
} cout << endl;
} void CSLL::displayDistinct(void) const
{
cout << "List distinct: "; if (0 == count)
{
cout << endl;
return;
} NodeType *p, *q;
bool visited; p = head;
cout << p->name << ' ';
p = p->next; while (p != NULL)
{
q = head;
visited = false;
while (q != p)
{
if (strcmp(p->name, q->name) == 0)
visited = true;
q = q->next;
} if (!visited)
cout << p->name << ' '; p = p->next;
} cout << endl;
} void testDisplayDistinct(void)
{
cout << "Testing displayDistinct()\n";
cout << "-------------------------\n";
char ch;
do
{
int n = rand()%(MAX_SIZE+1);
CSLL myList(n);
myList.display();
myList.displayDistinct();
cin >> ch;
}
while (ch != '0');
} void CSLL::displayPhysical(void) const
{
NodeType *ptr; cout << "List[" << count << "]:\n";
cout << "address name\n";
cout << "---------- ----\n";
ptr = head;
while (ptr != NULL)
{
cout << ptr << ' ' << ptr->name << endl;
ptr = ptr->next;
}
cout << endl;
} void testDisplayPhysical(void)
{
cout << "Testing displayPhysical()\n";
cout << "-------------------------\n";
char ch;
do
{
int n = rand()%(MAX_SIZE+1);
CSLL myList(n);
myList.display();
myList.displayPhysical();
cin >> ch;
}
while (ch != '0');
} bool CSLL::has(const char s[])
{
NodeType *p;
p = head;
while (p != NULL)
{
if (strcmp(p->name, s)==0)
return true;
p = p->next;
} return false;
} void testHas(void)
{
cout << "Testing has(name)\n";
cout << "-----------------\n";
char ch;
do
{
CSLL myList('r');
myList.display();
char tName[MAX_LEN+1];
strcpy(tName, testData[rand()%MAX_DATA]);
if (myList.has(tName))
cout << tName << " is in the list\n";
else
cout << tName << " is NOT in the list\n";
cin >> ch;
}
while (ch != '0');
} void CSLL::insertAtHead(const char s[])
{
NodeType *p;
p = new NodeType;
if (NULL == p)
{
cout << "Out of memory\n";
return;
} strcpy(p->name, s);
p->next = head;
head = p;
count++;
} void testInsertAtHead(void)
{
cout << "Testing insertAtHead(name)\n";
cout << "--------------------------\n";
char ch;
do
{
CSLL myList('r');
myList.display();
char tName[MAX_LEN+1];
strcpy(tName, testData[rand()%MAX_DATA]);
myList.insertAtHead(tName);
cout << "After inserting " << tName << " at head\n";
myList.display();
cin >> ch;
}
while (ch != '0');
} void CSLL::insertAtTail(const char s[])
{
NodeType *p;
p = new NodeType;
strcpy(p->name, s);
p->next = NULL; if (NULL == head)
head = p;
else
{
NodeType *last;
last = head;
while (last->next != NULL)
last = last->next;
last->next = p;
} count++;
} void testInsertAtTail(void)
{
cout << "Testing insertAtTail(name)\n";
cout << "--------------------------\n";
char ch;
do
{
CSLL *myListP;
myListP = new CSLL('r');
myListP->display();
char tName[MAX_LEN+1];
strcpy(tName, testData[rand()%MAX_DATA]);
myListP->insertAtTail(tName);
cout << "After inserting " << tName << " at tail\n";
myListP->display();
delete myListP;
cin >> ch;
}
while (ch != '0');
} CSLL &CSLL::operator = (const CSLL &list)
{
if (this->count > 0)
removeAll(); NodeType *p;
p = list.head;
while (p != NULL)
{
insertAtTail(p->name);
p = p->next;
}
return *this;
} void testOperatorAssign(void)
{
cout << "Testing operator =\n";
cout << "------------------\n";
char ch;
do
{
int n = rand()%(MAX_SIZE+1);
CSLL myList(n);
myList.display();
CSLL yourList, hisList;
hisList = yourList = myList;
yourList.display();
hisList.display();
cin >> ch;
}
while (ch != '0');
} void CSLL::removeAll(void)
{
NodeType *p;
while (head != NULL)
{
p = head;
head = head->next;
delete p;
}
count = 0;
} void testRemoveAll(void)
{
cout << "Testing removeAll()\n";
cout << "-------------------\n";
char ch;
do
{
int n = rand()%(MAX_SIZE+1);
CSLL myList(n);
myList.display();
myList.removeAll();
myList.display();
cin >> ch;
}
while (ch != '0');
}
|