自己写的一个错误百出的代码。。
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
//对于线性表有必要执行的操作:
//创建,撤销
//确定线性表是否为空
//确定线性表的长度
//按索引查找一个元素
//按元素查找索引。
//按索引删除元素
//按索引插入元素
//从左到右的顺序输出线性表元素
//挑战在ppt都没看懂的情况下自己写带有头结点的单向循环链表
//这是一个节点
template<class T>
class Node
{
public:
Node(){}
~Node();
Node(T data);
Node(const Node<T>& a);
public:
T data;
Node<T> *next;
};
template <class T>
Node<T>::Node(T data) {
this->data = data;
this->next = NULL;
}
template<class T>
Node<T>::Node(const Node<T>& a)
{
this->data = a->data;
this->next = a->next;
}
//析构函数
template<class T>
Node<T>::~Node()
{
delete next;
}
//这是一个带有头结点的单向循环链表
template<class T>
class CircleList {
public:
CircleList();
~CircleList();
//创建,撤销
//确定线性表是否为空
//确定线性表的长度
//按索引查找一个元素
//按元素查找索引。
//按索引删除元素
//按索引插入元素
//从左到右的顺序输出线性表元素
void checkIndex(int index);//查看位置是否超出范围
void init();
bool empty();
int length();
Node<T>& getByIndex(int index);
int getByNode(const Node<T>& e);
bool deleteNode(int index);
bool insert(T& e,int index);
void print();
protected:
Node<T> * head;
int chainLength;
};
//构造函数
template<class T>
CircleList<T>::CircleList()
{
head = new Node<T>();
head->next = head;
chainLength = 0;
}
//析构函数
template<class T>
CircleList<T>::~CircleList()
{
if (chainLength == 0)
{
delete head;
}
else {
Node<T>* deleteNode = head;
while (head->next)
{
deleteNode = head;
head = head->next;
delete deleteNode;
}
}
}
template<class T>
void CircleList<T>::checkIndex(int index)
{
if (index <= 0 || index > chainLength)
{
ostringstream s;
s << "index=" << index << "chain length= " << chainLength;
//throw illegalindex(s.str());
}
}
template<class T>
void CircleList<T>::init()
{
head = new Node<T>();
head->next = NULL;
cout << "请输入单向循环链表的长度";
cin >> chainLength;
T data;
Node<T> *cur = head;
if (chainLength > 0)
{
for (int i = 0; i < chainLength; i++)
{
cin >> data;
cur->next = new Node<T>(data);
cur = cur->next;
}
}
cur->next = head;
}
template<class T>
bool CircleList<T>::empty()
{
return chainLength == 0;
}
template<class T>
int CircleList<T>::length()
{
return chainLength;
}
template<class T>
Node<T>& CircleList<T>::getByIndex(int index) {
checkIndex(index);
Node<T> *cur = head;
for (int i = 0; i < index; i++)
{
cur = cur->next;
}
return cur;
}
template<class T>
int CircleList<T>::getByNode(const Node<T>& e) {
T data = e->data;
int index=0;
Node<T>* cur = head;
head->data = data;//想出来这招的人老聪明了
while (cur->data != data)
{
cur = cur->next;
index++;
}
if (cur == head)return -1;
return index;
}
template<class T>
bool CircleList<T>::deleteNode(int index) {
checkIndex(index);
Node<T>* cur = head;
Node<T>* deleteNode;
for (int i = 0; i < index-1; i++)
{
cur =cur->next;
}
deleteNode = cur->next;
cur->next = cur->next->next;
delete deleteNode;
return true;
}
template<class T>
bool CircleList<T>::insert(T& e,int index) {
if (index <= 0 || index > chainLength + 1)
{
ostringstream s;
s << "index=" << index << "chain length= " << chainLength;
//throw illegalIndex(s.str());
}
Node<T>* cur = head;
Node<T>* newNode = new Node(e);
for (int i = 0; i < index-1; i++)
{
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
}
template<class T>
void CircleList<T>::print()
{
Node<T>* cur = head->next;
while (cur != head)
{
cout << cur->data;
cur = cur->next;
}
}