C++数据结构之链表迭代器(十七)

这里引用一下猎豹网校的代码。多看,看了之后在独立敲出来!

  • main.cpp
#include <list> //C++ STL中的链表
#include "List.h" //我做的链表

using namespace std;

int main()
{
	cout << "测试: " << endl;

	cout << "这是标准C++ STL中的链表和迭代器: " << endl;
	std::list<int> listIntegers;
	listIntegers.push_front(5);
	listIntegers.push_front(15);
	listIntegers.push_front(25);
	listIntegers.push_front(35);

	std::list<int>::iterator i = listIntegers.begin();
	while(i != listIntegers.end())
	{
		cout << *i * 10 << " -> ";
		++i;
	}
	cout <<endl << endl;

	cout << "这是我的链表和迭代器: " << endl;
	List<int> intList;

	intList.Insert(5);
	intList.Insert(15);
	intList.Insert(25);
	intList.Insert(35);
	
	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(15);
	intList.Show();

	intList.Delete(20);
	intList.Show();


	List<char> charList;
	charList.Insert('a');
	charList.Insert('b');
	charList.Insert('c');
	charList.Insert('d');
	charList.Show();
	charList.Invert();
	charList.Show();

	List<char> char2List;
	char2List.Insert('e');
	char2List.Insert('f');
	char2List.Show();
	char2List.Invert();
	char2List.Show();

	charList.Concatenate(char2List);
	charList.Show();

	return 0;
}
  • List.h
#ifndef LIST_H
#define LIST_H

#include <iostream>

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 Insert(Type);
	void Delete(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 &current->data;
	}
	else return 0;
}

template<class Type>
ListNode<Type>::ListNode(Type element)
{
	data = element;
	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>::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>::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>::Show()
{
	for(ListNode<Type> *current = first; current; current = current->link)
	{
		std::cout << current->data;
		if(current->link) std::cout << " -> ";
	}
	std::cout << std::endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值