#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 << " ";
	}
}

'알고리즘 + 자료구조' 카테고리의 다른 글

트리  (0) 2023.06.15
Recursion(재귀)  (0) 2023.06.14
Recursion - English Ruler  (0) 2023.06.14
순수 가우스 소거법 Naive Gaussian Elimination  (0) 2022.07.14
호너씨와 조립제법과 수도미분  (0) 2022.07.10
복사했습니다!