Created
November 23, 2014 21:02
-
-
Save Ratstail91/87b9f2ff5d023742e734 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* Copyright: (c) Kayne Ruse 2014 | |
* | |
* This software is provided 'as-is', without any express or implied | |
* warranty. In no event will the authors be held liable for any damages | |
* arising from the use of this software. | |
* | |
* Permission is granted to anyone to use this software for any purpose, | |
* including commercial applications, and to alter it and redistribute it | |
* freely, subject to the following restrictions: | |
* | |
* 1. The origin of this software must not be misrepresented; you must not | |
* claim that you wrote the original software. If you use this software | |
* in a product, an acknowledgment in the product documentation would be | |
* appreciated but is not required. | |
* | |
* 2. Altered source versions must be plainly marked as such, and must not be | |
* misrepresented as being the original software. | |
* | |
* 3. This notice may not be removed or altered from any source | |
* distribution. | |
*/ | |
#ifndef LIST_HPP_ | |
#define LIST_HPP_ | |
template<typename T> | |
class Node { | |
public: | |
Node() = default; | |
~Node() = default; | |
Node<T>* SetNext(Node<T>* ptr) { return next = ptr; } | |
Node<T>* GetNext() { return next; } | |
T& operator*() { return element; } | |
private: | |
Node<T>* next = nullptr; | |
T element; | |
}; | |
template<typename T> | |
class List { | |
public: | |
List() = default; | |
~List() = default; | |
Node<T>* Push(Node<T>* slab) { | |
//recieve any number of cards | |
if (!slab) return nullptr; | |
Node<T>* tmpHead = head; | |
head = slab; | |
while (slab->GetNext()) slab = slab->GetNext(); | |
slab->SetNext(tmpHead); | |
} | |
Node<T>* Peek(int index = 0) { | |
Node<T>* it = head; | |
while (it && index--) it = it->GetNext(); | |
return it; | |
} | |
Node<T>* Pop(int index = 0, int count = 1) { | |
//nothing to return | |
if (!head) return nullptr; | |
if (index == 0) { | |
//returning the first card | |
Node<T>* ret = head, *tail = head; | |
while(tail->GetNext() && --count) tail = tail->GetNext(); | |
head = tail->GetNext(); | |
tail->SetNext(nullptr); | |
return ret; | |
} | |
//locate the slab | |
Node<T>* prev = head; | |
while(prev && --index) prev = prev->GetNext(); | |
Node<T>* tail = prev; | |
while(tail && count--) tail = tail->GetNext(); | |
//prev or tail overruns the list | |
if (!prev) return nullptr; | |
if (!tail) { | |
Node<T>* ret = prev->GetNext(); | |
prev->SetNext(nullptr); | |
return ret; | |
} | |
//cut the slab out | |
Node<T>* ret = prev->GetNext(); | |
prev->SetNext(tail->GetNext()); | |
tail->SetNext(nullptr); | |
return ret; | |
} | |
int GetSize() { | |
int ret = 0; | |
for (Node<T>* ptr = head; ptr; ptr = ptr->GetNext()) ++ret; | |
return ret; | |
} | |
private: | |
Node<T>* head = nullptr; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment