循环链表

 

#ifndef CIRCLIST_H

#define CIRCLIST_H

 

#include <iostream>

using namespace std;

 

template<class T>

struct CircLinkNode

{

     T data;

     CircLinkNode<T> *link;

     CircLinkNode(CircLinkNode<T> *next = NULL):link(next){}

     CircLinkNode(const T &d,CircLinkNode<T> *next = NULL):data(d),link(next){}

};

 

template<class T>

class CircList

{

public:

     CircList()

     {

         first = last = new CircLinkNode<T>;

         first->link = first;

     }

     CircList(const T &x)

     {

         first = last = new CircLinkNode<T>;

         first->link = first;

         first->data = x;

     }

     CircList(const CircList<T> &L);

     ~CircList()

     {

         MakeEmpty();

     }

     void MakeEmpty();  //清空链表

     void Input(T endTag);  //后插法输入建立循环链表

     int Length()const; //计算循环链表的长度

     bool IsEmpty()     //判断表空否

     {

         return first->link == first ? true : false;

     }

     CircLinkNode<T> *GetHead()const  //返回附加头结点地址

     {

         return first;

     }

     CircLinkNode<T> *Search(T x);    //搜索含数据x的元素

     CircLinkNode<T> *Locate(int i);  //定位第i个元素的地址

     bool GetData(int i,T &x);   //取出第i个元素的值

     bool SetData(int i,T &x);   //x修改第i个元素的值

     bool Insert(int i,T &x);    //在第i个元素后插入x

     bool Remove(int i,T &x);    //删除第i个元素,x返回该元素的值

     void Output();     //输出

private:

     CircLinkNode<T> *first,*last;    //头指针,尾指针

};

 

template<class T>

CircList<T>::CircList(const CircList<T> &L)

{

     T value;

     CircLinkNode<T> *srcptr = L.GetHead();    //被复制表的附加头结点地址

     last = first = new CircLinkNode<T>;

     while(srcptr->link  != NULL)

     {

         value = srcptr->link->data;

         last->link = new CircLinkNode<T>(value);

         last = last->link;

         srcptr = srcptr->link;

     }

     last->link = first;

}

 

template<class T>

void CircList<T>::MakeEmpty()

{

     CircLinkNode<T> *current;

     while (first->link != first)

     {

         current = first->link;

         first->link = current->link;

         delete current;

     }

}

 

template<class T>

void CircList<T>::Input(T endTag)

{

     CircLinkNode<T> *newNode;

     T val;

     MakeEmpty();

     cin>>val;

     while (val != endTag)

     {

         newNode = new CircLinkNode<T>(val);

         if (newNode == NULL)

         {

              cerr<<"存储分配错误!"<<endl;

              exit(1);

         }

         last->link = newNode;

         last = newNode;

         cin>>val;

     }

     last->link = first;

}

 

template<class T>

int CircList<T>::Length()const

{

     CircLinkNode<T> *current = first->link;

     int count = 0;

     while (current != first)

     {

         ++count;

         current = current->link;

     }

     return count;

}

 

template<class T>

CircLinkNode<T>* CircList<T>::Search(T x)

{

     CircLinkNode<T> *current = first->link;

     while (current != first)

     {

         if (current->data == x)

              break;

         else

              current = current->link;

     }

     if(current != first)

         return current;

     else

         return NULL;

}

 

template<class T>

CircLinkNode<T>* CircList<T>::Locate(int i)

{

     if(i<0)

         return NULL;

     if(i == 0)

         return first;

     CircLinkNode<T> *current = first->link;

     int k = 1;

     while (current != first && k < i)

     {

         ++k;

         current = current->link;

     }

     if(current != first)

         return current;

     else

         return NULL;

}

 

template<class T>

bool CircList<T>::GetData(int i,T &x)

{

     if(i<0)

         return false;

     CircLinkNode<T> *current = Locate(i);

     if(current == NULL)

         return false;

     else

     {

         x = current->data;

         return true;

     }

}

 

template<class T>

bool CircList<T>::SetData(int i,T &x)

{

     if(i<0)

         return false;

     CircLinkNode<T> *current = Locate(i);

     if(current == NULL)

         return false;

     else

     {

         current->data = x;

         return true;

     }

}

 

template<class T>

bool CircList<T>::Insert(int i,T &x)

{

     CircLinkNode<T> *current = Locate(i);

     if(current == NULL)

         return false;

     CircLinkNode<T> *newNode = new CircLinkNode<T>(x);

     if(newNode == NULL)

     {

         cerr<<"存储分配错误!"<<endl;

         exit(1);

     }

     if(current == last)

     {

         newNode->link = last->link;

         last->link = newNode;

         last = newNode;

     }

     else

     {

         newNode->link = current->link;

         current->link = newNode;

     }

     return true;

}

 

template<class T>

bool CircList<T>::Remove(int i,T &x)

{

     CircLinkNode<T> *current = Locate(i-1);

     if(current == NULL || current == last)

         return false;

     CircLinkNode<T> *del = current->link;

     if(del == last)

     {

         current->link = del->link;

         x = del->data;

         last = current;

         delete del;

     }

     else

     {

         current->link = del->link;

         x = del->data;

         delete del;

     }

     return true;

}

 

template<class T>

void CircList<T>::Output()

{

     CircLinkNode<T> *current = first->link;

     while (current != first)

     {

         cout<<current->data<<endl;

         current = current->link;

     }

}

 

#endif

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值