一、作用
1、可通过链表迭代器作为输出函数输出节点元素值
2、可通过链表迭代器为节点元素重新赋值
二、代码
main.cpp
#include <iostream>
#include <list>//C++ STL中的链表
#include "MyList.h"
using namespace std;
int main()
{
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();
cout<< *i<<"->";
while(i!=listIntegers.end())
{
cout<<*i<<"->";
++i;
}
cout<<endl<<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();
while(li.NextNotNull())
{
cout<<"->"<<*li.Next();
}
cout<<endl;
}
return 0;
}
MyList.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
template <class Type> class List;
template <class Type> class ListIterator;
/*节点类ListNode*/
template <class Type>
class ListNode
{
friend class List<Type>;
friend class ListIterator<Type>;
private:
Type data;
ListNode *link;
ListNode(Type);//私有构造函数,只有由友元类List调用
} ;
/*链表类List*/
template <class Type>
class List
{
friend class ListIterator<Type>;
public:
List()//构造函数。建立一个空的链表
{
first = 0;//头指针为0
};
void Insert(Type);//链表插入节点函数
void Delete(Type);//链表删除节点函数
void Invert();//链表元素反转函数
void Concatenate(List<Type>);//连接链表函数
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;//链表。这个迭代器ListIterator就是这个链表list的迭代器
ListNode<Type> *current;//私有的数据成员,指针current
};
/*判断链表迭代器所指向的链表里的当前节点是不是空的*/
template <class Type>
bool ListIterator<Type>::NotNull()
{
if(current)
{
return true;
}
return false;
}
/*判断当前节点的下一个节点是不是空的函数*/
template <class Type>
bool ListIterator<Type>::NextNotNull()
{
if(current&¤t->link)
{
return true;
}
return false;
}
/*返回链表的第一个节点的指针函数*/
template <class Type>
Type *ListIterator<Type>::First()
{
if(list.first)
{
return &list.first->data;
}
return 0;
}
/*返回链表当前节点下一个节点的指针函数*/
template <class Type>
Type *ListIterator<Type>::Next()
{
if(current)
{
current = current ->link;
return ¤t->data;
}
return 0;
}
/*ListNode的构造函数*/
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;//用来遍历整个链表
/*令current节点为first节点;
当current存在且其值不为k时;
将前一个节点(previous)指向current,其current指向后一个节点。
以此实现对链表的整个遍历*/
for(current = first;current&¤t->data != k;previous =current,current = current->link)
{
//空循环。目的:找到要被删除的节点
}
if(current)//找到了其data值为k的节点
{
if(previous)//如果有前一个那就不是第一个节点(即不是头结点)
{
previous->link = current->link;
}else//要删除的节点是头结点
{
first = first->link;
}
delete current;//current指向first,first是new出来的。所以current需要delete操作
}
}
/*链表元素反转函数*/
template <class Type>
void List<Type>::Invert()
{
ListNode<Type> *p = first;//指向链表的开头
ListNode<Type> *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)//链表b不是空的
{
ListNode<Type> *p;
for(p = first;p->link;p = p->link)
{
;//空循环。找到first节点链表的最后一个节点
}
p->link = b.first;
}
}
#endif // LIST_H
输出结果: