GenericList.cpp
From Giona
(Difference between revisions)
(3 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
- | + | ||
- | + | ||
- | + | /**************************************************************************** | |
+ | *This class represents a generic linked list. | ||
+ | * -------------------------------------------------------------------------- | ||
+ | *Author: Rosario Marino | ||
+ | ***************************************************************************/ | ||
+ | |||
#include "GenericList.h" | #include "GenericList.h" | ||
Line 10: | Line 15: | ||
GenericList::GenericList() | GenericList::GenericList() | ||
{ | { | ||
- | this->head = NULL; | + | Debug::debug("GenericList::GenericList()"); |
- | + | this->head = NULL; | |
- | + | this->length = 0; | |
+ | this->lastError = ERROR_NO; | ||
} | } | ||
- | + | ||
/* | /* | ||
* Destructor | * Destructor | ||
Line 20: | Line 26: | ||
GenericList::~GenericList() | GenericList::~GenericList() | ||
{ | { | ||
- | deleteList(); | + | Debug::debug("GenericList::~GenericList()"); |
+ | deleteList(); | ||
} | } | ||
/* | /* | ||
- | * Adds a node | + | * Adds a node at the end of the list |
* Returns FALSE in case of error, TRUE otherwise | * Returns FALSE in case of error, TRUE otherwise | ||
*/ | */ | ||
- | + | bool GenericList::addNode(GenericListNode* node) | |
{ | { | ||
- | if(node==NULL) | + | Debug::debug("bool GenericList::addNode(GenericListNode* node)"); |
- | + | if(node == NULL) | |
- | + | { | |
- | + | Debug::debugError("bool GenericList::addNode(GenericListNode* node)", "ERROR_NULL_NODE", "parameter: node"); | |
- | + | this->lastError = ERROR_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; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Adds a node on the head of the list | ||
+ | * Returns FALSE in case of error, TRUE otherwise | ||
+ | */ | ||
+ | bool GenericList::inHeadNode(GenericListNode* node) | ||
+ | { | ||
+ | Debug::debug("bool GenericList::inHeadNode(GenericListNode* node)"); | ||
+ | if(node == NULL) | ||
+ | { | ||
+ | Debug::debugError("bool GenericList::inHeadNode(GenericListNode* node)", "ERROR_NULL_NODE", "parameter: node"); | ||
+ | this->lastError = ERROR_NULL_NODE; | ||
+ | return false; | ||
+ | } | ||
+ | node->setNext(this->getHead()); | ||
+ | this->head = node; | ||
+ | this->length++; | ||
+ | return true; | ||
} | } | ||
Line 52: | Line 80: | ||
GenericListNode *GenericList::getHead() | GenericListNode *GenericList::getHead() | ||
{ | { | ||
- | return this->head; | + | Debug::debug("GenericListNode *GenericList::getHead()"); |
+ | return this->head; | ||
} | } | ||
+ | /* | ||
+ | * extracts and returns the head node of the list | ||
+ | */ | ||
+ | GenericListNode *GenericList::extractHead() | ||
+ | { | ||
+ | Debug::debug("GenericListNode *GenericList::extractHead()"); | ||
+ | if(this->head == NULL) | ||
+ | { | ||
+ | Debug::debugError("GenericListNode *GenericList::extractHead()", "ERROR_EMPTY_LIST", EMPTY_STRING); | ||
+ | this->lastError = ERROR_EMPTY_LIST; | ||
+ | return NULL; | ||
+ | } | ||
+ | GenericListNode* node = this->head; | ||
+ | this->head =node->getNext(); | ||
+ | this->length--; | ||
+ | return node; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * extracts the node at position idx and returns it | ||
+ | */ | ||
+ | GenericListNode* GenericList::extractNode(int idx) | ||
+ | { | ||
+ | Debug::debug("GenericListNode* GenericList::extractNode(int idx)"); | ||
+ | if((idx<0)||(idx>length-1)) | ||
+ | { | ||
+ | Debug::debugError("GenericListNode* GenericList::extractNode(int idx)", "ERROR_OUT_OF_RANGE_INDEX", "parameter: idx"); | ||
+ | this->lastError = ERROR_OUT_OF_RANGE_INDEX; | ||
+ | return NULL; | ||
+ | } | ||
+ | if(idx==0) | ||
+ | return(this->extractHead()); | ||
+ | GenericListNode * tmp=this->getHead(); | ||
+ | for(int i=1; i<idx; i++) | ||
+ | tmp=tmp->getNext(); | ||
+ | GenericListNode* node= tmp->getNext(); | ||
+ | tmp->setNext(node->getNext()); | ||
+ | this->length--; | ||
+ | return node; | ||
+ | } | ||
+ | |||
+ | |||
+ | /* | ||
+ | * returns the node at position idx without extracting it from the list | ||
+ | */ | ||
+ | GenericListNode* GenericList::viewNode(int idx) | ||
+ | { | ||
+ | Debug::debug("GenericListNode* GenericList::viewNode(int idx)"); | ||
+ | if((idx<0)||(idx>length-1)) | ||
+ | { | ||
+ | Debug::debugError("GenericListNode* GenericList::viewNode(int idx)", "ERROR_OUT_OF_RANGE_INDEX", "parameter: idx"); | ||
+ | this->lastError = ERROR_OUT_OF_RANGE_INDEX; | ||
+ | return NULL; | ||
+ | } | ||
+ | if(idx==0) | ||
+ | return(this->getHead()); | ||
+ | GenericListNode * tmp=this->getHead(); | ||
+ | for(int i=1; i<=idx; i++) | ||
+ | tmp=tmp->getNext(); | ||
+ | return tmp; | ||
+ | } | ||
/* | /* | ||
Line 60: | Line 150: | ||
* of error, TRUE otherwise | * of error, TRUE otherwise | ||
*/ | */ | ||
- | + | bool GenericList::deleteNode(GenericListNode *node) | |
{ | { | ||
- | if(this->head==NULL) | + | Debug::debug("bool GenericList::deleteNode(GenericListNode *node)"); |
- | + | if(this->head == NULL) | |
- | + | { | |
- | + | Debug::debugError("bool GenericList::deleteNode(GenericListNode *node)", "ERROR_EMPTY_LIST", EMPTY_STRING); | |
- | + | this->lastError = ERROR_EMPTY_LIST; | |
- | + | return false; | |
- | + | } | |
- | + | if(node == NULL) | |
- | + | { | |
- | + | Debug::debugError("bool GenericList::deleteNode(GenericListNode *node)", "ERROR_NULL_NODE", "parameter: node"); | |
- | + | this->lastError = ERROR_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) | |
- | + | { | |
- | + | Debug::debugError("bool GenericList::deleteNode(GenericListNode *node)", "ERROR_NODE_NOT_FOUND", "parameter: node"); | |
- | + | this->lastError = ERROR_NODE_NOT_FOUND; | |
- | + | return false; | |
- | + | } | |
- | + | GenericListNode* delNode = tmp->getNext(); | |
+ | tmp->setNext(delNode->getNext()); | ||
+ | delete(delNode); | ||
+ | this->length--; | ||
+ | return true; | ||
} | } | ||
Line 99: | Line 193: | ||
int GenericList::getLastError() | int GenericList::getLastError() | ||
{ | { | ||
- | return this->lastError; | + | Debug::debug("int GenericList::getLastError()"); |
+ | return this->lastError; | ||
} | } | ||
Line 107: | Line 202: | ||
int GenericList::getLength() | int GenericList::getLength() | ||
{ | { | ||
- | return this->length; | + | Debug::debug("int GenericList::getLength()"); |
+ | return this->length; | ||
} | } | ||
Line 115: | Line 211: | ||
void GenericList::deleteList() | void GenericList::deleteList() | ||
{ | { | ||
- | GenericListNode * tmp = this->head; | + | Debug::debug("void GenericList::deleteList()"); |
- | + | GenericListNode * tmp = this->head; | |
- | + | while(tmp) | |
- | + | { | |
- | + | GenericListNode* tmpNext=tmp->getNext(); | |
- | + | delete(tmp); | |
- | + | tmp = tmpNext; | |
+ | } | ||
} | } | ||
- | |||
</pre> | </pre> | ||
- | + | [[Il database Object Oriented Generico|BACK]] | |
[[Category:Codice Sorgente]] | [[Category:Codice Sorgente]] |
Current revision as of 17:40, 17 December 2007
/**************************************************************************** *This class represents a generic linked list. * -------------------------------------------------------------------------- *Author: Rosario Marino ***************************************************************************/ #include "GenericList.h" /* * Constructor */ GenericList::GenericList() { Debug::debug("GenericList::GenericList()"); this->head = NULL; this->length = 0; this->lastError = ERROR_NO; } /* * Destructor */ GenericList::~GenericList() { Debug::debug("GenericList::~GenericList()"); deleteList(); } /* * Adds a node at the end of the list * Returns FALSE in case of error, TRUE otherwise */ bool GenericList::addNode(GenericListNode* node) { Debug::debug("bool GenericList::addNode(GenericListNode* node)"); if(node == NULL) { Debug::debugError("bool GenericList::addNode(GenericListNode* node)", "ERROR_NULL_NODE", "parameter: node"); this->lastError = ERROR_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; } /* * Adds a node on the head of the list * Returns FALSE in case of error, TRUE otherwise */ bool GenericList::inHeadNode(GenericListNode* node) { Debug::debug("bool GenericList::inHeadNode(GenericListNode* node)"); if(node == NULL) { Debug::debugError("bool GenericList::inHeadNode(GenericListNode* node)", "ERROR_NULL_NODE", "parameter: node"); this->lastError = ERROR_NULL_NODE; return false; } node->setNext(this->getHead()); this->head = node; this->length++; return true; } /* * returns the head node of the list */ GenericListNode *GenericList::getHead() { Debug::debug("GenericListNode *GenericList::getHead()"); return this->head; } /* * extracts and returns the head node of the list */ GenericListNode *GenericList::extractHead() { Debug::debug("GenericListNode *GenericList::extractHead()"); if(this->head == NULL) { Debug::debugError("GenericListNode *GenericList::extractHead()", "ERROR_EMPTY_LIST", EMPTY_STRING); this->lastError = ERROR_EMPTY_LIST; return NULL; } GenericListNode* node = this->head; this->head =node->getNext(); this->length--; return node; } /* * extracts the node at position idx and returns it */ GenericListNode* GenericList::extractNode(int idx) { Debug::debug("GenericListNode* GenericList::extractNode(int idx)"); if((idx<0)||(idx>length-1)) { Debug::debugError("GenericListNode* GenericList::extractNode(int idx)", "ERROR_OUT_OF_RANGE_INDEX", "parameter: idx"); this->lastError = ERROR_OUT_OF_RANGE_INDEX; return NULL; } if(idx==0) return(this->extractHead()); GenericListNode * tmp=this->getHead(); for(int i=1; i<idx; i++) tmp=tmp->getNext(); GenericListNode* node= tmp->getNext(); tmp->setNext(node->getNext()); this->length--; return node; } /* * returns the node at position idx without extracting it from the list */ GenericListNode* GenericList::viewNode(int idx) { Debug::debug("GenericListNode* GenericList::viewNode(int idx)"); if((idx<0)||(idx>length-1)) { Debug::debugError("GenericListNode* GenericList::viewNode(int idx)", "ERROR_OUT_OF_RANGE_INDEX", "parameter: idx"); this->lastError = ERROR_OUT_OF_RANGE_INDEX; return NULL; } if(idx==0) return(this->getHead()); GenericListNode * tmp=this->getHead(); for(int i=1; i<=idx; i++) tmp=tmp->getNext(); return tmp; } /* * Deletes the node from the list. Returns FALSE in case * of error, TRUE otherwise */ bool GenericList::deleteNode(GenericListNode *node) { Debug::debug("bool GenericList::deleteNode(GenericListNode *node)"); if(this->head == NULL) { Debug::debugError("bool GenericList::deleteNode(GenericListNode *node)", "ERROR_EMPTY_LIST", EMPTY_STRING); this->lastError = ERROR_EMPTY_LIST; return false; } if(node == NULL) { Debug::debugError("bool GenericList::deleteNode(GenericListNode *node)", "ERROR_NULL_NODE", "parameter: node"); this->lastError = ERROR_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) { Debug::debugError("bool GenericList::deleteNode(GenericListNode *node)", "ERROR_NODE_NOT_FOUND", "parameter: node"); this->lastError = ERROR_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() { Debug::debug("int GenericList::getLastError()"); return this->lastError; } /* * Returns the number of list nodes */ int GenericList::getLength() { Debug::debug("int GenericList::getLength()"); return this->length; } /* * (Private) deletes all list nodes */ void GenericList::deleteList() { Debug::debug("void GenericList::deleteList()"); GenericListNode * tmp = this->head; while(tmp) { GenericListNode* tmpNext=tmp->getNext(); delete(tmp); tmp = tmpNext; } }