简易List

表——Vector实现  :

不易被索引、插入、删除代价低( 已知位置,否则劣在于查找 )、查找低效;


这次实现过程错误百出,最后收获满满:

·对于嵌套类Node、iterator、const_iterator就在List类内实现吧;

·类内的函数实现顺序有时候会让编译器提示出List的对象并没有这个功能,但是运行还是可以的,坑死人啊;

·讨论一下*this,以下是我的理解:this 时刻指向当前对象,*this即为当前对象,对于类成员函数的第一个参数隐藏为(*this),有:

iterator begin() {
		iterator ite(head); //方式一
		return ++ite;
}
iterator begin() const {
		return head->next;  //方式二
}
这里用到iterator 的构造函数,让current=this了;

·对于即将销毁的右值List,只要接管了其首尾及大小,即可完成接管这条链:

List(List && rhs) :theSize{ rhs.theSize }, head{ rhs.head }
	tail {rhs.tail} {
	rhs.theSize = 0;
	rhs.head = nullptr;
	rhs.tail = nullptr;
}

·函数关系:

List()--->>>init();

clear()--->>>pop_front()--->>>erase();

pop()--->>>erase();

push()--->>>insert();

·类关系:

List的基础数据就是Node,同时Node也要提供给迭代器,在这里它就是一种直接被存取而不是通过方法访问的数据的类;

iterator继承自const_iterator,一些方法函数例如retrieve()只想让继承关系的能看到,而不让其他类有这些访问权限,那么就选择了protected;

由于iterator的构造函数是从指针变量显示地构造迭代器,这样不被允许,我不知道为什么啊,故不能让其公有,但是还要让List能访问,故friend class List<Object>;


贴上代码:


/*
list
·iterator 为 内嵌类类型,功能丰富
包含:begin()|end()|erase()|insert()|构造|检验

·list 需要Node节点类
包含:五大函数(其中operator = 为深层拷贝)
size()|empty()|clear()|back()|front()
push_back()|push_front()|pop_front()|pop_back()|

*/

template
    
    
     
     
class List
{
private:
	//节点
	struct Node
	{
		Object data;
		Node * prev;
		Node * next;
		Node(const Object & d = Object{}, Node*p = nullptr, Node*n = nullptr)
			:data{ d }, prev{ p }, next{ n } {};
		Node(Object && d, Node*p = nullptr, Node*n = nullptr)
			:data{ move(d) }, prev{ p }, next{ n } {};
	};
public:
	//迭代器
	class const_iterator
	{
	public:
		const_iterator() :current{ nullptr } {}
		const Object & operator* () const { return retrive(); }
		const_iterator & operator++() {
			current = current->next;
			return *this;
		}
		const_iterator & operator--() {
			current = current->prev;
			return *this;
		}
		const_iterator & operator++(int) {
			const iterator old = *this;
			++(*this);
			return old;
		}
		const_iterator & operator--(int) {
			const iterator old = *this;
			--(*this);
			return old;
		}
		bool operator==(const const_iterator & rhs) const {
			return current == rhs.current;
		}
		bool operator!=(const const_iterator & rhs) const {
			return !(*this == rhs);
		}
	protected:
		Node * current;
		const List
     
      *theList;
		Object & retrive() const { return current->data; }
		//从指针变量显示地构造迭代器是不允许的,故不能是公有的
		//要让iterator能看到故protect
		//要让List能看到故friend List
		const_iterator(Node * p):current{ p } {}
		friend class List;
	};
	class iterator : public const_iterator
	{
	public:
		iterator() {}
		Object & operator* () { return const_iterator::retrive(); }
		const Object & operator* () const {
			return const_iterator::operator*();
		}
		iterator & operator++() {
			current = current->next;
			return *this;
		}
		iterator & operator--() {
			current = current->prev;
			return *this;
		}
		iterator & operator++(int) {
			iterator old = *this;
			++(*this);
			return old;
		}
		iterator & operator--(int) {
			iterator old = *this;
			--(*this);
			return old;
		}
	protected:
		iterator( Node * p):const_iterator( p) {}
		friend class List;
	};

	//迭代器相关函数
	iterator begin() {
		iterator ite(head); //方式一
		return ++ite;
	}
	const_iterator begin() const {
		return head->next;  //方式二
	}
	iterator end() {
		return tail;
	}
	const_iterator end() const {
		return tail;
	}
	iterator insert(iterator ite, const Object & x) {
		Node * p = ite.current;
		theSize++;
		return p->prev = p->prev->next = new Node(x, p->prev, p);
	}
	iterator insert(iterator ite, Object && x) {
		Node * p = ite.current;
		theSize++;
		return p->prev = p->prev->next = new Node(move(x), p->prev, p);
	}
	iterator erase(iterator ite) {
		Node * p = ite.current;
		iterator retVal(p->next);
		p->prev->next = p->next;
		p->next->prev = p->prev;
		delete p;
		theSize--;
		return retVal;
	}
	iterator erase(iterator from, iterator to) {
		for (iterator ite = form; ite != to;)
			ite = erase(ite);
		return to;
	}

	//List本身函数
	void init() {
		theSize = 0;
		head = new Node;
		tail = new Node;
		head->next = tail;
		tail->prev = head;
	}
	int size() const { return theSize; }
	bool empty() const { return theSize == 0; }
	void clear() {
		while (!empty())
			pop_front();
	}
	void pop_front() { erase(begin()); }
	void pop_back() { erase(--end()); }
	Object & front() { return *begin(); }
	const Object & front() const { return *begin(); }
	Object & back() { return *--end(); }
	const Object & back() const { return *--end(); }
	void push_front(const Object & x) { insert(begin(), x); }
	void push_front(Object && x) { insert(begin(), move(x)); }
	void push_back(const Object & x) { insert(end(), x); }
	void push_back(Object && x) { insert(end(), move(x)); }
    List() { init(); }
	List(const List & rhs) {
		init();
		for (auto & x : rhs)
			push_back(x);
	}
	List(List && rhs) :theSize{ rhs.theSize }, head{ rhs.head }
		tail {rhs.tail} {
		rhs.theSize = 0;
		rhs.head = nullptr;
		rhs.tail = nullptr;
	}
	List & operator =(const List & rhs) {
		List copy = rhs;
		swap(*this, copy);
		return *this;
	}
	List & operator =(List && rhs) {
		swap(thsSize, rhs.theSize);
		swap(head, rhs.head);
		swap(tail, rhs.tail);
	}
	~List() {
		clear();
		delete head;
		delete tail;
	}

private:
	int theSize;
	Node * head;
	Node * tail;
};

如要加上范围检验,比如构造迭代器,要确认当前的位置是不是要求的List的、不要在头结点前插入等等:

iterator insert(iterator ite, const Object & x) {
	ite.assertIsValid();
	if (ite.theLise != this)
		throw IteratorMismatchException{};
	Node * p = ite.current;
	thsSize++;
	return p->prev = p->prev->next = new Node(x, p->prev, p);
}

class const_iterator
{
protected:
	Node * current;
	const List *theList;
	const_iterator(const List & lst, Node * p)
		:theList{ &lst }, current{ p } {}
	void assertIsValid() {
		if (theList == nullptr || current == nullptr || current == theList->head)
			throw IteratorOutOfBoundsExcepction{};
}

iterator begin() {
	iterator ite(*this, head);
	return ++ite;
}



综合Vector和List发现其优劣必应征其特点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值