GenericList.cpp
From Giona
(Difference between revisions)
Line 29: | Line 29: | ||
BOOL GenericList::addNode(GenericListNode* node) | BOOL GenericList::addNode(GenericListNode* node) | ||
{ | { | ||
- | if(node==NULL) | + | if(node == NULL) |
{ | { | ||
this->lastError = GENERIC_LIST_ERR_NULL_NODE; | this->lastError = GENERIC_LIST_ERR_NULL_NODE; | ||
Line 35: | Line 35: | ||
} | } | ||
this->length++; | this->length++; | ||
- | if(this->head==NULL) | + | if(this->head == NULL) |
{ | { | ||
head=node; | head=node; | ||
Line 54: | Line 54: | ||
return this->head; | return this->head; | ||
} | } | ||
- | |||
/* | /* | ||
Line 62: | Line 61: | ||
BOOL GenericList::deleteNode(GenericListNode *node) | BOOL GenericList::deleteNode(GenericListNode *node) | ||
{ | { | ||
- | if(this->head==NULL) | + | if(this->head == NULL) |
{ | { | ||
this->lastError = GENERIC_LIST_ERR_EMPTY_LIST; | this->lastError = GENERIC_LIST_ERR_EMPTY_LIST; | ||
return FALSE; | return FALSE; | ||
} | } | ||
- | if(node==NULL) | + | if(node == NULL) |
{ | { | ||
this->lastError = GENERIC_LIST_ERR_NULL_NODE; | this->lastError = GENERIC_LIST_ERR_NULL_NODE; | ||
Line 80: | Line 79: | ||
return TRUE; | return TRUE; | ||
} | } | ||
- | while((tmp->getNext()!=NULL) | + | while((tmp->getNext() !=NULL) AND (tmp->getNext() != node)) |
- | tmp=tmp->getNext(); | + | tmp = tmp->getNext(); |
- | if(tmp->getNext()==NULL) | + | if(tmp->getNext() == NULL) |
{ | { | ||
this->lastError = GENERIC_LIST_ERR_NODE_NOT_FOUND; | this->lastError = GENERIC_LIST_ERR_NODE_NOT_FOUND; | ||
return FALSE; | return FALSE; | ||
} | } | ||
- | GenericListNode* delNode=tmp->getNext(); | + | GenericListNode* delNode = tmp->getNext(); |
tmp->setNext(delNode->getNext()); | tmp->setNext(delNode->getNext()); | ||
delete(delNode); | delete(delNode); | ||
Line 119: | Line 118: | ||
{ | { | ||
GenericListNode* tmpNext=tmp->getNext(); | GenericListNode* tmpNext=tmp->getNext(); | ||
- | delete (tmp); | + | delete(tmp); |
- | tmp=tmpNext; | + | tmp = tmpNext; |
} | } | ||
} | } | ||
- | |||
</pre> | </pre> | ||
[[Category:Codice Sorgente]] | [[Category:Codice Sorgente]] |
Revision as of 09:37, 6 July 2007
#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) AND (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; } }