Last active
January 5, 2017 22:57
-
-
Save elektrowolle/8453b90743eb75ce3b23 to your computer and use it in GitHub Desktop.
Simple Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename Type> class List { | |
public: | |
Type value; | |
List* previousItem; | |
List* nextItem; | |
List ( Type value, List *previous = NULL ) { | |
this->set (value); | |
this->previousItem = NULL; | |
this->nextItem = NULL; | |
} | |
~List ( ) { | |
if(nextItem != NULL) | |
delete nextItem; | |
} | |
List* add ( Type value ) { | |
if (next() == NULL) { | |
return this->nextItem = new List ( value, this ); | |
}else { | |
return next()->add(value); | |
} | |
} | |
void remove ( ) { | |
this->previous->nextItem = this->nextItem; | |
this->next->previousItem = this->previousItem; | |
this->previousItem = NULL; | |
this->nextItem = NULL; | |
delete this; | |
} | |
List* next ( ) { | |
return nextItem; | |
} | |
List* previous ( ) { | |
return previousItem; | |
} | |
Type get ( ) { | |
return value; | |
} | |
void set (Type value) { | |
this->value = value; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment