OK. I edited this post. I got the code to run and it works. I have a question about the Copy Constructor and Destructor. If I pull them out of the program it still works fine. Why??? I didn't think it would work or at least the links would be messed up. Any thoughts? Thanks again.
Code:
#include <iostream>
#include <string>
using namespace std;
//******************CLASS NODE*****************//
class node
{
private:
int data;
node *next;
public:
node(int);
node *getNext();
void setNext(node *);
void setData(int);
int getData();
};
node::node(int value)
{
data = value;
next = '\0';
}
node * node::getNext()
{
return next;
}
void node::setNext(node *newNodePtr)
{
next = newNodePtr;
}
void node::setData(int value)
{
data = value;
}
int node::getData()
{
return data;
}
//***********END OF CLASS NODE*****************//
//************CLASS LISTCONTROLLER*************//
class listController
{
private:
int count;
node *head;
node *tail;
public:
listController();
listController(const listController &);
~listController();
void add(node *);
int getCount();
void printList();
node *deleteNode(int);
int deleteAllSpecific(int);
int deleteAll();
node *searchList(int);
int isEmpty();
};
listController::listController()
{
count = 0;
head = '\0';
tail = '\0';
}
listController::listController(const listController &Copy)
{
node *origCurrNodePtr = NULL; //pointer to original linked list
node *copyCurrNodePtr = NULL; //pointer to copied linked list
origCurrNodePtr = Copy.head; //set orig pointer to head
head = new node(*(Copy.head)); //create first copy node w/head pointing to it
copyCurrNodePtr = head; //set copy pointer to head
while(origCurrNodePtr->getNext() != NULL) //loop through orig link and make a copy
{
origCurrNodePtr = origCurrNodePtr->getNext(); //advance orig pointer
copyCurrNodePtr->setNext(new node(origCurrNodePtr->getData())); //make new node to copy ptr
copyCurrNodePtr = copyCurrNodePtr->getNext(); //advance copy ptr
}
tail = copyCurrNodePtr; //set tail to last node
count = Copy.count; //copy count
}
listController::~listController()
{
node *currNodePtr = NULL;
node *tempNodePtr = NULL;
currNodePtr = head;
while(currNodePtr != NULL)
{
tempNodePtr = currNodePtr;
currNodePtr = currNodePtr->getNext();
head = currNodePtr;
if(currNodePtr == NULL)
{
tail = head;
}
delete(tempNodePtr);
}
}
void listController::add(node *newNodePtr)
{
node *currNodePtr = NULL;
node *prevNodePtr = NULL;
currNodePtr = head;
prevNodePtr = head;
if(head == NULL) //no nodes (first node to add)
{
head = newNodePtr;
tail = newNodePtr;
}
else
{
while(currNodePtr != NULL && currNodePtr->getData() >= newNodePtr->getData())
{
prevNodePtr = currNodePtr;
currNodePtr = currNodePtr->getNext();
}
if(currNodePtr == NULL) //add to rear
{
prevNodePtr->setNext(newNodePtr);
tail = newNodePtr;
}
else if(currNodePtr == head) //add to front
{
newNodePtr->setNext(currNodePtr);
head = newNodePtr;
}
else //add in middle
{
prevNodePtr->setNext(newNodePtr);
newNodePtr->setNext(currNodePtr);
}
}
count++;
}
int listController::getCount()
{
return count;
}
void listController::printList()
{
node *currNodePtr = NULL;
currNodePtr = head;
while(currNodePtr != NULL)
{
cout<<currNodePtr->getData()<<endl;
currNodePtr = currNodePtr->getNext();
}
}
node * listController::deleteNode(int value)
{
node *currNodePtr = NULL;
node *prevNodePtr = NULL;
currNodePtr = head;
prevNodePtr = head;
//loop until value is found
while(currNodePtr != NULL && currNodePtr->getData() != value)
{
prevNodePtr = currNodePtr;
currNodePtr = currNodePtr->getNext();
}
if(currNodePtr->getData() == value)
{
if(currNodePtr == head) //if value found is first node
{
head = currNodePtr->getNext();
}
else if(currNodePtr == tail) //if value found is last node
{
tail = prevNodePtr;
prevNodePtr->setNext(NULL);
}
else //if value found is somewhere in between
{
prevNodePtr->setNext(currNodePtr->getNext());
}
count--;
}
return currNodePtr;
}
int listController::deleteAllSpecific(int value)
{
node *currNodePtr = NULL;
node *prevNodePtr = NULL;
node *tempNodePtr = NULL;
int nodesDeleted = 0;
currNodePtr = head;
prevNodePtr = head;
//loop until null
while(currNodePtr != NULL)
{
if(currNodePtr->getData() == value)
{
if(currNodePtr == head) //if value found is first node
{
head = currNodePtr->getNext();
prevNodePtr = currNodePtr->getNext();
}
else if(currNodePtr == tail) //if value found is last node
{
tail = prevNodePtr;
prevNodePtr->setNext(NULL);
}
else //if value found is somewhere in between
{
prevNodePtr->setNext(currNodePtr->getNext());
}
nodesDeleted++;
count--;
tempNodePtr = currNodePtr;
currNodePtr = currNodePtr->getNext();
delete(tempNodePtr); //free node memory
}
else //advance pointers
{
prevNodePtr = currNodePtr;
currNodePtr = currNodePtr->getNext();
}
}
return nodesDeleted;
}
int listController::deleteAll()
{
node *currNodePtr = NULL;
node *tempNodePtr = NULL;
int nodesDeleted = 0;
currNodePtr = head;
while(currNodePtr != NULL)
{
tempNodePtr = currNodePtr;
currNodePtr = currNodePtr->getNext();
head = currNodePtr;
if(currNodePtr == NULL)
{
tail = head;
}
nodesDeleted++;
count--;
delete(tempNodePtr);
}
return nodesDeleted;
}
node * listController::searchList(int value)
{
node *currNodePtr = NULL;
currNodePtr = head;
while(currNodePtr != NULL && currNodePtr->getData() != value)
{
currNodePtr = currNodePtr->getNext();
}
return currNodePtr;
}
int listController::isEmpty()
{
int flag = 1;
if(count > 0)
{
flag = 0;
}
return flag;
}
//*********END OF CLASS LISTCONTROLLER*********//
//******************MAIN FUNCTIONS**********************//
node *makeNode(int value)
{
node *newNodePtr = '\0';
newNodePtr = new node(value);
return newNodePtr;
}
//*************END OF MAIN FUNCTIONS********************//
//***********************MAIN******************//
int main()
{
int number = 0;
int nodes = 0;
node *result;
char selection;
char inputArray[256];
char numberArray[256];
listController L1;
do
{
cout<<endl<<
"-->[A] or [a] will add a node"<<endl<<
"-->[D] or [d] will delete a specific node"<<endl<<
"-->[E] or [e] will exit the program"<<endl<<
"-->[L] or [l] will search for a specific node"<<endl<<
"-->[M] or [m] will delete all nodes in list"<<endl<<
"-->[P] or [p] will print the list"<<endl<<
"-->[S] or [s] will delete all nodes with same value"<<endl<<endl;
cout<<"Enter a command---> ";
fgets(inputArray,256,stdin);
selection = inputArray[0];
number = atoi(&inputArray[1]);
switch(toupper(selection))
{
case 'A': //add a node
L1.add(makeNode(number));
cout<<endl<<"Node Count: "<<L1.getCount()<<endl<<endl;
break;
case 'D': //delete a specific node
if(L1.getCount() == 0)
{
cout<<endl<<"There are no nodes left to delete"<<endl<<endl;
}
else
{
result = L1.deleteNode(number);
cout<<endl<<"Node Count: "<<L1.getCount()<<endl<<endl;
cout<<"Return: "<<result<<endl;
}
break;
case 'E': //will exit the program
printf("\n\n\n");
break;
case 'L': //will search for a specific node
if(L1.getCount() == 0)
{
cout<<endl<<"There are no nodes left to search"<<endl<<endl;
}
else
{
result = L1.searchList(number);
cout<<endl<<"Node Count: "<<L1.getCount()<<endl<<endl;
cout<<"Return: "<<result<<endl;
}
break;
case 'M': //delete all nodes in list
if(L1.getCount() == 0)
{
cout<<endl<<"There are no nodes left to delete"<<endl<<endl;
}
else
{
nodes = L1.deleteAll();
cout<<endl<<"Node Count: "<<L1.getCount()<<endl<<endl;
cout<<"Nodes deleted: "<<nodes<<endl;
}
break;
case 'P': //will print the list
if(L1.getCount() == 0)
{
cout<<endl<<"There are no nodes to print"<<endl<<endl;
}
else
{
L1.printList();
cout<<endl<<"Node Count: "<<L1.getCount()<<endl<<endl;
}
break;
case 'S': //delete all nodes with same value
if(L1.getCount() == 0)
{
cout<<endl<<"There are no nodes left to delete"<<endl<<endl;
}
else
{
nodes = L1.deleteAllSpecific(number);
cout<<endl<<"Node Count: "<<L1.getCount()<<endl<<endl;
cout<<"Return: "<<nodes<<endl;
}
break;
default:
printf("\n\n*****You have entered an invalid command*****\n\n\n");
}
}while(selection != 'e' && selection != 'E');
system("PAUSE");
return 0;
}
//*****************END OF MAIN*****************//