📄 listiterator.hpp
字号:
#include<iostream.h>
#include<stdlib.h>
#include"list.hpp"
#ifndef ITERATOR_H
#define ITERATOR_H
//////////////////////////////////////////////////////////////////////////
//游标类定义开始
//////////////////////////////////////////////////////////////////////////
template<class Type>
class ListIterator
{
public:
ListIterator(List<Type>& l);//构造函数,初始化数据成员list和当前节点指针current
void init(); //初始化函数,将currrent指向第一个节点(实际指向的是头节点first)
bool operator ! ();//判断当前节点是否为空
Type& operator () ();//取出当前节点元素
void operator ++ ();//current指向下一个节点
void operator = (Type& value);//改变当前节点的数据域
void removeCurrent(); //删除当前节点
void Locate(Type& x);//在链表中寻找第一个数据域不小于x的节点,并将current指向第一个数据域不小于x的节点的前驱
void addBefore(Type& newValue);//在当前节点前插入一个元素
void addAfter(Type& newValue);//在当前节点后插入一个元素
protected:
List<Type>& list; //游标类要操作的单链表
ListNode<Type>* current;//当前要操作的节点,实际指向的是当前要操作的节点的前驱节点,以方便在当前结点前插入元素
};
//////////////////////////////////////////////////////////////////////////
template<class Type>
ListIterator<Type>::ListIterator(List<Type>& l):list(l),current(l.first)
{}
//////////////////////////////////////////////////////////////////////////
template<class Type>
bool ListIterator<Type>::operator ! ()
{
if(current == NULL || current->link == NULL)
return false;
else
return true;
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::operator ++()
{
current=current->link;
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::init()
{
current=list.first;
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
Type& ListIterator<Type>::operator () ()
{
if(current->link != NULL && current!=NULL)
return current->link->data;
else
return list.first->data;
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::operator =(Type& value)
{
if(current != NULL && current->link !=NULL)
current->link.data=value;
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::addAfter(Type& value)
{
if(current==NULL)
return;
else if(current->link==NULL)
current->link=new ListNode<Type>(value,NULL);
else
{
ListNode<Type> *p=current->link;
p->link=new ListNode<Type>(value,p->link);
}
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::addBefore(Type& value)
{
if(current == NULL)
return;
else if(current->link ==NULL)
current->link=new ListNode<Type>(value,NULL);
else
current->link=new ListNode<Type>(value,current->link);
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::removeCurrent()
{
if(current==NULL && current->link==NULL)
{
cerr<<"无法删除!当前节点后已无节点"<<endl;
exit(1);
}
ListNode<term> *p=current->link;
current->link=p->link;
delete p;
}
//////////////////////////////////////////////////////////////////////////
template<class Type>
void ListIterator<Type>::Locate(Type& x)
{
init();
while(current->link!=NULL && current->link->data<x)
current=current->link;
}
//////////////////////////////////////////////////////////////////////////
//游标类定义结束
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -