《CPlusPlusPrimer》第五章编程源码——list类的简单实现

本文档提供了一份C++ Primer第五章中关于list类的简易实现,包括list.h、list.cpp、list_item.h及test.cpp四个文件。通过这些源码,读者可以深入理解list的内部结构和插入、迭代器、删除等操作的实现细节。
摘要由CSDN通过智能技术生成

list.h

#ifndef _CPlusPlusPrimer_chapter5_list
#define _CPlusPlusPrimer_chapter5_list

#include <iostream>

namespace Icanth_CPlusPlusPrimer
{
	template <typename elemType>
	class list_item;

	template <typename elemType>
	class list
	{
	public:
		list()
			:_at_front(0), _at_end(0), _current (0), _size(0) {}
		
		list( const list& ); // 拷贝函数
		list& operator=( const list& );
		~list() { remove_all(); }

		void insert( list_item<elemType> *ptr, elemType value ); // 如果ptr=0,则插入最前~否则,将value插入ptr之后
		void insert_end( elemType value );
		void insert_front( elemType value );
		void insert_all( const list &rhs );
		int remove( elemType value ); // 返回删除个数
		void remove_all();
		void remove_front();

		list_item<elemType> *find(elemType value );
		list_item<elemType> *next_iter();
		list_item<elemType> *init_iter( list_item<elemType> *it = 0);

		void display( std::ostream &os = cout);
		void reverse();
		int size() { return _size; }
	private:
		void bump_up_size() { ++_size; }
		void bump_down_size() { --_size; }

		list_item<elemType> *_at_front;
		list_item<elemType> *_at_end;
		list_item<elemType> *_current;

		int _size;
	};
}

#endif


list.cpp

#include "list.h"

#include "list_item.h"

namespace ICP = Icanth_CPlusPlusPrimer;

template <typename elemType>
ICP::list<elemType>::list( const list& rhs )
	: _at_front(0), _at_end(0), _current (0), _size(0)
{
	list_item<elemType> *pt = rhs._at_front;
	while( pt )
	{
		insert_front( pt->value() );
		pt = pt->next();
	}
}

template <typename elemType>
ICP::list<elemType>& ICP::list<elemType>::operator=( const list<elemType>& rhs)
{
	remove_all();
	insert_all(rhs);
	
	return (*this);
}

template <typename elemType>
void ICP::list<elemType>::insert( list_item<elemType> *ptr, elemType value )
{
	if(!ptr)
	{
		insert_front( value );
	}
	else
	{
		new list_item<elemType>( value, ptr );
		bump_up_size();
	}
}

template <typename elemType>
void ICP::list<elemType>::insert_front( elemType value )
{
	list_item<elemType> *ptr = new list_item<elemType>( value );
	if(!_at_front)
	{ // 如果现在插入首个元素
		_at_front = _at_end = ptr;
	}
	else
	{
		ptr->next( _at_front );
		_at_front = ptr;
	}
	bump_up_size();
}

template <typename elemType>
void ICP::list<elemType>::insert_end( elemType value )
{ 
	if(!_at_end)
	{
		_at_end = _at_front = new list_item<elemType>( value );
	}
	else
	{
		_at_end = new list_item<elemType>( value, _at_end );
	}

	bump_up_size();
}

template <typename elemType>
void ICP::list<elemType>::insert_all( const list<elemType> &rhs )
{
	list_item<elemType> *pt = rhs._at_front;
	while(pt)
	{
		insert_end( pt->value() );
		pt = pt->next();
	}
}

template <typename elemType>
int ICP::list<elemType>::remove( elemType value )
{
	int rmv_cnt = 0; // 定义移除value的个数
	list_item<elemType> *plist = _at_front;
	
	// 移除最前面的value,以防止_at_front指针失效
	while(plist && plist->value() == value)
	{
		plist = plist->next();
		remove_front();
		++rmv_cnt;
	}

	if(!plist)
		return rmv_cnt;

	list_item<elemType> *prev = plist;
	plist = plist->next();
	while( plist )
	{
		if(plist->value() == value)
		{
			if( plist == _current )
				_current = plist->next();

			prev->next( plist->next() );
			delete plist;
			++rmv_cnt;
			bump_down_size();
			plist = prev->next();
			if( !plist )
			{
				_at_end = prev;
				return rmv_cnt;
			}
		}
		else
		{
			prev = plist;
			plist = plist->next();
		}
	}
	return rmv_cnt;
}

template <typename elemType>
void ICP::list<elemType>::remove_front()
{
	if(_at_front)
	{
		if(_current ==  _at_front) // 修正_current指针
			_current = _current->next();

		list_item<elemType> * x = _at_front;

		if(_at_front == _at_end)
		{
			_at_front = _at_end = 0;
		}
		else
		{
			_at_front = _at_front->next();
		}
		delete x;
		bump_down_size();
	}
}

template <typename elemType>
void ICP::list<elemType>::remove_all()
{
	while(_at_front)
	{
		remove_front();		
	}
	_current = 0;
}

template <typename elemType>
ICP::list_item<elemType>* ICP::list<elemType>::find( elemType value )
{
	list_item<elemType>* ptr = _at_front;
	while( ptr )
	{
		if( ptr->value() == value )
			break;
		ptr = ptr->next();
	}

	return ptr;
}

template <typename elemType>
ICP::list_item<elemType>* ICP::list<elemType>::init_iter( list_item<elemType> *it)
{
	if( it == 0)
	{
		_current = _at_front;
	}
	else
	{
		_current = it;
	}

	return _current;
}

template <typename elemType>
ICP::list_item<elemType>* ICP::list<elemType>::next_iter()
{
	if( ! _current )
	{
		return 0;
	}
	else
	{
		list_item<elemType>* tmp  = _current;
		_current = _current->next();

		return tmp;
	}
}

template <typename elemType>
void ICP::list<elemType>::reverse()
{
	list_item<elemType> *prev = 0;
	list_item<elemType> *ptr = _at_front;

	_at_front = _at_end;
	_at_end = ptr;
	while( ptr )
	{
		list_item<elemType> *tmp  = ptr->next();
		ptr->next(prev);
		prev = ptr;
		ptr = tmp;
	}
}

template <typename elemType>
void ICP::list<elemType>::display( std::ostream &os = cout)
{
	os << "( " << size() << " ) ( ";
	list_item<elemType> *ptr = _at_front;

	for( ; ptr ; ptr = ptr->next() )
	{
		os << ptr->value() << " ";
	}
	os << " )" << endl;
}


list_item.h

#ifndef _CPlusPlusPrimer_chapter5_list_item
#define _CPlusPlusPrimer_chapter5_list_item

namespace Icanth_CPlusPlusPrimer
{
	template<typename elemType> // typename可与class 互换
	class list_item
	{
	public:
		list_item(elemType value, list_item *item = 0): _value(value)
		{
			if( ! item )
			{
				_next = 0;
			}
			else
			{
				_next = item->_next;
				item->_next = this;
			}
		}

		elemType value() 
		{
			return _value;
		}

		void value(elemType value)
		{
			_value = value;
		}

		list_item* next()
		{
			return _next;
		}

		void next(list_item* link)
		{
			_next = link;
		}

	private:
		elemType _value;
		list_item *_next;
	};
}

#endif


test.cpp

#include "list.cpp"

namespace ICP = Icanth_CPlusPlusPrimer;

using namespace std;

int main()
{
	using ICP::list;

	list<int> int_list;
	
	int_list.insert(0, 1);
	int_list.insert(0, 2);
	int_list.insert(0, 3);
	int_list.insert_end(4);
	
	cout << "after initializing hte int_list:\n";
	int_list.display();

	int_list.remove(2);
	cout << "\nafter removing the value 2:\n";
	int_list.display();
	
	using ICP::list_item;
	if( list_item<int>* elem = int_list.find(3) )
	{
		int_list.insert( elem, 8 );
		cout << "\nafter finding the elem 3 and inserting 8 after it:\n";
		int_list.display();
	}

	cout << "\nthe next is displaying the elems using iterator:\n";
	for( list_item<int>* iter = int_list.init_iter(); iter ; iter = int_list.next_iter() )
	{
		cout << iter->value() << " ";
	}
	cout << endl;
	
	cout << "\nafter reserving the int_list:\n";
	int_list.reverse();
	int_list.display();

	list<int> int_list2(int_list);
	int_list2.insert_all( int_list );
	cout << "\nafter copying the int_list1 and inserting the int_list1:\n";
	int_list2.display();

	int_list.remove_all();
	cout << "\nafter removing all elements:\n";
	int_list.display();

	int_list = int_list2;
	cout << "\nafter operate=(const list<elemtype> &):\n";
	int_list.display();

	return 0;
}


运行截图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值