Need help with C++ linked list using objects
Here is what I have so far. The spec sheet the professor handed out was very strict. It only specifies what the member functions and classes will contain, no main() or program function info. I am having a hard time figuring out how to get the add member function to work. It receives a pointer to a node but how do I get the value passed to set in the data attribute? I assume it would be in the main() call of L1.add(int);
A little confused need help with this and I'm sure with a few more things, only have a few days left before the semester is over :eek: . THANKS!
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)
{
}
listController::~listController()
{
}
void listController::add(node *newNodePtr)
{
class node *currNodePtr = NULL;
newNodePtr = new node();
if(head == NULL)//no nodes
{
head = newNodePtr;
tail = newNodePtr;
}
else
{
tail->setNext(newNodePtr);
tail = newNodePtr;
}
}
int listController::getCount()
{
return count;
}
void listController::printList()
{
class node *currNodePtr = NULL;
currNodePtr = head;
while(currNodePtr->getNext() != NULL)
{
cout<<currNodePtr->getData()<<endl;
currNodePtr->setNext(currNodePtr);
}
}
node * listController::deleteNode(int value)
{
class node *currNodePtr = NULL;
class 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)
{
class node *currNodePtr = NULL;
class node *prevNodePtr = NULL;
class 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()
{
class node *currNodePtr = NULL;
class 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)
{
class 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******************//
int main()
{
listController L1;
system("PAUSE");
return(0);
}
//*****************END OF MAIN*****************//