C++实现循环链表

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

注意:循环链表含有头结点

#include 
using namespace std;
template
struct Node
{
	Type data;
	Node *next;
};
template
class Circlist
{
protected:
	int len;//链表中结点个数
	Node* Head; //指向头结点
public:
	Circlist();//默认构造函数
	Circlist(const Circlist& otherList);//拷贝构造函数
	~Circlist();
	void createListForward();//头插法
	void createBackward();//尾插法
	void initList();//生成头结点,尾部设置为NULL
	bool isEmptyList();
	void printList();
	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 Circlist& operator=(const Circlist&otherList);
};

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

template
Circlist::Circlist(const Circlist&otherList)
{
	Head = new Node;
	Head->next = Head;
	len =0;
	Node* current = Head;
	Node* otherListCurrent = otherList.Head->next;//otherListCurrent指向第一个元素
	if (otherList.Head->next != otherList.Head)//被拷贝的表不是空表
	{
		while(otherListCurrent->next!=otherList.Head)//拷贝的目标不为空
		{
			Node* newNode = new Node;
			newNode->data = otherListCurrent->data;
			newNode->next = current->next;
			current->next = newNode;
			current=current->next;
			otherListCurrent = otherListCurrent->next;
			len++;
		}
		if (otherListCurrent->next==otherList.Head)
		{
			Node* newNode = new Node;
			newNode->data = otherListCurrent->data;
			newNode->next = current->next;
			current->next = newNode;
			len++;
		}
	}
}

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

template
Circlist::~Circlist()
{
	destoryList();
}

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

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

template
void Circlist::initList() //只剩下头结点,和指针设置
{
	destoryList();//所有结点都销毁,在重建头结点
	Head = new Node;
	Head->next = Head;
	len =0;
}

template
bool Circlist::isEmptyList()
{
	if (Head->next==Head)
	{
		return true;
	}
	else
	{
		return false;
	}
}

template
void Circlist::printList()
{
	Node*current=Head->next;
	while (current!=Head)
	{
		cout<data<next;
	}	
}

template
int Circlist::length()
{
	return len;
}

template
void Circlist::destoryList()//销毁包括头结点
{
	Node* current;
	Node* temp;
	if (Head!=NULL)//析构函数也要调这个函数,因此要判断头是不是为空,为空表示已经释放
	{
		temp = Head;
		current = Head->next;
		while(current!=Head)
		{
			delete temp;
			temp = current;
			current = current->next;
		}
		delete temp;
		len=0;
	}
}

template
void Circlist::getFirstData(Type& firstItem)
{
	if (!isEmptyList())
	{
		firstItem = (Head->next)->data;
	}
	else
	{
		cout<<"链表为空!"<
void Circlist::search(Type searchItem)
{
	Node* current;
	if (isEmptyList())
	{
		cout<<"List is Empty"<next;//越过头结点,指向第一个元素
		while (current!=Head && current->data!=searchItem)
		{
			current = current->next;
		}
		if (current!=Head)
		{
			cout<
void Circlist::insertFirst(const Type newItem)
{
	Node *newNode = new Node;
	newNode->data = newItem;
	newNode->next = Head->next;
	Head->next = newNode;
	len++;
}

template
void Circlist::insertLast(const Type newItem)
{
	Node *newNode = new Node;
	newNode->data = newItem;
	//寻找位置
	Node* current = Head;
	while (current->next != Head)
	{
		current = current ->next;
	}
	//此时current指向结点的尾部,就是应该插入的位置
	newNode->next = current->next;
	current->next = newNode;
	len++;
}

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

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



template
void Circlist::deleteNode(const Type deleteItem)
{
	Node* current=Head -> next;
	Node* trailCurrent = Head;//指向current前面的结点
	if (isEmptyList())
	{
		cout<<"List is Empty"<data!=deleteItem)//删除第一个元素后的元素
		{
			trailCurrent = current;
			current=current->next;
		}
		if (current==NULL)
		{
			cout<<"Item is not Found in the list"<next = current->next;
			delete current;
			cout<<"Item is delete in the list"<
void Circlist::deleteNode(const int pos,Type& deleteItem)
{
	int i=1;
	Node* current = Head;
	Node* temp;
	if (pos<1 || pos>len)
	{
		cout<<"删除位置不正确!"<next;
		i++;
	}
	temp = current->next;
	current->next = temp->next;
	deleteItem = temp->data;
	delete temp;
	len--;
}

template
void Circlist::reverse()
{
	//先处理头结点
	Node* current = Head->next;
	Head->next=Head;
	if (current==Head)
	{
		cout<<"链表为空!"<* nextCurrent = current->next;
			current->next = Head->next;
			Head->next=current;
			current = nextCurrent;
		}
	}
}


void main()
{
	Circlist list1;
	list1.createBackward();
	list1.reverse();
	list1.printList();
	system("pause");
}


 

你可能感兴趣的:(数据结构)