STL_List

namespace wh {
	template<class T>
	struct _list_node {
		_list_node<T>* _prev;
		_list_node<T>* _next;
		T _data;
		_list_node(const T& x = T()) :_data(x), _prev(nullptr), _next(nullptr) {}
	};

	template<class T, class Ref, class Ptr>
	struct _list_iterator {
		typedef _list_node<T> Node;
		typedef _list_iterator<T, Ref, Ptr> self;
		Node* _node;
		_list_iterator(Node* node) :_node(node) {

		}

		Ref operator*() {
			return _node->_data;
		}

		Ptr operator->() {
			return &_node->data;
		}

		self& operator++() {
			_node = _node->_next;
			return *this;
		}

		self& operator++(int) {
			self* tmp = this;
			_node = _node->_next;
			return *tmp;
		}

		self& operator+(int n) {
			self* tmp = this;
			while (n--) {
				tmp->_node = tmp->_node->_next;
			}
			return *tmp;
		}

		self& operator-(int n) {
			self* tmp = this;
			while (n--) {
				tmp->_node = tmp->_node->_prev;
			}
			return *tmp;
		}

		bool operator!=(const self& it) {
			return _node != it._node;
		}

	};

	template<class T>
	class list {
		typedef _list_node<T> Node;
	private:
		Node* _head;
	public:
		typedef  _list_iterator<T, T&, T*> iterator;
		typedef  _list_iterator<T, const T&, const T*> const_iterator;
		list() {
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
		}

		list(const list<T>& lt) {
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			const_iterator it = lt.cbegin();
			while (it != lt.cend()) {
				push_back(*it);
				it++;
			}
		}

		list<T>& operator=(list<T> lt) {
			std::swap(_head, lt._head);
			return *this;
		}


		void push_back(const T& x) {
			Node* newnode = new Node(x);
			Node* tail = _head->_prev;
			tail->_next = newnode;
			newnode->_next = _head;
			newnode->_prev = tail;
			_head->_prev = newnode;
		}

		void insert(iterator pos, const T& x) {
			Node* cur = pos._node;
			Node* pos_prev = cur->_prev;
			Node* newnode = new Node(x);
			newnode->_prev = pos_prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			pos_prev->_next = newnode;
		}

		iterator erase(iterator pos) {
			assert(pos != end());
			Node* cur = pos._node;
			Node* next = cur->_next;
			Node* prev = cur->_prev;
			delete cur;
			next->_prev = prev;
			prev->_next = next;
			return next;
		}

		void clear() {
			iterator it = begin();
			while (it != end()) {
				it = erase(it);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		iterator begin() {
			return _head->_next;
		}

		iterator end() {
			return _head;
		}

		const_iterator cbegin()const {
			return const_iterator(_head->_next);
		}

		const_iterator cend()const {
			return const_iterator(_head);
		}
	};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值