C++实现单链表(含头结点)

VS2005运行通过,如有问题,请各位大牛指正。

注意:单链表含有头结点

代码:

#include <iostream>
using namespace std;
template<class Type>
struct Node
{
	Type data;
	Node<Type> *next;
};
template<class Type>
class LinkList
{
protected:
	int len;//链表中结点个数
	Node<Type>* Head; //指向头结点
public:
	LinkList();//默认构造函数
	LinkList(const LinkList<Type>& otherList);//拷贝构造函数
	~LinkList();
	void createListForward();//头插法
	void createBackward();//尾插法
	void initList();//生成头结点,尾部设置为NULL
	bool isEmptyList();
	int length();
	void destoryList();
	void getFirstData(Type& firstItem);  
	void search(const Type searchItem);  
	void insertFirst(const Type newItem);  
	void insertLast(const Type newItem);  
	void insertBefore(const int pos,const Type newItem);  
	void insertAfter(const int pos,const Type newItem);  
	void deleteNode(const Type deleteItem);  
	void deleteNode(const int pos,Type& deleteItem);
	void reverse();
	const LinkList<Type>& operator=(const LinkList<Type>&otherList); 
	friend ostream& operator<< <>(ostream& cout,const LinkList<Type>& list);//注意 在<< 后加上 <>表明这是个函数模板
};

template<class Type>
LinkList<Type>::LinkList() //初始化时,只有一个头结点,有head指向
{
	Head = new Node<Type>;
	Head->next = NULL;
	len =0;
}

template<class Type>
LinkList<Type>::LinkList(const LinkList<Type>&otherList)
{
	//首先建立头结点
	Head = new Node<Type>;
	Head->next = NULL;
	len =0;
	Node<Type>* current = Head;//总是指向本链表的最后一个结点,待插入结点直接插入
	Node<Type>* otherListCurrent=otherList.Head->next;//otherListCurrent指向第一个元素
	while(otherListCurrent!=NULL)//拷贝的目标不为空
	{
		Node<Type>* newNode = new Node<Type>;
		newNode->data = otherListCurrent->data;
		newNode->next = current->next;
		current->next = newNode;
		current=current->next;
		otherListCurrent = otherListCurrent->next;
		len++;
	}

}

template<class Type>
const LinkList<Type>& LinkList<Type>::operator=(const LinkList<Type>&otherList)//赋值函数
{
	Node<Type>* current = Head;//current总是指向要插的位置
	Node<Type>* otherListCurrent=otherList.Head->next;//otherListCurrent指向第一个元素
	if (this!=&otherList)//避免自己给自己赋值
	{
		if (current->next!=NULL)
		{
			initList();//自己有结点,先销毁
		}
		while(otherListCurrent!=NULL)
		{
			Node<Type>* newNode = new Node<Type>;
			newNode->data = otherListCurrent->data;
			newNode->next = current->next;
			current->next = newNode;
			current=current->next;
			otherListCurrent = otherListCurrent->next;
			len++;
		}

	}
	return *this;//为了连续赋值
}

template<class Type>
LinkList<Type>::~LinkList()
{
	destoryList();
}

template<class Type>
void LinkList<Type>::createListForward()//头插法
{
	Node<Type>* newNode;
	cout<<"输入链表长度:"<<endl;
	cin>>len;
	for (int i=0;i<len;i++)
	{
		newNode = new Node<Type>;
		cout<<"输入元素:"<<endl;
		cin>>newNode->data;
		newNode->next=Head->next;
		Head->next = newNode; //每插入一个结点,都是要把它放在第一个结点的位置
	}
}

template<class Type>
void LinkList<Type>::createBackward()//尾插法
{
	Node<Type>* current = Head;//current指向头结点
	Node<Type>* newNode;
	cout<<"输入链表长度:"<<endl;
	cin>>len;
	for (int i=0;i<len;i++)
	{
		newNode = new Node<Type>;
		cout<<"输入元素:"<<endl;
		cin>>newNode->data;
		newNode->next = current->next;
		current->next = newNode;
		current=current->next;
	}
}

template<class Type>
void LinkList<Type>::initList() //所有结点都销毁
{
	destoryList();
	Head = new Node<Type>;
	Head->next = NULL;
	len =0;
}

template<class Type>
bool LinkList<Type>::isEmptyList()
{
	if (Head->next==NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}

template<class Type>
int LinkList<Type>::length()
{
	return len;
}

template<class Type>
void LinkList<Type>::destoryList()//销毁包括头结点
{
	Node<Type>* current;
	while(Head!=NULL)
	{
		current = Head;
		Head = current->next;
		delete current;
	}
	Head=NULL;
	len=0;
}

template<class Type>
void LinkList<Type>::getFirstData(Type& firstItem)
{
	if (!isEmptyList())
	{
		firstItem = (Head->next)->data;
	}
	else
	{
		cout<<"链表为空!"<<endl;
	}
}

template<class Type>
void LinkList<Type>::search(const Type searchItem)
{
	Node<Type>* current;
	if (isEmptyList())
	{
		cout<<"List is Empty"<<endl;
	}
	else
	{
		current = Head->next;//越过头结点,指向第一个元素
		while (current!=NULL && current->data!=searchElement)
		{
			current = current->next;
		}
		if (current!=NULL)
		{
			cout<<"Item is Found in the list"<<endl;
		}
		else
		{
			cout<<"Item is not Found in the list"<<endl;
		}
	}
}

template<class Type>
void LinkList<Type>::insertFirst(const Type newItem)
{
	Node<Type> *newNode = new Node<Type>;
	newNode->data = newItem;
	newNode->next = Head->next;
	Head->next = newNode;
}

template<class Type>
void LinkList<Type>::insertLast(const Type newItem)
{
	Node<Type> *newNode = new Node<Type>;
	newNode->data = newItem;
	Node<Type>* current = Head;  //寻找位置
	while (current->next != NULL)  
	{  
		current = current ->next;  
	}  
	newNode->next = current->next;
	current->next = newNode;
}

template<class Type>
void LinkList<Type>::insertBefore(const int pos,const Type newItem)
{
	int i=1;  
	Node<Type>* current = Head->next;  
	if (pos<1 || pos>len)  
	{  
		cout<<"插入位置不正确!"<<endl;  
		return;  
	}  
	Node<Type>* newNode = new Node<Type>;  
	newNode->data = newItem;  
	if (1==pos)  
	{  
		newNode->next = Head->next;  
		Head->next = newNode;  
	}  
	else  
	{  
		while(i<pos-1)  
		{  
			current = current->next;  
			i++;  
		}  
		newNode->next = current->next;  
		current->next = newNode;  
	}  
	len++; 
}

template<class Type>
void LinkList<Type>::insertAfter(const int pos,const Type newItem)
{
	int i=1;  
	Node<Type>* current = Head->next;//current指向第一个位置,和i配合,指向第i个结点   
	if (pos<1 || pos>len)  
	{  
		cout<<"插入位置不正确!"<<endl;  
		return;  
	}  
	Node<Type>* newNode = new Node<Type>;  
	newNode->data = newItem;  
	while(i<pos)  
	{  
		current = current->next;  
		i++;  
	}  
	newNode->next = current->next;  
	current->next = newNode;  
	len++; 
}

template<class Type>
void LinkList<Type>::deleteNode(const Type deleteItem)
{
	Node<Type>* current=Head -> next;
	Node<Type>* trailCurrent = Head;//指向current前面的结点
	if (isEmptyList())
	{
		cout<<"List is Empty"<<endl;
	}
	else
	{
		while (current!=NULL && current->data!=deleteItem)
		{
			trailCurrent = current;
			current=current->next;
		}
		if (current==NULL)
		{
			cout<<deleteItem<<" is not Found in the list"<<endl;
		}
		else
		{
			trailCurrent->next = current->next;
			delete current;
			cout<<deleteItem<<" is delete in the list"<<endl;
			len--;
		}
	}
}

template<class Type>
void LinkList<Type>::deleteNode(const int pos,Type& deleteItem)
{
	int i=1;  
	Node<Type>* current = Head;  
	Node<Type>* temp;  
	if (pos<1 || pos>len)  
	{  
		cout<<"删除位置不正确!"<<endl;  
		deleteItem = -1;  
		return;  
	}  
	while(i<pos)  
	{  
		current = current->next;  
		i++;  
	}  
	temp = current->next;  
	current->next = temp->next;  
	deleteItem = temp->data;  
	delete temp;  
	len--;  

}

template<class Type>
void LinkList<Type>::reverse()
{
	Node<Type>* current = Head->next;
	Head->next=NULL;
	if (current==NULL)
	{
		cout<<"链表为空!"<<endl;
	}
	else
	{
		while (current!=NULL)
		{
			Node<Type>* nextCurrent = current->next;
			current->next = Head->next;
			Head->next=current;
			current = nextCurrent;
		}
	}
}

template<class Type>
ostream& operator<< (ostream& cout,const LinkList<Type>& list)
{
	Node<Type>*current=list.Head->next; //有头结点,这是current指向第一个结点
	if (current!=NULL)
	{
		while (current!=NULL)
		{
			cout<<current->data<<endl;
			current=current->next;
		}
	}
	else
	{
		cout<<"链表为空!"<<endl;
	}
	return cout;
}
void main()
{
	LinkList<int> list1;
	list1.createBackward();
	list1.reverse();
	cout<<list1;
	system("pause");
}

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值