有头结点的单向链表

自己写的一个错误百出的代码。。

#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;
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值