基本数据结构:循环单链表

上篇对单链表进行了详细说明,本篇接下来说说单循环链表

单循环链表就是在单链表基础上,尾巴元素指向了头,形成了圆圈,操作时只要注意尾部元素链接头就OK,废话不多说,直接上代码

单循环链表定义类 CircleLinkedList.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef CIRCLE_LINKEDLIST_H  
  2. #define CIRCLE_LINKEDLIST_H  
  3. #include <iostream>  
  4. #include <cstdlib>  
  5.   
  6. using std::cin;  
  7. using std::cout;  
  8. using std::endl;  
  9. using std::istream;  
  10. using std::ostream;  
  11. using std::cerr;  
  12.   
  13. template <class T>  
  14. struct LinkNode  
  15. {  
  16.     T data;//值域  
  17.     LinkNode<T> *link;//指针域  
  18.   
  19.     LinkNode(LinkNode<T> *ptr = nullptr)  
  20.     {  
  21.         link = ptr;  
  22.     }  
  23.   
  24.     LinkNode(const T& item,LinkNode<T> *ptr = nullptr)  
  25.     {  
  26.         data = item;  
  27.         link = ptr;  
  28.     }  
  29. };  
  30.   
  31. template <class T>  
  32. class CircleList  
  33. {  
  34. public:  
  35.     CircleList()//构造函数  
  36.     {  
  37.         first = new LinkNode<T>;  
  38.         first->link = first;//循环起来,围成圆圈  
  39.         length = 0;  
  40.     }  
  41.   
  42.     ~CircleList()//析构函数  
  43.     {  
  44.         clear();  
  45.         delete first;  
  46.     }  
  47.   
  48.     int Length()const;//返回链表长度  
  49.     LinkNode<T> *getHead()const{  
  50.         return first;  
  51.     }  
  52.     LinkNode<T> *Search(const T &x);//查找x是否存在链表中  
  53.     void clear();//清空链表,仅余头结点  
  54.     LinkNode<T> *locate(int i) const;//返回第i个元素,元素下标从1开始  
  55.     bool insert(int i,T& t);//在第i个位置插入元素  
  56.     bool remove(int i);//移除第i个位置插入元素  
  57.       
  58.   
  59.     bool isEmpty()const{  
  60.         return (first->link == first)?true:false;  
  61.     }  
  62.   
  63.     friend istream& operator >> (istream &in,CircleList<T> &list)  
  64.     {  
  65.         list.clear();//首先list清空  
  66.         LinkNode<T> *first,*last;  
  67.         first = list.first;  
  68.         last = first;  
  69.   
  70.         while(!in.eof())  
  71.         {  
  72.             T val;  
  73.             in >> val;  
  74.             LinkNode<T> *node = new LinkNode<T>(val);  
  75.             last->link = node;  
  76.             list.length++;  
  77.             last = node;  
  78.   
  79.         }  
  80.   
  81.         last->link = first;//尾结点指向头成圆  
  82.         return in;  
  83.     }  
  84.   
  85.     friend ostream& operator << (ostream& out,const CircleList<T> &list)  
  86.     {  
  87.         LinkNode<T> *p = list.first->link;  
  88.         int i = 1;  
  89.         while(p != list.first)  
  90.         {  
  91.             cout <<"#" << i <<":" << p->data << endl;  
  92.             p = p->link;  
  93.             ++i;  
  94.         }  
  95.         return out;  
  96.     }  
  97.   
  98. protected:  
  99.     LinkNode<T> *first;//记录链表头指针  
  100.     int length;//记录链表长度  
  101. };  
  102.   
  103. template <class T>  
  104. void CircleList<T>::clear()  
  105. {  
  106.     LinkNode<T> *p = first->link;  
  107.     LinkNode<T> *q;  
  108.     while(p != this->first)  
  109.     {  
  110.         q = p;  
  111.         p = p->link;//p向后移  
  112.         first->link = p;//重新与头结点建立联系  
  113.         delete q;//删除结点  
  114.         length--;     
  115.     }  
  116. }  
  117.   
  118. template <class T>  
  119. LinkNode<T> * CircleList<T>::Search(const T &x)  
  120. {  
  121.     LinkNode<T> *p = this->first->link;  
  122.     while(p != this->first)  
  123.     {  
  124.         if(p->data == x)  
  125.         {  
  126.             return p;  
  127.         }  
  128.         p = p->link;  
  129.     }  
  130.   
  131.     return p;  
  132. }  
  133.   
  134. template <class T>  
  135. LinkNode<T> * CircleList<T>::locate(int i) const  
  136. {  
  137.     if(i == 0)  
  138.     {  
  139.         return first;  
  140.     }  
  141.   
  142.     if(i < 0 || i > length)  
  143.     {  
  144.         return first;  
  145.     }  
  146.   
  147.     LinkNode<T> *p = first->link;  
  148.     for(int j = 1; j < i; j++)  
  149.     {  
  150.         p = p->link;  
  151.     }  
  152.   
  153.     return p;  
  154. }  
  155.   
  156. template <class T>  
  157. bool CircleList<T>::insert(int i,T& t)  
  158. {  
  159.     LinkNode<T> *current = locate(i-1);  
  160.     if(current == first)  
  161.     {  
  162.         return false;  
  163.     }  
  164.   
  165.     LinkNode<T> *node = new LinkNode<T>(t);  
  166.     if(node == nullptr)  
  167.     {  
  168.         cerr << "memory malloc failed!" << endl;  
  169.         exit(1);  
  170.     }  
  171.   
  172.     node->link = current->link;  
  173.     current->link = node;  
  174.     ++length;  
  175.   
  176.     return true;  
  177. }  
  178.   
  179. template <class T>  
  180. bool CircleList<T>::remove(int i)  
  181. {  
  182.     LinkNode<T> *current = locate(i-1);  
  183.     if(current == first)  
  184.     {  
  185.         return false;  
  186.     }  
  187.   
  188.     LinkNode<T> *p = current->link;  
  189.     current->link = p->link;  
  190.     delete p;  
  191.     length--;  
  192.   
  193.     return true;  
  194. }  
  195.   
  196.   
  197.   
  198. #endif  

测试类 main.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "CircleLinkedList.h"  
  2. #include <fstream>  
  3. using std::ifstream;  
  4. using std::ios;  
  5.   
  6. int main()  
  7. {  
  8.     CircleList<int> list;  
  9.       
  10.     //list.init();  
  11.     ifstream in = ifstream("list.txt",ios::in);  
  12.     in >> list;  
  13.     cout << "The initial list in the file is:\n" << list << endl;  
  14.   
  15.       
  16.     /*cout << "input the number you want search:"; 
  17.     int number; 
  18.     cin >> number; 
  19.     LinkNode<int> *p = list.Search(number); 
  20.     if(p == list.getHead()) 
  21.     { 
  22.         cout << number << " not exsited" << endl; 
  23.     } 
  24.     else 
  25.     { 
  26.         cout  << number << " exsited" << endl; 
  27.     }*/  
  28.       
  29.     /*list.clear(); 
  30.     cout << "The cleaded list in the file is:\n" << list << endl;*/  
  31.   
  32.   
  33.     /*cout << "input the index you want locate:"; 
  34.     int index; 
  35.     cin >> index; 
  36.     LinkNode<int> *p = list.locate(index); 
  37.     if(p == list.getHead()) 
  38.     { 
  39.         cout << "element at " << index << " not exsited" << endl; 
  40.     } 
  41.     else 
  42.     { 
  43.         cout  << "element at " << index << " is " << p->data <<endl; 
  44.     }*/  
  45.   
  46.     /*cout << "input the index、value you want insert ,split by space "; 
  47.     int index; 
  48.     int value; 
  49.     cin >> index >> value; 
  50.  
  51.     if(list.insert(index,value)) 
  52.     { 
  53.         cout << list << endl; 
  54.     }*/  
  55.   
  56.     cout << "input the index you want delete  ";  
  57.     int index;  
  58.     cin >> index ;  
  59.   
  60.     if(list.remove(index))  
  61.     {  
  62.         cout << list << endl;  
  63.     }  
  64.   
  65.     in.close();  
  66.     system("pause");  
  67.     return 0;  
  68. }  
  69.   
  70. /* 
  71. int main() 
  72. { 
  73.     ifstream in = ifstream("list.txt",ios::in); 
  74.     int num; 
  75.     if(!in) 
  76.     { 
  77.         cout << "文件打不开" << endl; 
  78.     } 
  79.     else 
  80.     { 
  81.         while(in >> num) 
  82.         { 
  83.             cout << num; 
  84.         } 
  85.     } 
  86.      
  87.     in.close(); 
  88.     system("pause"); 
  89.     return 0; 
  90. }*/  

数据输入文件 list.txt

1 2 3 4 56 7 890 34 56 24 578 1246 45

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值