Skip to content

Instantly share code, notes, and snippets.

@elektrowolle
Last active January 5, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elektrowolle/8453b90743eb75ce3b23 to your computer and use it in GitHub Desktop.
Save elektrowolle/8453b90743eb75ce3b23 to your computer and use it in GitHub Desktop.
Simple Linked List
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