单链表(List)的模板实现

//Filename :List.h
#ifndef LIST_H
#define LIST_H

template <class T>
struct LinkNode{ //节点结构体定义
	T data;
	LinkNode<T> *link;
	LinkNode(LinkNode<T> * ptr= NULL)
	{
		link = ptr;
	}
	LinkNode(const T &x,LinkNode<T> *ptr = NULL)
	{
		data = x;
		link = ptr;
	}
};

template<class T>
class List
{
public:
	List(){first  = new LinkNode<T>;};              //构造函数
	List(const T&x){first = new LinkNode<T>(x);}    //构造函数
	List(List<T> & L);                              //复制构造函数
	~List(){makeEmpty();};                           //析构函数
	void makeEmpty();                               //链表置为空
	int Length();                              //计算链表长度
	LinkNode<T> * getHead(){return first;}     //返回头结点地址
	LinkNode<T> * Search(T x);                      //搜索含数据x的元素
	LinkNode<T> * Locate(int i);                    //搜索第i个元素的地址
	bool getData(int i,T &x);                  //取出第i个元素的值
	void setData(int i,T &x);                       //设置第i个元素的值
	bool Insert(int i,T &x);                        //在第i个元素后插入x
	bool Remove(int i,T &x);                        //删除第i个元素的值
	bool IsEmpty(){return first->link == NULL ? true:false;}//判断表空?
	bool IsFull(){return false;}               //判断表满

protected:
	LinkNode<T> *first;
};

//Filename:List.cpp
#include "List.h"

template<class T>
List<T>::List(List<T>& L){
	//复制构造函数
	T value;
	LinkNode<T> *srcptr = L.getHead();
	LinkNode<T> *destptr = first = new LinkNode<T>;
	while(srcptr->link!=NULL)
	{
		value = srcptr->link->data;
		destptr ->link = new LinkNode<T>(value);
		destptr = destptr ->link;
		srcptr = srcptr->link;
	}
	destptr->link = NULL;
}

template<class T>
void List<T>::makeEmpty(){
	//链表置为空
	LinkNode<T> * q;
	while(first->link !=NULL)
	{
		q = first->link;
		first->link = q->link;
		delete q;
	}
}

template<class T>
int List<T>::Length(){
	//返回链表长度
	LinkNode<T> *p = first->link;
	int count = 0;
	while(p!=NULL)
	{
		count++;
		p = p->link;
	}
	return count;
}

template<class T>
LinkNode<T> * List<T>::Search(T x){
	//寻找数据为x的节点,成功返回节点地址,失败返回NULL
	LinkNode<T> *current = first->link;
	while(current!=NULL)
	{
		if(current->data == x )break;
		else current  = current->link;
	}
	return current;
}

template<class T>
LinkNode<T> * List<T>::Locate(int i){
	//定位第i个元素,若i<0或超出链表中节点数目返回NULL
	if(i<0) return NULL;
	LinkNode<T> *current = first;
	int k = 0;
	while(current!=NULL && k<i)//若i为0,返回的是first
	{
		current = current->link;
		k++;
	}
	return current;
}

template<class T>
bool List<T>::getData(int i,T& x){
	//获得第i个元素的值,存在x中
	if(i<=0)return NULL;
	LinkNode<T> *current = Locate(i);
	if(current==NULL)return false;
	else{
		x = current->data;
		return true;
	}
}

template<class T>
void List<T>::setData(int i,T& x){
	//设置第i个元素的值为x
	if(i<=0)return ;
	LinkNode<T> *current = Locate(i);
	if(current == NULL) return;
	else {current->data = x;}
}

template<class T>
bool List<T>::Insert(int i,T& x){
	//将x插入第i个元素之后
	LinkNode<T> *current = Locate(i);
	if(current==NULL) return false; //插入失败
	LinkNode<T> *newNode = new LinkNode<T>(x);
	if(newNode ==NULL){cerr<<"存储分配错误!"<<endl;exit(1);}
	newNode->link = current->link;
	current->link = newNode;
	return true;
}

template<class T>
bool List<T>::Remove(int i,T& x){
	//删除第i个元素的值,其值暂存于
	LinkNode<T> *current = Locate(i-1); //要修改删除节点前一个节点的指针,故定位至i-1
	if(current ==NULL || current->link ==NULL)return false;
	LinkNode<T> *del = current->link;
	current->link = del->link;
	x = del->data;
	delete del;
	return true;
}

#endif // LIST_H

测试程序:
#include <iostream>
#include "List.h"
using namespace std;

int main()
{
	List<int> list(20);

	cout<<"The Length is : "<< list.Length()<<endl;

	for(int i=0;i<20;i++)//必须得从第0个开始插入
		list.Insert(i,i);
	cout<<"\nAfter Insert, The length is: "<<list.Length()<<endl;
	cout<<"The values are:\n";
	for(int i = 1;i<=list.Length();i++)
	{
		int temp ;
		list.getData(i,temp);
		cout<<temp<<" ";
	}

	for(int i = 1;i<=list.Length();i++)
	{
		int temp = i*i;
		list.setData(i,temp);
	}
	cout<<"\n\nAfter SetData, The length is: "<<list.Length()<<endl;
	cout<<"The values are:\n";
	for(int i = 1;i<=list.Length();i++)
	{
		int temp ;
		list.getData(i,temp);
		cout<<temp<<" ";
	}

	for(int i = list.Length();i>0;i--)
	{
		int temp ;
		list.Remove(i,temp);
	}
	cout<<"\n\nAfter Remove, The length is : "<<list.Length()<<endl;
	system("pause");
	return 0;

}
测试结果:

单链表(List)的模板实现_第1张图片



你可能感兴趣的:(单链表)