// insert element
const bool linkedlist::insert (const int value)
{
node* newnode = NULL;
node* lastnode = NULL;
newnode = new node; // alloc new node
if ( !newnode ) return false; // if new fails
newnode->value = value; // asign value
newnode->next = NULL; // init next element
if ( this->elements == 0) // there is no nodes
this->firstnode = newnode; // assign first node
else
{
lastnode = this->firstnode;
// goto last node
while (lastnode->next)
lastnode = lastnode->next;
lastnode->next = newnode; // set new node as next node
}
this->elements++; // update node count
return true;
}
// destructor
linkedlist::~linkedlist()
{
node* currentnode = NULL;
node* nextnode = NULL;
if (this->elements > 0) // list has nodes
{
currentnode = this->firstnode; // get first node
while ( currentnode ) // while has nodes
{
nextnode = currentnode->next; // get next node
delete currentnode;
currentnode = NULL; // delete node
currentnode = nextnode; // update current node
}
this->firstnode = NULL;
//this->elements = NULL; ----> innecesario
}
return;
}