c++的List容器的简单实现

大耗子 2020年02月24日 313次浏览

c的List容器的简单实现,直接复刻的c自带的list容器

#pragma once
#include <iostream>
using namespace std;

template<class T>
class MyList
{
private:
	struct node
	{
		T data;
		node* pre;
		node* next;
	};
public:
	struct iterator
	{
		node* p;
		iterator(){}
		iterator(node* p){ this->p = p; }
		iterator(iterator & temp){ p = temp.p; }
		iterator& operator =(iterator const& temp)
		{
			p = temp.p;
			return *this;
		}
		bool operator !=(iterator& other) const{ return p != other.p; }
		bool operator ==(iterator& other) const{ return !(*this!=other); }
		iterator& operator++();
		iterator operator++(int);
		iterator& operator--();
		iterator operator--(int);
		iterator operator+(size_t num);
		iterator operator-(size_t num);
		T& operator[](size_t index);

		T& operator *();
		T* operator ->();
	};
public:
	MyList();
	MyList(initializer_list<T> it);
	MyList(MyList<T> const& other);
	MyList(int n, T const& srcDate);
	MyList(iterator b, iterator e);
	~MyList();
private:
	void creative()
	{
		head = new node;
		head->next = head;
		head->pre = head;
	}
public:
	void swap(iterator &a ,iterator &b);
	void sort(iterator a, size_t len, bool(*pf)(T a, T b));

public:
	iterator begin(){ return head->next; }
	iterator end(){ return head; }

public:
	void push_back(T const& data);
	void push_front(T const& data);
	void pop_back();
	void pop_front();
	void clear();
	void resize(size_t Num);
	void swap(MyList<T> &other);
	void reverse();
	void sort();
	void unique();
	void unique(bool (*pf)(T a,T b));
	T& back();
	T& front();
	T& at(size_t index)const;
	bool empty();
	int max_size();
	
	

public:
	T& operator[](size_t index);
	void operator =(MyList const & OtherList);
	

public:
	void insert(iterator where, const T& data);
	void insert(iterator where, size_t const num ,const T& data);
	void insert(iterator where, iterator first, iterator last); 
	void insert(iterator where, initializer_list<T> it);
	//void insert(iterator where, inputiterator first, inputiterator last); 
	//insert将一个、几个或一系列元素插入列表中的指定位置。
	//assign将元素从列表中擦除并将一组新的元素复制到目标列表。
	void assign(size_t num, const T& val);
	void assign(initializer_list<T> it);
	void assign(iterator first, iterator last);//不可以使用自身的迭代器
	//void assign( inputiterator first, inputiterator last); 
	void remove(const T& val);
	void splice(const iterator where, MyList<T>& source);
	//void splice(const iterator where, MyList<T>&& source);
	void splice(const iterator where, MyList<T>& source, const iterator one);
	void splice(const iterator where, MyList<T>& source, const iterator first, const iterator last);
	iterator erase(iterator where);
	iterator erase(iterator first, iterator last);
	void merge(MyList<T>& other);
	void merge(MyList<T>& other, bool(*pf)(T a, T b));
	void remove_if(bool (*pt)(T a));
private:
	node* head;

};

template<class T>
void MyList<T>::remove_if(bool (*pt)(T a))
{
	for (iterator temp = begin(); temp.p != head; )
	if ((*pt)(temp.p->data))
	{
		auto it = temp++;
		erase(it);
	}
	else
		temp++;
}

template<class T>
void MyList<T>::merge(MyList<T>& other)
{
	merge(other,isMin);
}
template<class T>
void MyList<T>::merge(MyList<T>& other, bool(*pf)(T a, T b))
{
	splice(end(), other);
	sort(begin(), max_size(), *pf);
}

template<class T>
void MyList<T>::sort(iterator a, size_t len, bool(*pf)(T a, T b))
{
	iterator it = begin();
	for (size_t i = 0; i < len-1; i++)
	{
		size_t min = i;
		for (size_t j = i + 1; j <= len-1; j++)
		{
			if ((*pf)(*(it + j), *(it + min)))
				min = j;
		}
		if (min != i)
		{
			swap(it + i, it + min);
			it = begin();
		}
	}


}
template<class T>
void MyList<T>::sort()
{
	sort(begin(), max_size(), isMin);//好哒
}
template<class T>
void MyList<T>::swap(iterator &a, iterator &b)
{
	auto temp=*a;
	*a = *b;
	*b = temp;
	//node* tempA = a.p->next;
	//node* tempB = b.p->pre;
	//node* temp = a.p->pre;
	//temp->next = b.p;
	//b.p->pre = temp;

	//temp = b.p->next;
	//temp->pre = a.p;
	//a.p->next = temp;
	//if (tempA == b.p)//相邻的情况
	//{
	//	b.p->next = a.p;
	//	a.p->pre = b.p;
	//}
	//else
	//{
	//	b.p->next = tempA;
	//	tempA->pre = b.p;
	//	a.p->pre = tempB;
	//	tempB->next = a.p;
	//}
}

template<class T>
bool Equal(T a, T b)
{
	return (a == b);
}

template<class T>
bool isMin(T a, T b)
{
	return !(a > b);
}
template<class T>
bool isMax(T a, T b)
{
	return !(a < b);
}

template<class T>
void MyList<T>::unique(bool(*pf)(T a, T b))
{
	iterator tempOne, tempTwo;
	tempOne.p = head->next;
	tempTwo.p = head->next->next;
	for (; tempTwo != end(); tempOne++, tempTwo++)
	while ((*pf)(*tempOne, *tempTwo) && tempTwo != end())
	{
		iterator temp = tempOne++;
		tempTwo++;
		erase(temp);
	}
}

template<class T>
void MyList<T>::unique()
{
	unique(Equal);
}



template<class T>
void MyList<T>::reverse()
{
	node* temp = head->pre;
	node* tempNext=temp->pre;
	head->next = temp;
	while (temp!=head)
	{
		temp->next = tempNext;
		tempNext = temp->next->pre;
		temp->next->pre = temp;
		temp = temp->next;
	}
}

template<class T>
void MyList<T>::splice(const iterator where, MyList<T>& source, const iterator first, const iterator last)
{
	node* temp = where.p->pre;
	node* firstTemp = first.p->pre;

	temp->next = first.p;
	first.p->pre = temp;
	last.p->pre->next = where.p;
	where.p->pre = last.p->pre;

	firstTemp->next = last.p;
	last.p->pre = first.p->pre;
}

template<class T>
void MyList<T>::splice(const iterator where, MyList<T>& source,const iterator one)
{
	node* temp = where.p->pre;
	node* oneTemp = one.p->pre;
	oneTemp->next = one.p->next;
	one.p->next->pre = oneTemp;
	temp->next = one.p;
	one.p->pre = temp;
	one.p->next = where.p;
	where.p->pre = one.p;
}

template<class T>
void MyList<T>::splice(const iterator where, MyList<T>& source)
{
	node* temp = where.p->pre;
	temp->next = source.head->next;
	source.head->next->pre = temp;
	where.p->pre = source.head->pre;
	source.head->pre->next=where.p;

	source.head->next = source.head;
	source.head->pre = source.head;

}

template<class T>
void MyList<T>::remove(const T& val)
{
	node* temp = head->next;
	while (temp!=head)
		if (temp->data==val)
		{
			node* tempdele = temp;
			temp = temp->next;
			tempdele->pre->next = tempdele->next;
			tempdele->next->pre = tempdele->pre;
			delete tempdele;
		}
		else
			temp = temp->next;
}

template<class T>
typename MyList<T>::iterator MyList<T>::erase(iterator where)
{
	if (head != where.p)
	{
		node* temp = where.p;
		where.p = where.p->next;
		temp->pre->next = temp->next;
		temp->next->pre = temp->pre;
		delete temp;
	}
	return where;
}
template<class T>
typename MyList<T>::iterator MyList<T>::erase(iterator first, iterator last)
{
	for (iterator temp; first != last; )
	{
		temp=first++;
		erase(temp);
	}
	return last;
}


template<class T>
void MyList<T>::assign(size_t num, const T& val)
{
	clear();
	for (size_t i = 0; i < num; i++)
		push_back(val);
}

template<class T>
void MyList<T>::assign(initializer_list<T> it )
{ 
	clear();
	for (auto s : it)
		push_back(s);
}

template<class T>
void MyList<T>::assign(iterator first, iterator last)
{
	clear();
	for (; first!=last; first++)
		push_back(first.p->data);
}

template<class T>
void MyList<T>::swap(MyList<T> &other)
{
	node*p = head;
	head = other.head;
	other.head = p;
}

template<class T>
void MyList<T>::insert(iterator where, const T& data)
{
	node* val = new node;
	val->data = data;

	val->pre = where.p->pre;
	val->next = where.p;
	where.p->pre->next = val;
	where.p->pre = val;
}

template<class T>
void MyList<T>::insert(iterator where, size_t const num, const T& data)
{
	for (size_t i = 0; i < num; i++)
		insert(where,data);
}

template<class T>
void MyList<T>::insert(iterator where, iterator first, iterator last)
{
	for (; first != last; first++)
		insert(where, first.p->data);
}

template<class T>
void MyList<T>::insert(iterator where, initializer_list<T> it)
{
	for (auto s:it)
		insert(where,s);
}

template<class T>
void MyList<T>::resize(size_t Num)
{
	size_t max = max_size();
	if (Num>max)
		for (size_t i = 0; i < Num-max; i++)
			push_back(0);
	else
		for (size_t i = 0; i < max-Num; i++)
			pop_back();
}


template<class T>
void MyList<T>::pop_front()
{
	node* temp = head->next;
	temp->next->pre = head;
	head->next = temp->next;
	delete temp;
}

template<class T>
void MyList<T>::pop_back()
{
	node* temp = head->pre;
	temp->pre->next = head;
	head->pre = temp->pre;
	delete temp;
}

template<class T>
int MyList<T>::max_size()
{
	int maxNum=0;
	for (node* temp = head->next; temp != head; temp = temp->next, maxNum++);
	return maxNum;
}

template<class T>
bool MyList<T>::empty()
{
	return head->next == head;
}

template<class T>
T& MyList<T>::front()
{
	return head->next->data;
}

template<class T>
T& MyList<T>::back()
{
	return head->pre->data;
}

template<class T>
void MyList<T>::clear()
{
	for (size_t i = 0, max = max_size(); i < max; i++)
		pop_back();
}

template<class T>
T& MyList<T>::iterator::operator*()
{
	return p->data;
}
template<class T>
T* MyList<T>::iterator::operator->()
{
	return &p->data;
}

template<class T>
void MyList<T>::operator =(MyList const & OtherList)
{
	assign(OtherList.head->next, OtherList.head);//不可以使用自身的迭代器
}

template<class T>
T& MyList<T>::operator[](size_t index)
{
	return at(index);
}

template<class T>
T& MyList<T>::at(size_t index)const
{
	iterator temp = head->next;
	return temp[index];
}

template<class T>
T& MyList<T>::iterator::operator[](size_t index) 
{
	iterator temp=*this;
	
	for (size_t i = 0; i < index; i++)
		temp.p = temp.p->next;
	return temp.p->data;
}

template<class T>
typename MyList<T>::iterator  MyList<T>::iterator::operator-(size_t num)
{
	iterator temp = *this;
	for (size_t i = 0; i < num; i++)
		temp.p = temp.p->pre;
	return temp;
}

template<class T>
typename MyList<T>::iterator  MyList<T>::iterator::operator+(size_t num)
{
	
	iterator temp =*this;
	
	for (size_t i = 0; i < num; i++)
		temp.p = temp.p->next;
	return temp;
}

template<class T>
typename MyList<T>::iterator MyList<T>::iterator::operator--(int)
{
	iterator temp=*this;
	p = p->pre;
	return temp;
}
template<class T>
typename MyList<T>::iterator MyList<T>::iterator::operator++(int)
{
	iterator temp = *this;
	p = p->next;
	return temp;
}

template<class T>
typename MyList<T>::iterator& MyList<T>::iterator::operator++()
{
	p = p->next;
	return *this;
}
template<class T>
typename MyList<T>::iterator& MyList<T>::iterator::operator--()
{
	p = p->pre;
	return *this;
}
template<class T>
void MyList<T>::push_front(T const& data)
{
	node* temp = new node;
	temp->data = data;

	temp->next = head->next;
	temp->pre = head;
	head->next->pre = temp;
	head->next = temp;
	
}

template<class T>
void MyList<T>::push_back(T const& data)
{
	node* temp = new node;
	temp->data = data;

	temp->pre = head->pre;
	temp->next = head;
	temp->pre->next = temp;
	head->pre = temp;
}

template<class T>
MyList<T>::MyList(MyList<T> const& OtherList)
{
	creative();
	assign(OtherList.head->next, OtherList.head);//不可以使用自身的迭代器
}
template<class T>
MyList<T>::MyList(int n, T const& val)
{
	creative();
	assign(n ,val);
}
template<class T>
MyList<T>::MyList(iterator b, iterator e)
{
	creative();
	assign(b, e);
}

template<class T>
MyList<T>::MyList(initializer_list<T> it)
{
	creative();
	for (auto a : it)
		push_back(a);
}
template<class T>
MyList<T>::MyList()
{
	creative();
}

template<class T>
MyList<T>::~MyList()
{
	if (head)
	{
		clear();
		delete head;
	}
}