Last active
August 29, 2015 13:56
-
-
Save svenoaks/9037852 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
using namespace std; | |
template <class E> | |
class LinkedList | |
{ | |
struct Node; | |
Node* first; | |
Node* last; | |
public: | |
struct Node | |
{ | |
Node* prev; | |
Node* next; | |
E data; | |
}; | |
class iterator | |
{ | |
Node* thiz; | |
public: | |
iterator(Node* ptr) | |
{ | |
thiz = ptr; | |
} | |
E& operator*() | |
{ | |
return thiz->data; | |
} | |
iterator operator++() | |
{ | |
thiz = thiz->next; | |
return *this; | |
} | |
iterator operator--() | |
{ | |
thiz = thiz->prev; | |
return *this; | |
} | |
bool operator!=(const iterator& other) const | |
{ | |
return thiz != other.thiz; | |
} | |
bool operator==(const iterator& other) const | |
{ | |
return thiz == other.thiz; | |
} | |
}; | |
E& front() | |
{ | |
return (*first).data; | |
} | |
E& back() | |
{ | |
return last->prev->data; | |
} | |
iterator begin() | |
{ | |
return iterator{ first }; | |
} | |
iterator end() | |
{ | |
return iterator{ last }; | |
} | |
void reverse() | |
{ | |
Node* current = first; | |
while (current != last) | |
{ | |
Node* temp = current->next; | |
swap(current->prev, current->next); | |
current = temp; | |
} | |
Node* new_first = last->prev; | |
first->next = last; | |
last->prev = first; | |
first = new_first; | |
} | |
void push_back(const E& val) | |
{ | |
Node* new_last = new Node(); | |
last->data = val; | |
last->next = new_last; | |
new_last->prev = last; | |
new_last->next = NULL; | |
last = new_last; | |
} | |
void pop_back() | |
{ | |
Node* new_last = last->prev; | |
delete last; | |
last = new_last; | |
last->next = NULL; | |
} | |
void pop_front() | |
{ | |
Node* new_first = first->next; | |
delete first; | |
first = new_first; | |
first->prev = NULL; | |
} | |
LinkedList() | |
{ | |
first = new Node(); | |
first->prev = NULL; | |
first->next = NULL; | |
last = first; | |
} | |
~LinkedList() | |
{ | |
Node* toDelete = first; | |
Node* next; | |
do | |
{ | |
next = toDelete->next; | |
delete toDelete; | |
toDelete = next; | |
} while (toDelete); | |
} | |
}; | |
int main() | |
{ | |
LinkedList<int> t{}; | |
t.push_back(2); | |
t.push_back(5); | |
t.push_back(9); | |
t.push_back(11); | |
t.push_back(25); | |
t.push_back(49); | |
for (auto& i : t) | |
{ | |
cout << i << endl; | |
} | |
cout << endl; | |
t.reverse(); | |
for (auto& i : t) | |
{ | |
cout << i << endl; | |
} | |
cout << endl; | |
LinkedList<int>::iterator it = t.begin(); | |
for (; it != t.end(); ++it) | |
{ | |
cout << *it << endl; | |
} | |
--it; | |
cout << endl; | |
for (;; --it) | |
{ | |
cout << *it << endl; | |
if (it == t.begin()) break; | |
} | |
cout << endl; | |
t.pop_back(); | |
t.pop_back(); | |
t.pop_front(); | |
t.pop_front(); | |
t.push_back(11); | |
t.push_back(13); | |
for (auto& i : t) | |
{ | |
cout << i << endl; | |
} | |
cout << endl; | |
t.back() -= t.front(); | |
cout << t.back(); | |
cout << endl; | |
system("pause"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment