#include <iostream>
#include <algorithm>
using namespace std;
struct single_node{
int data;
single_node* next;
};
class single_lst{
public:
using node = single_node;
using node_ptr = node*;
public:
struct single_lst_iterator{
public:
single_lst_iterator(node_ptr p) :ptr(p){}
int& operator*(){ return ptr->data; }
node_ptr get(){ return ptr; }
single_lst_iterator& operator++(){
ptr = ptr->next;
return *this;
}
single_lst_iterator& operator++(int){
single_lst_iterator result = *this;
++(*this);
return result;
}
friend bool operator==(const single_lst_iterator& left,
const single_lst_iterator& right){
return left.ptr == right.ptr;
}
friend bool operator!=(const single_lst_iterator& left,
const single_lst_iterator& right){
return left.ptr != right.ptr;
}
private:
node_ptr ptr;
};
public:
single_lst() = default;
single_lst(const single_lst& other) : head(nullptr){
if(other.head){
head = new node { 0, nullptr };
auto cur = head;
auto it = other.begin();
while(true){
cur->data = *it;
auto tmp = it;
tmp++;
if(tmp == this->end()){
break;
}
cur->next = new node { 0 , nullptr };
cur = cur->next;
it = tmp;
}
}
}
single_lst(const std::initializer_list<int>& Ilist) : head(nullptr){
for(auto it = std::rbegin(Ilist); it != std::rend(Ilist); it++){
push_front(*it);
}
}
void push_front(int val){
node* newNode = new node { val, nullptr };
if(head){
newNode->next = head;
}
head = newNode;
}
void pop_front(){
auto first = head;
if(head){
head = head->next;
delete first;
}
}
single_lst_iterator begin(){ return single_lst_iterator(head); }
single_lst_iterator begin() const{ return single_lst_iterator(head); }
single_lst_iterator end(){ return single_lst_iterator(nullptr); }
single_lst_iterator end()const{ return single_lst_iterator(nullptr); }
private:
node_ptr head;
};
auto main() ->int{
single_lst lst { 1,2,3 };
lst.push_front(1);
lst.push_front(32);
lst.push_front(64);
lst.push_front(1);
lst.pop_front();
for(auto & ele : lst){
cout << ele << " ";
}
}