list 模板类的简单实现

来源:互联网 发布:网络尖兵软件下载 编辑:程序博客网 时间:2024/05/23 11:32

最近学数据结构,于是尝试着去实现了一个 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 ; }ci_t   operator++ (int) {ci_t  ci(*this) ;pNode = pNode->next ;return ci ;}// --ci_t & operator--() { pNode = pNode->prev ; return *this ; }ci_t   operator--(int) {ci_t ci(*this) ;pNode = pNode->prev ;return ci ;}// ==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> ;using const_Iterator<T>::pNode ; // 不加这句,codeblocks 就一直提示错误:// error: 'pNode' was not declared in this scope|public:Iterator (Node<T> * n) : const_Iterator<T>(n) {}Iterator (const Iterator &ci): const_Iterator<T>(ci) {}Iterator operator= (const Iterator &ci) { pNode = ci.pNode; return *this ;}T & operator* () { return const_Iterator<T>::pNode->elem ; }T * operator->() { return &const_Iterator<T>::pNode->elem ; }// 不重定义这些操作符,所有对 Iterator 的操作(++,--)的结果都会返回 基类,// 导致无法实现这些 修改内容 的语句:erase (--end()) ;// 编译器会提示,const_Iterator<T> 类无法转换为 Iterator<T> 类Iterator & operator-- () {pNode = pNode->prev ;return *this ;}Iterator   operator-- (int) {Iterator itr(*this) ;pNode = pNode->prev ;return itr ;}Iterator & operator++ () {pNode = pNode->next ;return *this ;}Iterator   operator++ (int) {Iterator itr(*this) ;pNode = pNode->next ;return itr ;}} ;////////////////////////////////////////////////////////////////////////////// List, 简单实现,无错误检测,无边界检测//template <class T>class List {public:typedef const_Iterator<T>const_iterator ;typedef Iterator<T>iterator ;public:// Copy controyList () { 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 ;}// Iteratorsiterator begin ()    { return iterator(head->next) ; }const_iterator begin () const { return const_iterator(head->next) ; }iterator end   ()    { return iterator(tail) ; }const_iteratorend  () const { return const_iterator(tail) ; }//rbegin () ;//rend () ;// Capacityboolempty () const { return !theSize ; }int size () const { return  theSize ; }//max_size () ;//resize () ;// Element accessT   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 类指定的内容,如果 const_iterator 类传进来,就会提示// 基类 无法转换为 继承类。从而导致编译不通过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 ;}//swapvoid clear () { erase (begin(), end()) ; }/*// Operationsspliceremoveremove_ifuniquemerge sort reverse // Allocatorget_allocator*/private:Node<T>*head ;Node<T>*tail ;inttheSize ;void init () {head = new Node<T> ;tail = new Node<T> ;head->next = tail ;tail->prev = head ;theSize = 0 ;}} ;


原创粉丝点击