Is this good or bad? I'm thinking that it needs to be this way if you want to delete an arbitrary node, or am I totally lost? :>
Well, I would like to use this as a sort of baseclass, as Matsp suggested, in the future, using it for inheritance. Did do some checking and testing but I couldn't figure out how to redefine 'struct member' so that the inherited class' member struct contained some more usefull information, everytime I tried to insert data into the new member structs variables the program just segFaulted. Will have to do some more testing regarding this, or if you people have some pointers on this topic?
I did move everything into a .h file and I believe I've fixed this. Now it only comes into play when '#define DEBUG' is used.
Not sure I really understand what you are saying here. Either way, 'operator =' can't be used since it's declared private and the compiler just spits out an error message.
Ok, now I'm really confused ;p. If you could, please expand on that.
The destructor now calls 'wipeList()' directly, no matter if there is a list or not. The checking wether there's a list or not is handled by that function. However, both 'wipeList()' and later 'delStart()' calls 'listExists()' which adds, as you said earlier, redundency, but I'm not sure if this can be avoided if I later want to call 'delStart()' or 'delEnd()' without calling 'wipeList()'.
Clever, that never crossed my mind, cheers :>.
I managed to fix those functions, I think, not as you suggested though, but after some testing they both seem to work fine.
I think I've managed to solve that now, rewrote 'delLink()' and 'addLink()', have a look.
See above.
Added a 'listExists()' check, should work now.
Feels like I'm learning new stuff by the minute ^^.
ps. Updated the code in the first post to reflect the changes.
EDIT: Seems I can no longer update the original post for some reason >.<.
Here it is anyway:
Code:#ifndef LIST #define LIST #ifdef DEBUG #include <iostream> using namespace std; #endif class list { private: struct member { int ID; member *next; member *prev; }; int nextID; member *start; member *link; member *end; list operator = (list); public: list() { start = link = end = NULL; nextID = 0; } ~list() { wipeList(); } void addLink(); void delLink(int ID); void delStart(); void delEnd(); void wipeList(); bool nextLink(); bool prevLink(); bool seekLink(int ID); bool listExists(); #ifdef DEBUG void printLinkID(); #endif }; bool list::listExists() { return (start != NULL); } void list::wipeList() { while(listExists()) { delStart(); } } void list::delStart() { if(listExists()) { member *temp = start; if(start->next != NULL) { start = start->next; start->prev = NULL; } else { start = end = NULL; } delete temp; } } void list::delEnd() { if(listExists()) { member *temp = end; if(end->prev != NULL) { end = end->prev; end->next = NULL; } else { end = start = NULL; } delete temp; } } void list::delLink(int ID) { if(seekLink(ID)) { if(link->prev == NULL) { delStart(); } else if(link->next == NULL) { delEnd(); } else { member *temp = link; link->prev->next = link->next; link->next->prev = link->prev; link = link->next; delete temp; } } } void list::addLink() { member *newLink = new member; newLink->ID = nextID++; link = end; if(listExists()) { link->next = newLink; link->next->prev = link; end = link->next; } else { link = newLink; link->prev = link->next = NULL; start = end = link; } } bool list::seekLink(int ID) { link = start; if(listExists()) { do { if(link->ID == ID) { return true; } } while(nextLink()); } return false; } bool list::nextLink() { if(listExists()) { if(link->next != NULL) { link = link->next; return true; } return false; } } bool list::prevLink() { if(listExists()) { if(link->prev != NULL) { link = link->prev; return true; } return false; } } #ifdef DEBUG void list::printLinkID() { if(listExists()) { link = start; cout << "List Exists!" << endl; do { cout << link->ID << endl; } while(nextLink()); } else { cout << "List does not exist!" << endl; } } #endif #endif



LinkBack URL
About LinkBacks



