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

来源:互联网 发布:达内java飞机大战图片 编辑:程序博客网 时间:2024/06/06 02:29

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

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

单循环链表定义类 CircleLinkedList.h

#ifndef CIRCLE_LINKEDLIST_H#define CIRCLE_LINKEDLIST_H#include <iostream>#include <cstdlib>using std::cin;using std::cout;using std::endl;using std::istream;using std::ostream;using std::cerr;template <class T>struct LinkNode{T data;//值域LinkNode<T> *link;//指针域LinkNode(LinkNode<T> *ptr = nullptr){link = ptr;}LinkNode(const T& item,LinkNode<T> *ptr = nullptr){data = item;link = ptr;}};template <class T>class CircleList{public:CircleList()//构造函数{first = new LinkNode<T>;first->link = first;//循环起来,围成圆圈length = 0;}~CircleList()//析构函数{clear();delete first;}int Length()const;//返回链表长度LinkNode<T> *getHead()const{return first;}LinkNode<T> *Search(const T &x);//查找x是否存在链表中void clear();//清空链表,仅余头结点LinkNode<T> *locate(int i) const;//返回第i个元素,元素下标从1开始bool insert(int i,T& t);//在第i个位置插入元素bool remove(int i);//移除第i个位置插入元素bool isEmpty()const{return (first->link == first)?true:false;}friend istream& operator >> (istream &in,CircleList<T> &list){list.clear();//首先list清空LinkNode<T> *first,*last;first = list.first;last = first;while(!in.eof()){T val;in >> val;LinkNode<T> *node = new LinkNode<T>(val);last->link = node;list.length++;last = node;}last->link = first;//尾结点指向头成圆return in;}friend ostream& operator << (ostream& out,const CircleList<T> &list){LinkNode<T> *p = list.first->link;int i = 1;while(p != list.first){cout <<"#" << i <<":" << p->data << endl;p = p->link;++i;}return out;}protected:LinkNode<T> *first;//记录链表头指针int length;//记录链表长度};template <class T>void CircleList<T>::clear(){LinkNode<T> *p = first->link;LinkNode<T> *q;while(p != this->first){q = p;p = p->link;//p向后移first->link = p;//重新与头结点建立联系delete q;//删除结点length--;}}template <class T>LinkNode<T> * CircleList<T>::Search(const T &x){LinkNode<T> *p = this->first->link;while(p != this->first){if(p->data == x){return p;}p = p->link;}return p;}template <class T>LinkNode<T> * CircleList<T>::locate(int i) const{if(i == 0){return first;}if(i < 0 || i > length){return first;}LinkNode<T> *p = first->link;for(int j = 1; j < i; j++){p = p->link;}return p;}template <class T>bool CircleList<T>::insert(int i,T& t){LinkNode<T> *current = locate(i-1);if(current == first){return false;}LinkNode<T> *node = new LinkNode<T>(t);if(node == nullptr){cerr << "memory malloc failed!" << endl;exit(1);}node->link = current->link;current->link = node;++length;return true;}template <class T>bool CircleList<T>::remove(int i){LinkNode<T> *current = locate(i-1);if(current == first){return false;}LinkNode<T> *p = current->link;current->link = p->link;delete p;length--;return true;}#endif

测试类 main.cpp

#include "CircleLinkedList.h"#include <fstream>using std::ifstream;using std::ios;int main(){CircleList<int> list;//list.init();ifstream in = ifstream("list.txt",ios::in);in >> list;cout << "The initial list in the file is:\n" << list << endl;/*cout << "input the number you want search:";int number;cin >> number;LinkNode<int> *p = list.Search(number);if(p == list.getHead()){cout << number << " not exsited" << endl;}else{cout  << number << " exsited" << endl;}*//*list.clear();cout << "The cleaded list in the file is:\n" << list << endl;*//*cout << "input the index you want locate:";int index;cin >> index;LinkNode<int> *p = list.locate(index);if(p == list.getHead()){cout << "element at " << index << " not exsited" << endl;}else{cout  << "element at " << index << " is " << p->data <<endl;}*//*cout << "input the index、value you want insert ,split by space ";int index;int value;cin >> index >> value;if(list.insert(index,value)){cout << list << endl;}*/cout << "input the index you want delete  ";int index;cin >> index ;if(list.remove(index)){cout << list << endl;}in.close();system("pause");return 0;}/*int main(){ifstream in = ifstream("list.txt",ios::in);int num;if(!in){cout << "文件打不开" << endl;}else{while(in >> num){cout << num;}}in.close();system("pause");return 0;}*/

数据输入文件 list.txt

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


0 0
原创粉丝点击