循环链表的实现

vs2008运行正确,如有误,请各位大牛指正!

注意与单链表(带头结点)的差别

// CycleList.cpp : 定义控制台应用程序的入口点。
//循环链表

#include "stdafx.h"
#include <iostream>
using namespace std;

template<class Type>
struct Node
{
	Type data;
	Node<Type> *next;
};

template<class Type>
class CycleList
{
public:
	CycleList();//默认构造函数
	CycleList(const CycleList<Type>& otherList);//拷贝构造函数
	~CycleList();
	void createInsertHead();//头插法
	void createInsertRear();//尾插法
	void initList();//生成头结点,尾部设置为NULL
	bool isEmpty();	
	int  length();
	void destoryList();
	void getFirstData(Type& firstdata);
	void find(const Type finddata);
	void insertFirst(const Type newdata);
	void insertLast(const Type newdata);
	void insertBefore(const int pos,const Type newdata);//在指定位置前插入新的元素
	void insertAfter(const int pos,const Type newdata);//在指定位置后插入新的元素
	void deleteNode(const Type deletedata);
	void deleteNode(const int pos,Type& deletedata);
	void reverse();
	const CycleList<Type>& operator=(const CycleList<Type>&otherList);//重载赋值运算符
	friend ostream& operator<< <>(ostream& out, const CycleList& cyclelist);

private:
	int m_length;//链表中结点个数
	Node<Type>* head; //指向头结点
};

template<class Type>
CycleList<Type>::CycleList()//默认构造函数
{
	head = new Node<Type>;
	head->next = head;
	m_length = 0;
}

template<class Type>
CycleList<Type>::CycleList(const CycleList<Type>& otherList)//拷贝构造函数
{
	//首先建立头结点
	head = new Node<Type>;
	head->next = head;
	m_length = otherList.m_length;

	Node<Type> *current = head;
	Node<Type> *otherListcurrent = otherList.head->next;
	if (otherList.head->next!=otherList.head)//链表不空
	{
		while(otherListcurrent->next!=otherList.head)//拷贝的目标不空
		{
			Node<Type> *newnode = new Node<Type>;
			newnode->data = otherListcurrent->data;
			newnode->next = current->next;
			current->next = newnode;
			current = current->next;
			otherListcurrent = otherListcurrent->next;
		}
		if (otherListcurrent->next == otherList.head)//循环链表只有一个结点
		{
			Node<Type> *newnode = new Node<Type>;
			newnode->data = otherListcurrent->data;
			newnode->next = current->next;
			current->next = newnode;
		}
	}	
}

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

template<class Type>
void CycleList<Type>::createInsertHead()//头插法
{	
	Node<Type> *newnode;
	cout<<"请输入链表的长度:";
	cin>>m_length;
	cout<<"请输入链表的元素:";
	for (int i=1; i<=m_length; i++)
	{
		newnode = new Node<Type>;
		cin>>newnode->data;
		newnode->next = head->next;
		head->next = newnode;
	}
}

template<class Type>
void CycleList<Type>::createInsertRear()//尾插法
{
	Node<Type> *current = head;
	Node<Type> *newnode;
	cout<<"请输入链表的长度:";
	cin>>m_length;
	cout<<"请输入链表的元素:";
	for (int i=1; i<=m_length; i++)
	{
		newnode = new Node<Type>;
		cin>>newnode->data;
		newnode->next = current->next;
		current->next = newnode;
		current = current->next;
	}
}

template<class Type>
void CycleList<Type>::initList()//生成头结点,尾部设置为NULL
{
	destoryList();
	head = new Node<Type>;
	head->next = head;
	m_length = 0;
}

template<class Type>
bool CycleList<Type>::isEmpty()
{
	if (head->next == head)
	{
		return true;
	}
	else
	{
		return false;
	}
}

template<class Type>
int  CycleList<Type>::length()
{
	return m_length;
}

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

template<class Type>
void CycleList<Type>::getFirstData(Type& firstdata)
{
	if (!isEmpty())
	{
		firstdata = head->next->data;
	}
	else
	{
		cout<<"链表为空!"<<endl;
	}
}

template<class Type>
void CycleList<Type>::find(const Type finddata)
{
	Node<Type> *current;
	if (isEmpty())
	{
		cout<<"链表为空!"<<endl;
	}
	else
	{
		current = head->next;
		while( current!=head && current->data!=finddata)
		{
			current = current->next;
		}
		if (current==head)
		{
			cout<<finddata<<"没有被找到!"<<endl;
		}
		else
		{
			cout<<finddata<<"被找到!"<<endl;
		}
	}
	
}

template<class Type>
void CycleList<Type>::insertFirst(const Type newdata)
{
	Node<Type> *newnode = new Node<Type>;
	newnode->data = newdata;
	newnode->next = head->next;
	head-> next = newnode;
	m_length++;
}

template<class Type>
void CycleList<Type>::insertLast(const Type newdata)
{
	Node<Type> *current = head;
	Node<Type> *newnode;
	while(current->next!=head)
	{
		current = current->next;		
	}
	newnode = new Node<Type>;
	newnode->data = newdata;
	newnode->next = current->next;
	current-> next = newnode;
	m_length++;
}

template<class Type>
void CycleList<Type>::insertBefore(const int pos,const Type newdata)//在指定位置前插入新的元素
{
	int i = 1;
	Node<Type> *newnode;
	Node<Type> *current=head;
	if (pos<1 || pos>m_length)
	{
		cout<<"指定的位置不正确!"<<endl;
	}
	else
	{
		newnode = new Node<Type>;
		newnode->data = newdata;
		
		while(i<pos)
		{
          current = current->next;
		  i++;
		}
		newnode->next = current->next;
		current-> next = newnode;
		m_length++;
	}
}

template<class Type>
void CycleList<Type>::insertAfter(const int pos,const Type newdata)//在指定位置后插入新的元素
{
	int i = 0;
	Node<Type> *newnode;
	Node<Type> *current=head;
	if (pos<1 || pos>m_length)
	{
		cout<<"指定的位置不正确!"<<endl;
	}
	else
	{
		newnode = new Node<Type>;
		newnode->data = newdata;

		while(i<pos)
		{
			current = current->next;
			i++;
		}
		newnode->next = current->next;
		current-> next = newnode;
		m_length++;
	}
}

template<class Type>
void CycleList<Type>::deleteNode(const Type deletedata)
{

	if (isEmpty())
	{
		cout<<"链表为空!"<<endl;
	}
	else
	{
        Node<Type> *precurrent = head;
		Node<Type> *current = head->next;
		while(current!=head && current->data != deletedata)
		{
			precurrent = current;
			current = current->next;
		}
        if (current == head)
        {
			cout<<deletedata<<"不存在!"<<endl;
        }
		else
		{
            precurrent->next = current->next;
			delete current;
			cout<<deletedata<<"已被删除!"<<endl;
			m_length--;
		}		
	}
}

template<class Type>
void CycleList<Type>::deleteNode(const int pos,Type& deletedata)
{
	int i=1;
	if (pos<1 || pos>m_length)
	{
		cout<<"指定的位置不正确!"<<endl;
		deletedata = -1;
		return;
	}
	else
	{
		if (isEmpty())
		{
			cout<<"链表为空!"<<endl;			
		}
		else
		{			
			Node<Type> *current = head;//一个指针足矣
			Node<Type> *temp;
            while(i<pos)
			{				
				current = current->next;
				i++;
			}
            temp = current->next;
			deletedata = temp->data;
			current->next = temp->next;
            delete temp;
			m_length--;
			cout<<"位置为"<<pos<<"的元素已被删除!"<<endl;
		}
	}
}

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

template<class Type>
const CycleList<Type>& CycleList<Type>::operator=(const CycleList<Type>&otherList)//重载赋值运算符
{
	if (this!=&otherList)
	{
		if (!isEmpty())
		{
			initList();
		}

		Node<Type> *current = head;
		Node<Type> *otherListcurrent = otherList.head->next;
		if (otherList.head->next!=otherList.head)//链表不空
		{
			while(otherListcurrent->next!=otherList.head)//拷贝的目标不空
			{
				Node<Type> *newnode = new Node<Type>;
				newnode->data = otherListcurrent->data;
				newnode->next = current->next;
				current->next = newnode;
				current = current->next;
				otherListcurrent = otherListcurrent->next;
			}
			if (otherListcurrent->next == otherList.head)//循环链表只有一个结点
			{
				Node<Type> *newnode = new Node<Type>;
				newnode->data = otherListcurrent->data;
				newnode->next = current->next;
				current->next = newnode;
			}
		}
	}
	return *this;
}

template<class Type>
ostream& operator<< <>(ostream& out, const CycleList<Type>& cyclelist)
{
	Node<Type> *current = cyclelist.head->next;
	while(current!=cyclelist.head)
	{
		cout<<current->data<<" ";
		current = current->next;
	}
	cout<<endl;
	return out;
}


int _tmain(int argc, _TCHAR* argv[])
{
	CycleList<int> *list = new CycleList<int>;
	list->createInsertHead();
	cout<<"list:"<<*list<<endl;
	list->initList();
	list->createInsertRear();
	cout<<"list:"<<*list<<endl;
	int firstdata;
	list->getFirstData(firstdata);
	cout<<"第一个数:"<<firstdata<<endl;
	list->find(5);
    list->insertFirst(89);
	list->insertLast(100);
	cout<<"list:"<<*list<<endl;
	list->insertBefore(4,63);
	cout<<"list:"<<*list<<endl;
	list->insertAfter(2,88);
	cout<<"list:"<<*list<<endl;
	list->deleteNode(7);
	list->deleteNode(9);
	int deletedata;
	list->deleteNode(3,deletedata);
	
	cout<<"list:"<<*list<<endl;

	CycleList<int> *copylist = new CycleList<int>(*list); 
	cout<<"copylist:"<<*copylist<<endl;

	list->reverse();
	copylist = list;
	cout<<"copylist:"<<*copylist<<endl;

    system("pause");
	return 0;
}


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值