#include "TIntKeyList.h" TIntKeyListNode::TIntKeyListNode() { next = NULL; data = NULL; key = UNDEFINED; } TIntKeyListNode::~TIntKeyListNode() { } void TIntKeyListNode::Dump() { if (next != NULL) { std::cout << key << " " << data << " -> " << next << std::endl; } else { std::cout << key << " " << data << " -> EOL" << std::endl; } } //---- newclass --------------------------------------------------------- TIntKeyList::TIntKeyList() { head = NULL; curr = NULL; } // Attention: Datapointermemory will not be released! TIntKeyList::~TIntKeyList() { while(head != NULL) { curr = head; head = curr->next; delete curr; } } void *TIntKeyList::Next() { if (curr->next != NULL) { curr = curr->next; return(curr->data); } else { return(NULL); } } void *TIntKeyList::Get() { if (curr != NULL) { return(curr->data); } else { return(NULL); } } void *TIntKeyList::Get(int key) { if (key < 0) { std::cerr << "TIntKeyList::get -> negative key encountered (" <key > key) { curr = head; }; // if keynode is in list it must be before curr while(curr->key < key && curr->next != NULL) { curr = curr->next; } if (curr->key == key) { // was the key found? return(curr->data); } else { return(NULL); } } } // insert the given data pointer at position defined by key, curr is not effected void TIntKeyList::Ins(int key,void *data) { if (key < 0) { std::cerr << "TIntKeyList::ins -> negative key encountered (" << key << ")" << std::endl; return; }; TIntKeyListNode *newnode = new TIntKeyListNode(); newnode->key = key; newnode->data = data; if (head == NULL) { // list is empty? head = newnode; curr = newnode; newnode->next = NULL; } else { // find predecessor if (head->key > key) { // newnode will become new listhead? newnode->next = head; head = newnode; } else { TIntKeyListNode *pred = head; while(pred->next != NULL && pred->next->key <= key) { pred = pred->next; } newnode->next = pred->next; pred->next = newnode; } } } // delete function is not very efficient because list is searched liniarily for entry void TIntKeyList::Del(int key) { if (head!=NULL) { // list not empty TIntKeyListNode *delnode; if (head->key = key) { delnode = head; if(curr == head) { // first element has to be removed curr = head->next; } head = head->next; } else { TIntKeyListNode *pred = head; while(pred->next != NULL && pred->next->key < key) { pred = pred->next; } if(pred->next == NULL) { // all keys smaller => key not found return; } else { if(pred->next->key==key) { delnode = pred->next; pred->next = delnode->next; if(curr==delnode) { curr = pred; } } else { // key not found return; } } } delete(delnode); return; } } void TIntKeyList::Reset() { curr = head; } int TIntKeyList::Empty() { return(head == NULL); } void TIntKeyList::Dump() { TIntKeyListNode *outnode; std::cout << "list dump: " << std::endl; if (head == NULL) { std::cout << "empty list" << std::endl; } for (outnode = head; outnode != NULL; outnode = outnode->next) { std::cout << outnode << " : "; outnode->Dump(); } }