⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myllist.h

📁 gamecode 很不错的小游戏源代码
💻 H
字号:
/////////////////////////////////////////////
//链表类及其实现
//作者:曾铮
//时间:2003年
////////////////////////////////////////////////
#ifndef Myllist_h_
#define Myllist_h_

#include "iostream.h"
#include "stdlib.h"

#define OK 1
//#define ERROR -1
#define OVERFLOW -2

//typedef int elemtype;
typedef int status;

//template<class elemtype> class linklist;

template<class elemtype>
struct node//结点结构
{
	elemtype data;//结点数据
	node *next;//向后指针
};

template<class elemtype>
class linklist
{
private:

	node<elemtype> *head;//头结点	
	node<elemtype> *tail;//尾结点
	int length;//链表长度

public:
	linklist();
	status clearlist();//清空链表
	status listempty();//判断链表是否为空
	int listlength();//取得链表长度
	status locateelem(elemtype e);//判断结点是否在链表中,是则返回结点号,否则返回-1
	status getelem(int i,elemtype &e);//取得i号结点
	status getelem(int i,elemtype *e);//取得i号结点
	status delelem(int i);//删除i号结点
	status insert(elemtype e,int i);//在i号插入结点
	status insert(elemtype *e,int i);//在i号插入结点
	virtual ~linklist();
};

//Member function 
template<class elemtype>
linklist<elemtype>::linklist()
{
	this->length=0;
	this->head=new node<elemtype>;
	this->tail=new node<elemtype>;
	this->head->next=this->tail;
	this->tail=NULL;

}

template<class elemtype>
linklist<elemtype>::~linklist()
{
	node<elemtype> *next;
	while(head)
	{
		next=head->next;
		delete head;
		head=next;
	}
}

template<class elemtype>
status linklist<elemtype>::listempty ()
{
	if(this->head->next !=this->tail)
	{
		return FALSE;
	}
	return TRUE;	
}

template<class elemtype>
status linklist<elemtype>::getelem (int i,elemtype &e)
{
	if(i<1||i>this->length)
		return ERROR;
	node<elemtype> *temp;
	temp=new node<elemtype>;
	temp=this->head;
	int k;
	for(k=1;k<=i;k++)
		temp=temp->next ;
	e=temp->data ;
	temp=NULL;
	delete temp;
	return TRUE;
}

template<class elemtype>
status linklist<elemtype>::getelem (int i,elemtype *e)
{
	if(i<1||i>this->length)
		return ERROR;
	node<elemtype> *temp;
	temp=new node<elemtype>;
	temp=this->head;
	int k;
	for(k=1;k<=i;k++)
		temp=temp->next ;
	*e=temp->data;
	temp=NULL;
	delete temp;
	return TRUE;
}

template<class elemtype>
status linklist<elemtype>::insert (elemtype e,int i)
{
	if(i<1)
		return ERROR;
	node<elemtype> *temp;
	temp=new node<elemtype>;
	temp=this->head;
	int k=1;
	while(k<=i-1)
	{
		temp=temp->next ;
		k++;
	}
	node<elemtype> *elem;
	elem=new node<elemtype>;
	elem->data =e;
	elem->next=temp->next ;
	temp->next =elem;
	this->length++;
	return TRUE;
}

template<class elemtype>
status linklist<elemtype>::insert (elemtype *e,int i)
{
	if(i<1)
		return ERROR;
	int k=1;
	node<elemtype> *temp;
	temp=new node<elemtype>;
	while(k<=i-1)
	{
		temp=temp->next;
		k++;
	}
	node<elemtype> *elem;
	elem=new node<elemtype>;
	elem->data =*e;//取e的值
	elem->next=temp->next ;
	temp->next =elem;
	length++;
	return TRUE;	
}

template<class elemtype>
status linklist<elemtype>::delelem(int i)
{
	if(i<1 || i> this->length)
		return ERROR;
	node<elemtype> *temp;
	temp=new node<elemtype>;
	temp=this->head;
	int k=1;
	while(k<=i-1)
	{
		temp=temp->next;
		k++;
	}
	//找到需要删除的结点的前一个结点:
	node<elemtype> *delp;
	delp=new node<elemtype>;
	delp=temp->next;//需要删除的指针复制到临时位置
	temp->next=delp->next;//修复链接
	length--;
	delp=NULL;
	delete delp;//删除结点
	return TRUE;
}

template<class elemtype>
int linklist<elemtype>::listlength ()
{
	if(this->head->next ==this->tail)
		return 0;
	return this->length;
}

template<class elemtype>
status linklist<elemtype>::clearlist ()
{
	if(!this->head)
		return ERROR;
	node<elemtype> *next;
	while(head)
	{
		next=head->next;
		delete head;
		head=next;
	}
	this->length=0;
	this->head=new node<elemtype>;
	this->tail=new node<elemtype>;
	this->head->next=tail;
	this->tail=NULL;
	return OK;
}

template<class elemtype>
status linklist<elemtype>::locateelem (elemtype e)
{
	if(this->head->next ==this->tail)
		return FALSE;
	int i;
	i=1;
	node<elemtype> *temp;
	temp=new node<elemtype>;
	temp=this->head->next;
	while(temp!=this->tail)
	{
		if(temp->data==e)
		{
			return i; 
		}
		temp=temp->next;
		i++;
	}
	temp=NULL;
	delete temp;
	return -1;
}

//////////////////////////////////////////////////////////////////////////////////////
//专门化
//////////////////////////////////////////////////////////////////////////////////////

template<>struct node<class elemtype*>//结点结构
{
	elemtype* data;//结点数据
	node *next;//向后指针
};

template<>class linklist<class elemtype*>
{
private:
	node<elemtype*> *head;//头结点	
	node<elemtype*> *tail;//尾结点
	int length;//链表长度

public:
	linklist();
	status clearlist();//清空链表
	status listempty();//判断链表是否为空
	int listlength();//取得链表长度
	status locateelem(elemtype *e);//判断结点是否在链表中,是则返回结点号,否则返回-1
	status getelem(int i,elemtype *e);//取得i号结点
	status delelem(int i);//删除i号结点
	status insert(elemtype *e,int i);//在i号插入结点
	virtual ~linklist();
};

//Member function 
template<>linklist<elemtype*>::linklist()
{
	this->length=0;
	this->head=new node<elemtype*>;
	this->tail=new node<elemtype*>;
	this->head->next=this->tail;
	this->tail=NULL;

}

template<>linklist<elemtype*>::~linklist()
{
	node<elemtype*> *next;
	while(this->head)
	{
		next=this->head->next;
		delete this->head;
		this->head=next;
	}
}

template<>status linklist<elemtype*>::listempty ()
{
	if(this->head->next !=this->tail)
	{
		return FALSE;
	}
	return TRUE;	
}

template<>status linklist<elemtype*>::getelem (int i,elemtype *e)
{
	if(i<1||i>this->length)
		return ERROR;
	node<elemtype*> *temp;
	temp=new node<elemtype*>;
	temp=this->head;
	int k;
	for(k=1;k<=i;k++)
		temp=temp->next ;
	e=temp->data;
	temp=NULL;
	delete temp;
	return TRUE;
}

template<>status linklist<elemtype*>::insert (elemtype *e,int i)
{
	if(i<1)
		return ERROR;
	int k=1;
	node<elemtype*> *temp;
	temp=new node<elemtype*>;
	while(k<=i-1)
	{
		temp=temp->next;
		k++;
	}
	node<elemtype*> *elem;
	elem=new node<elemtype*>;
	elem->data =e;//取e的值
	elem->next=temp->next ;
	temp->next =elem;
	this->length++;
	return TRUE;	
}
template<>status linklist<elemtype*>::delelem(int i)
{
	if(i<1 || i> this->length)
		return ERROR;
	node<elemtype*> *temp;
	temp=new node<elemtype*>;
	temp=this->head;
	int k=1;
	while(k<=i-1)
	{
		temp=temp->next;
		k++;
	}
	//找到需要删除的结点的前一个结点:
	node<elemtype*> *delp;
	delp=new node<elemtype*>;
	delp=temp->next;//需要删除的指针复制到临时位置
	temp->next=delp->next;//修复链接
	this->length--;
	delp=NULL;
	delete delp;//删除结点
	return TRUE;
}

template<>int linklist<elemtype*>::listlength ()
{
	if(this->head->next ==this->tail)
		return 0;
	return this->length;
}

template<>status linklist<elemtype*>::clearlist ()
{
	if(!this->head)
		return ERROR;
	node<elemtype*> *next;
	while(head)
	{
		next=head->next;
		delete head;
		head=next;
	}
	this->length=0;
	this->head=new node<elemtype*>;
	this->tail=new node<elemtype*>;
	this->head->next=tail;
	this->tail=NULL;
	return OK;
}

template<>status linklist<elemtype*>::locateelem (elemtype *e)
{
	if(this->head->next ==this->tail)
		return FALSE;
	int i;
	i=1;
	node<elemtype*> *temp;
	temp=new node<elemtype*>;
	temp=this->head->next;
	while(temp!=this->tail)
	{
		if(temp->data==e)
		{
			return i; 
		}
		temp=temp->next;
		i++;
	}
	temp=NULL;
	delete temp;
	return -1;
}

#endif


	




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -