Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

list 模板类的简单实现

来源: panyanyany 分享至:

最近学数据结构,于是尝试着去实现了一个 list 类,发现确实有很多问题,特别是类的继承这一块,有些问题搞不懂……

这个 list  类只是一个简单的实现,只提供了基本的功能,也没有边界检测什么的,越界访问的问题由使用者自己把握……

很多功能都是没有实现的,总得来说这是一个比较裸的 list 模板类,没有什么实用价值……


/* THE PROGRAM IS MADE BY PYY */

//#include <iostream>
//#include "class.h"

//using namespace std ;

//////////////////////////////////////////////////////////////////////////
//
// Decleration
//

template <class T>
class Node ;

template <class T>
class List ;

template <class T>
class const_Iterator ;

template <class T>
class Iterator ;

//////////////////////////////////////////////////////////////////////////
//
// Node
//

template <class T>
class Node {
	friend class List<T> ;
	friend class const_Iterator<T> ;
	friend class Iterator<T> ;
private:
	T 			elem ;
	Node		*prev ;
	Node	 	*next ;
	
	Node (const T &t = *(new T), Node *p = 0, Node *n = 0)
		: elem(t), prev(p), next(n) {}
} ;

//////////////////////////////////////////////////////////////////////////
//
// const_Iterator
//

template <class T>
class const_Iterator {
	friend class List<T> ;
protected:
	typedef const_Iterator<T> ci_t ;
public:
	const_Iterator (Node<T> * n) : pNode(n) {}
	const_Iterator (const ci_t &ci)
		: pNode(ci.pNode) {}
	const T & operator* () const { return pNode->elem ; }
	const T * operator->() const { return &pNode->elem ; }
	ci_t & operator= (const ci_t &ci) { pNode = ci.pNode ; return *this ; }
	// ++
	ci_t & operator++() { pNode = pNode->next ; return *this ; }
	const ci_t & operator++ (int) {
		ci_t  *pci = new ci_t(*this) ;
		pNode = pNode->next ;
		return *pci ;
	}
	// --
	ci_t & operator--() { pNode = pNode->prev ; return *this ; }
	const ci_t & operator--(int) {
		ci_t *pci = new ci_t(*this) ;
		pNode = pNode->prev ;
		return *pci ;
	}
	// ==
	bool operator== (const ci_t &ci) const {
		return pNode == ci.pNode ;
	}
	bool operator!= (const ci_t &ci) const {
		return !(*this == ci) ;
	}
protected:
	Node<T>	*pNode ;
} ;

//////////////////////////////////////////////////////////////////////////
//
// Iterator
//
template <class T>
class Iterator: public const_Iterator<T> {	
	friend class List<T> ;
public:
	Iterator (Node<T> * n) : const_Iterator<T>(n) {}
	Iterator (const Iterator &ci): const_Iterator<T>(ci) {}
	T & operator* () { return const_Iterator<T>::pNode->elem ; }
	T * operator->() { return &const_Iterator<T>::pNode->elem ; }
} ;

//////////////////////////////////////////////////////////////////////////
//
// List, 简单实现,无错误检测,无边界检测
//

template <class T>
class List {
public:
	typedef const_Iterator<T>	const_iterator ;
	typedef Iterator<T>			iterator ;
public:
	// Copy controy
	List () { init () ; }
	~List () { 
		clear () ; 
		delete head ;
		delete tail ;
	}
	const List<T> & operator= (const List<T> &rhs) {
		if (this == &rhs)
			return *this ;
		clear () ;
		for (const_iterator itr = rhs.begin () ; itr != rhs.end() ; ++itr)
			push_back (*itr) ;
		return *this ;
	}
	
	// Iterators
	iterator 		begin () 	   { return iterator(head->next) ; }
	const_iterator 	begin () const { return const_iterator(head->next) ; }
	iterator 		end   () 	   { return iterator(tail) ; }
	const_iterator	end	  () const { return const_iterator(tail) ; }
//	rbegin () ;
//	rend () ;
	
	// Capacity
	bool	empty 	() const { return !theSize ; }
	int 	size 	() const { return  theSize ; }
//	max_size () ;
//	resize () ;
	
	// Element access
	T 		  front () 		 { return *begin () ; }
	const T	  front () const { return *begin () ; }
	T		  back  () 		 { return *--end () ; }
	const T   back	() const { return *--end () ; }
	
	// Modifiers
//	assign () ;
	void push_front (const T &t) 	{ insert (begin(), t) ; }
	void pop_front 	() 				{ erase  (begin()) ; 	}
	void push_back 	(const T &t) 	{ insert (end(), t) ; 	}
	void pop_back 	() 				{ erase  (--end()) ; 	}
	iterator insert (iterator itr, const T &t) {
		++theSize ;
		
		Node<T> *pCurNode = itr.pNode ;
		return iterator(
			pCurNode->prev = pCurNode->prev->next = 
			new Node<T>(t, pCurNode->prev, pCurNode)) ;
	}
	iterator erase (iterator itr) {
		Node<T> *curNode = itr.pNode ;
		Node<T> *nextNode = curNode->next ;
		curNode->prev->next = curNode->next ;
		curNode->next->prev = curNode->prev ;
		
		delete curNode ;
		--theSize ;
		return iterator(nextNode) ;
	}
	iterator erase (iterator start, iterator end) {
		while (!empty() && (start != end))
			start = erase (start) ;
		return end ;
	}
//	swap
	void clear () { erase (begin(), end()) ; }
	
/*
	// Operations
	splice
	remove
	remove_if
	unique
	merge 
	sort 
	reverse 
	
	// Allocator
	get_allocator
*/

private:
	Node<T>		*head ;
	Node<T>		*tail ;
	int			theSize ;
	
	void init () {
		head = new Node<T> ;
		tail = new Node<T> ;
		
		head->next = tail ;
		tail->prev = head ;
		
		theSize = 0 ;
	}
} ;



Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史