From Giona
#include <stdlib.h>
#include "bool.h"
#include "GenericListNode.h"
#include "GenericList.h"
/*
* Constructor
*/
GenericList::GenericList()
{
this->head = NULL;
this->length = 0;
this->lastError = GENERIC_LIST_ERR_NO;
}
/*
* Destructor
*/
GenericList::~GenericList()
{
deleteList();
}
/*
* Adds a node in the list
* Returns FALSE in case of error, TRUE otherwise
*/
BOOL GenericList::addNode(GenericListNode* node)
{
if(node==NULL)
{
this->lastError = GENERIC_LIST_ERR_NULL_NODE;
return FALSE;
}
this->length++;
if(this->head==NULL)
{
head=node;
return TRUE;
}
GenericListNode *tmp = this->head;
while(tmp->getNext() != NULL)
tmp = tmp->getNext();
tmp->setNext(node);
return TRUE;
}
/*
* returns the head node of the list
*/
GenericListNode *GenericList::getHead()
{
return this->head;
}
/*
* Deletes the node from the list. Returns FALSE in case
* of error, TRUE otherwise
*/
BOOL GenericList::deleteNode(GenericListNode *node)
{
if(this->head==NULL)
{
this->lastError = GENERIC_LIST_ERR_EMPTY_LIST;
return FALSE;
}
if(node==NULL)
{
this->lastError = GENERIC_LIST_ERR_NULL_NODE;
return FALSE;
}
GenericListNode *tmp = this->head;
if (tmp == node)
{
this->head = tmp->getNext();
delete(tmp);
this->length --;
return TRUE;
}
while((tmp->getNext()!=NULL)&&(tmp->getNext()!=node))
tmp=tmp->getNext();
if(tmp->getNext()==NULL)
{
this->lastError = GENERIC_LIST_ERR_NODE_NOT_FOUND;
return FALSE;
}
GenericListNode* delNode=tmp->getNext();
tmp->setNext(delNode->getNext());
delete(delNode);
this->length--;
return TRUE;
}
/*
* Returns last error code
*/
int GenericList::getLastError()
{
return this->lastError;
}
/*
* Returns the number of list nodes
*/
int GenericList::getLength()
{
return this->length;
}
/*
* (Private) deletes all list nodes
*/
void GenericList::deleteList()
{
GenericListNode * tmp = this->head;
while(tmp)
{
GenericListNode* tmpNext=tmp->getNext();
delete (tmp);
tmp=tmpNext;
}
}