#include <iostream>
using namespace std;
template <typename T>
class Array {
unsigned int m_size;
T *arr;
public:
class iterator {
private:
T *ptr;
public:
iterator(T *ptr) {
this->ptr = ptr;
}
iterator operator++(int) {
ptr++;
return *this;
}
T operator*() {
return *ptr;
}
bool operator==(const iterator &it) {
return ptr == it.ptr;
}
};
Array(int size);
~Array();
int size();
T& operator[](int index);
iterator begin() {
iterator it(arr);
return it;
}
iterator end() {
iterator it(arr + m_size);
return it;
}
};
template <typename T>
Array<T>::Array(int size) {
this->m_size=size;
this->arr=new T[size];
}
template <typename T>
Array<T>::~Array() {
delete [] arr;
}
template <typename T>
int Array<T>::size() {
return m_size;
}
template <typename T>
T& Array<T>::operator[](int index) {
return arr[index];
}
template <typename T>
class LinkedList {
public:
struct Node;
private:
Node *head;
Node *tail;
public:
struct Node {
T t;
Node *next;
};
class iterator {
private:
Node *node;
public:
iterator(Node *);
iterator operator++(int);
T operator*();
bool operator==(const iterator &it);
};
LinkedList();
T at(int);
T operator[](int);
void push_front(T);
void push_back(T);
void pop_front();
void pop_back();
iterator begin();
iterator end();
};
template <typename T>
LinkedList<T>::LinkedList() {
head = nullptr;
tail = nullptr;
}
template <typename T>
void LinkedList<T>::push_front(T t) {
Node *node = new Node{t, head};
head = node;
if(tail == nullptr) {
tail = head;
}
}
template <typename T>
void LinkedList<T>::pop_front() {
Node *tmp = head;
head = head->next;
delete tmp;
}
template <typename T>
void LinkedList<T>::push_back(T t) {
Node *node = new Node{t, nullptr};
tail->next = node;
tail = node;
}
template <typename T>
void LinkedList<T>::pop_back() {
Node *old_tail = tail;
Node *prev = head;
while(prev->next != tail) {
prev = prev->next;
}
prev->next = nullptr;
tail = prev;
delete old_tail;
}
template <typename T>
T LinkedList<T>::at(int index) {
Node *node = head;
for(int i = 0; i < index; i++) {
node = node->next;
}
return node->t;
}
template <typename T>
T LinkedList<T>::operator [](int index) {
return at(index);
// Mike Litoris
}
template <typename T>
typename LinkedList<T>::iterator LinkedList<T>::begin() {
return iterator(head);
}
template <typename T>
typename LinkedList<T>::iterator LinkedList<T>::end() {
return iterator(tail);
}
template <typename T>
bool LinkedList<T>::iterator::operator==(const LinkedList<T>::iterator &it) {
return it.node == this->node;
}
template <typename T>
T LinkedList<T>::iterator::operator*() {
return this->node->t;
}
template <typename T>
typename LinkedList<T>::iterator LinkedList<T>::iterator::operator++(int) {
this->node = this->node->next;
return *this;
}
template <typename T>
LinkedList<T>::iterator::iterator(Node *n) {
node = n;
}
int main() {
LinkedList<int> ll;
ll.push_front(5);
// 5
ll.push_front(10);
// 10 5
ll.push_front(15);
// 15 10 5
ll.push_back(20);
// 15 10 5 20
ll.pop_front();
// 10 5 20
cout << ll[2] << endl;
// ll[0] -> 5;
// ll.at(0) -> 5;
for(auto it = ll.begin(); !(it == ll.end()); it++) {
cout << *it << endl;
}
return 0;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}