[Data Structure] 重新实现List类模板

vector和list的区别:http://www.cnblogs.com/shijingjing07/p/5587719.html

list是一个常见的容器,采用双向链表实现,所以是非线性存储结构,与vector相比,随机存取的效率低,而插入删除效率高。

基于迭代器的List类模板实现,包括双向链表的构造、删除、插入、迭代器的实现等功能。

template<class T>
struct Node {
	Node(const T& x) : _data(x) , _next(nullptr) , _prev(nullptr) { }

	Node<T>* _next;
	Node<T>* _prev;
	T _data;
};

template<class T>
struct Iterator {
	typedef Node<T> Node;

	Node* _node;
	Iterator(Node* x) : _node(x) { }
	Iterator() : _node(T()) {}

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

	T* operator->() {
		return &(operator*());
	}

	bool operator!=(const Iterator<T>& s) {
		return _node != s._node;
	}

	bool operator==(const Iterator<T>& s) {
		return _node == s._node;
	}

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

	//后置++
	Iterator<T> operator++(int) {
		Iterator<T> tmp(*this);
		_node = _node->_next;
		return tmp;
	}

	Iterator<T> &operator--() {
		_node = _node->_prev;
		return *this;
	}
	//后置--
	Iterator<T> operator--(int) {
		Iterator<T> tmp(*this);
		_node = _node->_prev;
		return tmp;
	}

	Iterator advance(int step) {
		while (step > 0) {
			_node = _node->_next;
			step--;
		}
		return *this;
	}

	Iterator goback(int step) {
		while (step > 0) {
			_node = _node->_prev;
			step--;
		}
		return *this;
	}
};

template<class T>
class List
{
public:
	friend void Display(List<T> list);

public:
	typedef T value_type;
	typedef T& reference;
	typedef const T& const_reference;
	typedef size_t size_type;
	typedef Node<T> Node;

	typedef Iterator<T> iterator;
	typedef Iterator<const T> const_iterator;

	List() {
		_head = new Node(T());
		_head->_next = _head;
		_head->_prev = _head;
	}

	~List() {
		clear();
		delete _head;
		_head = NULL;
	}

	void clear() {
		erase(begin(), end());
		_head->_next = _head;
		_head->_prev = _head;
	}

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

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

	iterator end() {
		return _head;
	}

	const_iterator end() const {
		return _head;
	}

#if 0
	iterator rbegin() {
		return _head->_prev;
	}

	const_iterator rbegin() {
		return _head->_prev;
	}

	iterator rend() {
		return _head;
	}

	const_iterator rend() {
		return _head;
	}
#endif

	void display() {
		iterator it = begin();
		while (it != end()) {
			cout << *it << " ";
			++it;
		}
		cout << '\n';
	}

	void push_back(const value_type& x) {
		insert(end(), x);
	}

	void push_front(const value_type& x) {
		insert(begin(), x);
	}

	void pop_back() {
		erase(--end());
	}
	void pop_front() {
		erase(begin());
	}

	iterator insert(iterator pos, const value_type& value) {
		Node* cur = pos._node;
		Node* prev = cur->_prev;

		Node* node = new Node(value);

		prev->_next = node;
		node->_prev = prev;

		node->_next = cur;
		cur->_prev = node;

		return pos;
	}

	void insert(iterator pos, size_type n, const value_type& value) {
		while (n > 0) {
			pos = insert(pos, value);
			n--;
		}
	}

	void insert(iterator pos, iterator f, iterator l) {
		while (f != l) {
			const value_type value = f._node->_data;
			pos = insert(pos, value);
			++f;
		}
	}

	iterator erase(iterator pos) {
		assert(pos != end());

		Node * cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;


		prev->_next = next;
		next->_prev = prev;
		delete cur;

		pos = prev;

		return next;
	}

	iterator erase(iterator first, iterator last) {
		while (first != last) {
			first = erase(first);
		}
		return first;
	}

	void remove(const value_type &value) {
		iterator it = begin();
		while (it != end()) {
			if (*it == value) {
				it = erase(it);
			} else {
				it++;
			}
		}
	}

	void assign(size_t n, const value_type& val) {
		clear();
		for (size_t i = 0; i < n; ++i)
		{
			push_back(val);
		}
	}

	void assign(iterator first, iterator last) {
		clear();
		while (first != last) {
			push_back(*first);
			++first;
		}
	}

	List& operator=(const List & other) {
		clear(); 
		insert(end(), other.begin(), other.end());
		return *this;
	}

	reference front() {
		return (*begin());
	}

	const_reference front() const {
		return (*begin());
	}

	reference back() {
		return *(--end());
	}

	const_reference  back() const {
		return *(--end());
	}

	void unique() {
		iterator it1 = begin(), it2 = begin();
		while (it1 != end()) {
			it2++ = it1;
			while (it2 != end()) {
				if (*it2 == *it1) {
					it2 = erase(it2);
				} else {
					it2++;
				}
			}
			++it1;
		}
	}

	void resize(size_type n, value_type value = value_type()) {
		if (n > size()) {
			insert(end(), n - size(), value);
		} else {
			while (n < size())
				pop_back();
		}
	}

	size_type size() {
		size_type size = 0;
		iterator it = begin();
		while (it++ != end()) {
			size++;
		}
		return size;
	}

	bool empty() {
		return begin() == end();
	}

protected:
	Node * _head;
};

template <class T>
void Display(List<T> list) {
#if 0
	iterator it = list.begin();
	while (it != list.end()) {
		cout << *it << "-->";
		cout << '\n';
		++it;
	}
#endif
}

测试代码

int main() {
	List<int> l2;
	l2.push_back(10);
	l2.push_back(20);
	l2.push_back(30);
	l2.push_back(40); //10 20 30 40
	//Display<int>(l2);
	l2.display(); //测试display
	cout << "首元素: " << l2.front() << endl; //测试front

	List<int> mylist;
	mylist.assign(l2.begin(), l2.end()); //测试assign
	cout << "mylist的内容: ";
	mylist.display();

	mylist.assign(10, 1);
	cout << "mylist的内容: ";
	mylist.display();

	mylist.unique();
	cout << "mylist的内容: ";
	mylist.display();

	mylist.clear();
	for (int i = 1; i < 10; ++i) 
		mylist.push_back(i);

	mylist.resize(5);
	mylist.resize(8, 100);
	mylist.resize(12);

	cout << "mylist的内容: ";
	mylist.display();

	mylist.clear(); //测试clear()
	if (mylist.empty())
		cout << "mylist为空" << endl;
	mylist.push_back(10); //测试push_back()

	while (mylist.back() != 5) {
		mylist.push_back(mylist.back() - 1); //测试back
	}

	//测试迭代器遍历
	cout << "mylist的内容: ";
	for (List<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
		cout << *it << " ";
	cout << '\n';

	mylist.clear();
	for (int i = 1; i < 10; ++i) 
		mylist.push_back(i * 10);

	List<int>::iterator it1 = mylist.begin();
	List<int>::iterator it2 = mylist.begin();
	it2.advance(6); //测试advance(step)
	++it1;

	//测试erase(iterator it)
	it1 = mylist.erase(it1);   // 10 30 40 50 60 70 80 90
	it2 = mylist.erase(it2);   // 10 30 40 50 60 80 90

	cout << "mylist的内容: ";
	mylist.display();

	++it1;
	--it2;

	//测试erase(iterator f, iterator l)
	mylist.erase(it1, it2);     // 10 30 60 80 90

	cout << "mylist的内容: ";
	mylist.display();

	it1 = mylist.begin();
	it1.advance(2);
	//测试insert
	mylist.insert(it1, 100); //10 30 100 60 80 90
	cout << "mylist的内容: ";
	mylist.display();

	//测试insert
	mylist.insert(it1, 2, 200); //10 30 100 200 200 60 80 90
	cout << "mylist的内容: ";
	mylist.display();

	//测试insert
	mylist.insert(it1, l2.begin(), l2.end()); //10 30 100 200 200 10 20 30 40 60 80 90
	cout << "mylist的内容: ";
	mylist.display();

	mylist.erase(--it1); //10 30 100 200 200 10 20 30 60 80 90
	cout << "mylist的内容: ";
	mylist.display();

	List<int> list2 = mylist;
	cout << "list2的内容: ";
	list2.display();

	list2.pop_back();
	list2.pop_front();
	list2.push_front(0);
	cout << "list2的内容: ";
	list2.display();

	list2.remove(200);
	cout << "list2的内容: ";
	list2.display();

	system("pause");
	return 0;
}

测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值