数据结构C++模板实现之----------------单向链表

//欢迎大家拍砖头给意见啊,大家的批评是我学习的动力,谢谢啦

#ifndef  _List_H_
#define  _List_H_

#include "stdafx.h"
template<class T>
class list;

template<class ElemType> class listNode{
public:
	listNode(ElemType data = 0):_data(data),_next(NULL){}
	~listNode(){}
	friend class list<ElemType>;
	ElemType _data;
	listNode* _next;
};

template<class T>
class list{
//公共接口
public:
	//注意_pHead,_pTail初始化顺序,必须和声明顺序一样
	list():_length(0),_pHead(new listNode<T>),_pTail(_pHead){}
	~list(){delete _pHead;delete _pTail;}
	bool IsEmpty(){ return _length>0?false:true; }
	listNode<T>* GetHead(){return _pHead;}
	listNode<T>* GetTail(){return _pTail;}
	void Push_Back(const T& data);
	//插入位置以1为最开始
	bool Insert(const T& data,const int index);
	listNode<T>* Find(const T& e);
	bool Delete(const int index);
	void Print();
	unsigned int GetLength(){return _length;}
	void Destroy();
//数据成员
private:
	unsigned int _length;
	listNode<T> *_pHead,*_pTail;//_pHead为链表地址,_pTail始终指向最后一个结点
private:
	list(const list<T>& rhs){}
	list<T>& operator=(){}
};



template<class T>
void list<T>::Push_Back(const T& data)
{
	listNode<T>* p = new listNode<T>(data);
	if (_length == 0)
	{
		p->_next = NULL;
		_pHead->_next = p;
		_pTail = p;
		_length++;
	}else{
		_pTail->_next = p;
		_pTail = p;
		_length++;
	}
	
}

template<class T>
bool list<T>::Insert(const T& data,const int index)
{
	//不合理的插入位置
	if (index>_length+1||index<=0)
	{
		return false;
	}
	listNode<T>* p = new listNode<T>(data);
	listNode<T>* q = _pHead;
	//插入第一个位置 则需要改变_pHead
	if (_length == 0)
	{
		//空链时的插入同时需要改变头尾节点
		_pHead->_next = p;
		_pTail = p;
		_length++;
		return true;
	}else if (index == 1)
	{
		//插入到第一位置改变头结点
		p->_next = _pHead->_next;
		_pHead->_next = p;
		_length++;
		return true;
	}else if (index == _length+1)
	{
		//插入到最后位置改变尾结点
		_pTail->_next = p;
		_pTail = p;
		_length++;
		return true;
	}

	//其他情况下头尾结点不需要改变
	//找到要插入位置的上一个节点
	int k = 1;
	while (k<index)
	{
		q = q->_next;
		k++;
	}
	p->_next = q->_next ;
	q->_next = p;
	_length++;
	return true;
}

template<class T>
listNode<T>* list<T>::Find(const T& e)
{
	//避免只有头结点而且刚好查找0的情况
	if(_length == 0)  return NULL;
	listNode<T>* p = _pHead->_next;
	while (p->_data!=e&&p->_next!=NULL)
	{
		p = p->_next;
	}
	return p;
}

template<class T>
bool list<T>::Delete(const int index)
{
	//删除节点位置不存在时
	if (index>_length||_length==0||index<=0) return false;
	listNode<T>* q = _pHead;
	listNode<T>* p;
	//只有一个节点并要删除时 需要修改头尾指针
	if (_length == 1&&index==1) 
	{
		p = q->_next;
		_pHead->_next = NULL;
		_pTail = _pHead ; 
		delete p;
		_length--;
		return true;
	}
	int k = 1;
	//找到要插入位置的上一个节点
	while (k<index)
	{
		q = q->_next;
		k++;
	}
	p = q->_next ; //p为需要删除的节点
	
	if (q == _pHead)
	{
		//删除节点为第一个节点时 需要修改头指针
		_pHead->_next = p->_next;
	}
	else if (p->_next == NULL)
	{
		//删除的是最后一个节点,则需要修改_pTail
		q->_next = NULL;
		_pTail = q;
	}
	else
	{
		q->_next = q->_next->_next;
	}
	delete p;
	_length--;
	return true;
}

template<class T>
void list<T>::Destroy()
{
	if(!_length) return;
	listNode<T>* p = _pHead->_next;
	listNode<T>* q ;
	while (p)
	{
		q = p;
		p = p->_next;
		delete q;
	}
	_pHead->_next = NULL;
	_pTail = _pHead;
	_length = 0;
	return;
}

template<class T>
void list<T>::Print()
{
	if (!_length)
	{
		std::cout<<"The list is null !"<<std::endl;
	}
	listNode<T>* p = _pHead->_next;
	int i = 1;
	while (p!=NULL)
	{
		std::cout<<"The "<<i<<" node's data is "<<p->_data<<std::endl;
		p = p->_next;
		i++;
	}
}

#endif

posted on 2011-02-28 14:07  Pro.Charm  阅读(767)  评论(4编辑  收藏  举报

导航