注意:循环链表含有头结点[cpp]viewplaincopyprint?#include <iostream> using namespace std; template<class Type> struct Node { Type data; Node<Type> *next; }; template<class Type> class Circlist { protected: int len;//链表中结点个数 Node<Type>* Head; //指向头结点 public: Circlist();//默认构造函数 Circlist(const Circlist<Type>& 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<Type>& operator=(const Circlist<Type>&otherList); }; template<class Type> Circlist<Type>::Circlist() //初始化时,只有一个头结点,有head指向 { Head = new Node<Type>; Head->next = Head; len =0; } template<class Type> Circlist<Type>::Circlist(const Circlist<Type>&otherList) { Head = new Node<Type>; Head->next = Head; len =0; Node<Type>* current = Head; Node<Type>* otherListCurrent = ->next;//otherListCurrent指向第一个元素 if (->next != )//被拷贝的表不是空表 { while(otherListCurrent->next!=)//拷贝的目标不为空 { Node<Type>* newNode = new Node<Type>; newNode->data = otherListCurrent->data; newNode->next = current->next; current->next = newNode; current=current->next; otherListCurrent = otherListCurrent->next; len++; } if (otherListCurrent->next==) { Node<Typ
C 实现循环链表 来自淘豆网m.daumloan.com转载请标明出处.