C++类模板实现链表与简单迭代器

  1. 实现了链表简单的几个功能
  2. 迭代器并不实用,写着玩(未重载操作符)
  • 头文件如下
#pragma once

template<class T> class MyList;
template<class T> class ListIterator;

//链表结点
template<class T>
class Node
{
public:
	Node(T item) : date(item) {};
	friend class MyList<T>;
	friend class ListIterator<T>;
private:
	T date;
	Node* next;
};

//链表
template<class T>
class MyList
{
public:
	MyList();
	~MyList();
	void Insert(const T&);
	void Delete(const T&);
	int Find(const T&) const;
	void Reserval();
	friend class ListIterator<T>;

private:
	Node<T>* m_head;
};

//链表迭代器
template<class T>
class ListIterator
{
public:
	ListIterator(const MyList<T>& L) :m_list(L), m_current(L.m_head) {}
	bool NotNull();//当前节点是否空
	bool NextNotNull();//当前节点的下一节点是否空
	T* First();
	T* Next();
private:
	const MyList<T>& m_list;
	Node<T>* m_current;

};



template<class T>
MyList<T>::MyList()
	: m_head(nullptr)
{
}

template<class T>
MyList<T>::~MyList()
{
	Node<T>* current = m_head;
	while (current)
	{
		Node<T>* q = current;
		current = current->next;
		delete q;
	}
}

template<class T>
inline void MyList<T>::Insert(const T& item)
{
	Node<T>* newNode = new Node<T>(item);

	newNode->next = m_head;
	m_head = newNode;
}

template<class T>
inline void MyList<T>::Delete(const T& item)
{
	Node<T>* previous = nullptr;
	Node<T>* current = m_head;
	while (current != nullptr)
	{
		if (current->date == item)
		{
			break;
		}
		previous = current;
		current = current->next;
	}

	if (current) //不是最后一个结点
	{
		if (previous) //不是第一个结点
		{
			previous->next = current->next;
			delete current;
		}
		else
		{
			m_head = m_head->next;
			delete current;
		}
	}
}

template<class T>
inline int MyList<T>::Find(const T& item) const
{
	int count = 0;
	Node<T>* current = m_head;
	while (current)
	{
		if (current->date == item)
		{
			return count;
		}
		count++;
		current = current->next;
	}
	return -1;
}

template<class T>
inline void MyList<T>::Reserval()
{
	Node<T>* p = m_head, * q = nullptr;
	while (p)
	{
		Node<T>* r = q;
		q = p;
		p = p->next;
		q->next = r;
	}
	m_head = q;

}

template<class T>
inline bool ListIterator<T>::NotNull()
{
	return m_current != nullptr;
}

template<class T>
inline bool ListIterator<T>::NextNotNull()
{
	return m_current && m_current->next != nullptr;
}

template<class T>
inline T* ListIterator<T>::First()
{
	if (m_list.m_head)
	{
		return &m_list.m_head->date;
	}
	return nullptr;
}

template<class T>
inline T* ListIterator<T>::Next()
{
	if (m_current)
	{
		m_current = m_current->next;
		return &m_current->date;
	}
	return nullptr;
}


  • 测试用例
void listTest() {

	MyList<int> intList;

	intList.Insert(5);
	intList.Insert(15);
	intList.Insert(25);
	intList.Insert(35);
	intList.Insert(45);

	intList.Reserval();
	intList.Delete(25);
	intList.Delete(5);
	intList.Delete(45);

	ListIterator<int> li(intList);//建立一个ListIterator类的对象,用链表intList初始化
	if (li.NotNull())
	{
		cout << *li.First();
		while (li.NextNotNull())
			cout << " -> " << *li.Next();
		cout << endl;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值