C++ 学习笔记 (5) 单链表模拟实现迭代器

2018.5.7 第一次接触模板编程

模板编程就类似于设计模式的工厂模式,可以接受多种类型的参数,达到复用。

程序结果:


仅为小测试,所以不用工程

#include <bits/stdc++.h>
#define rep( i , j , n ) for ( int i = int(j) ; i < int(n) ; ++i )
#define dew( i , j , n ) for ( int i = int(n-1) ; i > int(j) ; --i )
#define _PATH __FILE__ , __LINE__
typedef std::pair < int , int > P ;
using std::cin ;
using std::cout ;
using std::endl ;
using std::string ;

namespace YHL {
	template< typename T >
	class Node {
	public:
	    T data ;
	    Node<T> *next ;
	public:
	    explicit Node<T> () : data ( 0 ) , next ( nullptr ) {}
	    explicit Node<T> ( const T &_data , Node<T> *const _next = nullptr )
		    : data ( _data )
		    , next ( _next ) {}
	    ~Node<T> () { next = nullptr ; }
	    friend std::ostream& operator << ( std::ostream &out
	            , const Node<T> &One ) {
			out << One.data << " " ;
			return out ;
	    }
	} ;

	template< typename T > 
	class My_iterator {
	private: 
		Node<T> *ptr ;
	public:
		explicit My_iterator<T> () : ptr ( nullptr ) {}
		explicit My_iterator<T> ( Node<T> *const _ptr = nullptr ) 
			: ptr ( _ptr ) {}
		~My_iterator () { ptr = nullptr ; }
		Node<T>& operator* () {
			return *ptr ;
		}
		Node<T>* operator-> () {
			return ptr ;
		}
		inline void operator++ () {
			if ( ptr )  ptr = ptr->next ;
		}
		inline void operator++ ( int ) {
			if ( ptr )  ptr = ptr->next ;
		}
		bool operator != ( const My_iterator<T> &it ) const {
			return ptr != it.ptr ;
		}
	} ;

	template< typename T , typename Function >
	void My_foreach ( T src , T des , Function work ) {
		for ( ; src != des ; src++) 
			work ( *src ) ;
	}

	template< typename T > 
	class List {
	private:
		Node<T> *head , *tail ;
		size_t _size ;
	public:
		explicit List () : head ( new Node<T> () ) , tail ( head ) , _size ( 0 ) {}
		~List () noexcept {
			clear () ;
			if ( head != nullptr )
				delete head ;
			head = tail = nullptr ;
		}
		Node<T>* push_front ( const T &data ) {
			Node<T> *One = new Node<T> ( data , head->next ) ;
			if ( One->next == nullptr )
				tail = One ;
			head->next = One ;
			++_size ;
			return One ;
		}
		Node<T>* push_back ( const T &data ) {
			Node<T> *One = new Node<T> ( data ) ;
			tail->next = One ;
			tail = One ;
			++_size ;
			return One ;
		}
		bool push_front ( const int *arr , const int len ) {
			rep ( i , 0 , len )
				push_front ( arr[i] ) ;
			return true ;
		}
		bool push_back ( const int *arr , const int len ) {
			rep ( i , 0 , len )
				push_back ( arr[i] ) ;
			return true ;
		}
		bool pop_front () {
			Node<T> *it = head->next ;
			if ( !it ) return false ;
			if ( !it->next ) tail = head ;
			head->next = it->next ;
			delete it ; 
			it = nullptr ;
			--_size ;
			return true ;
		}
		bool pop_back () {
			Node<T> *it = head ;
			if ( !it->next ) return false ;
			while ( it->next != tail ) 
				it = it->next ;
			delete tail ;
			tail = nullptr ;
			it->next = nullptr ;
			--_size ;
			return true ;
		}
		bool empty () {
			return head == nullptr || head->next == nullptr ;
		}
		size_t size () const {
			return _size ;
		}
		void clear () {
			while ( _size ) pop_front () ;
		}
		My_iterator<T> begin () {
			return My_iterator<T> ( head->next ) ;
		}
		My_iterator<T> end () {
			// return My_iterator<T> ( nullptr ) ;
			return My_iterator<T> ( tail->next ) ;
		}
		void display () const {
			cout << "size  :  " << size () << "\t" ;
			Node<T> *it = head ;
			while ( it->next ) {
				cout << *it->next ;
				it = it->next ;
			}
			cout << endl ;
		}
	} ;
}

int main () {
	using YHL::List ;
	using YHL::My_iterator ;
	List<int> One ;
	One.push_back ( 1 ) ;
	One.push_back ( 0 ) ;
	One.push_back ( 2 ) ;
	One.push_back ( 2 ) ;
	One.display () ;
    
    int arr[4] = { 8 , 9 , 9 , 1 } ;
	One.push_front ( arr , 4 ) ;
	One.display () ;
	One.clear () ;

	int arr1[4] = { 8 , 9 , 9 , 1 } ;
	One.push_back ( arr1 , 4 ) ;
	One.display () ;

    rep ( i , 0 , 6 ) {
    	One.pop_front () ;
    	One.display () ;
    }

	int arr2[10] = { 12 , 5 , 3 , 20 , 9 , 11 , 7 , 4 , 2 , 1 } ;
	One.push_back ( arr2 , 10 ) ;
	One.display () ;

	cout << endl << "开始测试 auto 迭代器 :  " << endl << endl ;
	for ( const auto &it : One )
		cout << it << " " ;
	cout << endl << endl ;

	for ( auto it : One )    
		cout << it << " " ;
	cout << endl << endl ;

	for ( auto &it : One )    
		cout << it << " " ;
	cout << endl << endl ;

    cout << "开始测试 My_iterator 迭代器 :  " << endl << endl ;
	YHL::My_iterator<int> it = One.begin () ;
	for ( ; it != One.end () ; ++it )
		cout << *it << " " ;
	cout << endl << endl ;

	cout<< "开始测试 My_foreach 表达式 :  " << endl << endl ;
	YHL::My_foreach ( One.begin () , One.end () , 
		[] ( const YHL::Node<int> One) { cout << One << " " ; } ) ;
	return 0 ;
}


如有错误,敬请指正。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值