dsa-practice/libs/dope/linkedlist.cpp

57 lines
1.2 KiB
C++

#include "linkedlist.hpp"
DopeLinkedList::DopeLinkedList(){
for(int i = 0; i < MAX_DOPE_NODES; ++i){
nodes[i].Reset();
}
head = &nodes[0];
}
DopeNode::DopeNode(int init_data){
data = init_data;
next = NULL;
state = NodeState::UnInitialized;
}
DopeNode::DopeNode(){
Reset();
}
void DopeNode::Reset(){
this->data = 0;
this->next = NULL;
this->state = NodeState::UnInitialized;
}
void DopeLinkedList::AppendData(int init_data){
DopeNode* new_node = head;
DopeNode* node = head;
// Find next free slot and allocate it for the new node
for(int i=0; i < MAX_DOPE_NODES; ++i){
if(nodes[i].GetState() == NodeState::UnInitialized){
new_node = &nodes[i];
new_node->SetNext(NULL);
new_node->SetState(NodeState::Initialized);
new_node->SetData(init_data);
break;
}
}
if(head == new_node){
// Just set first node, nothing else to do
return;
}
// Find last node in linked list
if ((head->GetNext() == NULL) && (head->GetState() == NodeState::Initialized)){
// Special case, only one node so far
head->SetNext(new_node);
} else {
while(node->GetNext() != NULL){
node = node->GetNext();
}
node->SetNext(new_node);
}
}