链表迭代器

#ifndef _LIST_H
#define _LIST_H

template<class Type> class List;
template<class Type> class ListIterator;

template<class Type>
class ListNode
{
	friend class List<Type>;
	friend class ListIterator<Type>;
private:
	Type data;  // 节点里的数据,
	ListNode *link; // 指向下一个节点的数据,
	ListNode(Type);
};

template<class Type>
class List
{
	friend class ListIterator<Type>;
public:
	List() { first = 0; }
	void Delete(Type);
	void Insert(Type);
	void Invert();  // 翻转,
	void Concatenate(List<Type>); // 链接,
	void Show();  // 测试用的,
private:
	ListNode<Type> *first;
};

template<class Type>
class ListIterator
{
public:
	ListIterator(const List<Type>& l):list(l),current(l.first){}
	bool NotNull();
	bool NextNotNull();
	Type* First();
	Type* Next();
private:
	const List<Type> &list;
	ListNode<Type>* current;
};

template<class Type>
bool ListIterator<Type>::NotNull()
{
	if(current) return true;
	else return false;
}

template<class Type>
bool ListIterator<Type>::NextNotNull()
{
	if(current && current->link) return true;
	else return false;
}

template<class Type>
Type* ListIterator<Type>::First()
{
	if(list.first) return &list.first->data;
	else return 0;
}

template <class Type>
Type* ListIterator<Type>::Next()
{
	if(current)
	{
		current = current->link;
		return ¤t->data;
	}
	else return 0;
}

template<class Type>
ListNode<Type>::ListNode(Type element)
{
	data = element; // 参数就是节点里的数据放到data里边,
	link = 0;
}


template<class Type>
void List<Type>::Insert(Type k)
{
	ListNode<Type> *newnode = new ListNode<Type>(k);
	newnode->link = first;  // 新的节点作为第一个节点,
	first = newnode;
}

template<class Type>
void List<Type>::Delete(Type k)
{
	ListNode<Type> *previous = 0; // 前一个,
	ListNode<Type> *current;
	for(current = first;
		current && current->data  != k;
		previous = current, current = current->link)
	{
		;// 什么都不做,空循环,找到要被删除的节点,
	}
		if(current)
		{
			if(previous) previous->link = current->link;
			else first = first->link;
			delete current;
		}
}

template<class Type>
void List<Type>::Concatenate(List<Type> b)
{
	if(!first) { first = b.first; return; }
	if(b.first)
	{
		ListNode<Type> *p;
		for(p = first; p->link; p = p->link);  // 空循环,
		p->link = b.first;
	}
}


template<class Type>
void List<Type>::Invert()
{
	ListNode<Type> *p = first, *q = 0;
	while(p)
	{
		ListNode<Type> *r = q; 
		q = p;
		p = p->link;
		q->link = r;
	}
	first = q;
}

template<class Type>
void List<Type>::Show()
{
	for(ListNode<Type> *current = first; current;current = current->link)
	{
		std::cout << current->data;
		if(current->link) std::cout << " -> ";
	}
	std::cout << std::endl;
}

#endif _LIST_H

#include <iostream>
#include "List.h" //这个是自己做的链表,
#include <list>  // C++ STL中的链表

using namespace std;

int main()
{
	cout << "这是标准C++ STL中的链表和迭代器:" << endl;
	list<int> listIntegers;
	listIntegers.push_front(3);
	listIntegers.push_front(6);
	list<int>::iterator i = listIntegers.begin();
	while(i != listIntegers.end())
	{
		cout << *i << " -> ";
		++i;
	}
	cout << endl;
	
	cout << "这是自己设计的链表和迭代器:" << endl;
	List<int> intList;
	intList.Insert(31);
	intList.Insert(12);
	intList.Insert(3);
	intList.Insert(6);

	ListIterator<int> li(intList);
	if(li.NotNull())
	{
		cout << *li.First() * 10;  // 在迭代的过程中可以进行操作,
		while(li.NextNotNull())
			cout << " -> " << *li.Next() * 10;
		cout << endl;
	}

	return 0;

	intList.Show();
	intList.Invert();
	intList.Show();

	intList.Delete(6);
	intList.Show();

	intList.Delete(60);
	intList.Show();


	List<char> charList;
	charList.Insert('x');
	charList.Insert('i');
	charList.Insert('a');
	charList.Insert('o');
	charList.Invert();
	charList.Show();


	List<char> charList2;
	charList2.Insert('c');
	charList2.Insert('u');
	charList2.Insert('i');
	charList2.Invert();
	charList2.Show();

	charList.Concatenate(charList2);
	charList.Show();

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值