C++复习:迭代器

template<class T>
class ListNode {
public:
    ListNode(const T &theData,ListNode<T>* afterMe);
    ListNode<T>* getLink();
    void setLink(ListNode<T> *node);
    const T getData();
    virtual ~ListNode();
private:
    ListNode<T> *next;
    T data;

};

template<class T>
ListNode<T>::ListNode(const T &theData,ListNode<T>* afterMe){
    data = theData;
    if(afterMe) afterMe->setLink(this);
    next = NULL;
}
template<class T>
ListNode<T>::~ListNode(){

}
template<class T>
ListNode<T>* ListNode<T>::getLink(){
    return next;
}
template<class T>
void ListNode<T>::setLink(ListNode<T> *node){
    next = node;
}
template<class T>
const T ListNode<T>::getData(){
    return data;
}

template<class T>
class ListIterator {
public:
    ListIterator(ListNode<T>* thePtr);
    virtual ~ListIterator();
    const ListIterator<T> operator++(int);
    const ListIterator<T> operator++();
    bool operator==(const ListIterator rSide);
    bool operator!=(const ListIterator rSide);
    const T operator*();
private:
    ListNode<T> *ptr;

};

template<class T>
ListIterator<T>::ListIterator(ListNode<T> *thePtr) {
    // TODO Auto-generated constructor stub
    ptr = thePtr;
}
template<class T>
const ListIterator<T> ListIterator<T>::operator++(int){
    ListIterator<T> t = *this;
    ptr = ptr->getLink();
    return t;
}
template<class T>
const ListIterator<T> ListIterator<T>::operator++(){
    ptr = ptr->getLink();
    return *this;
}
template<class T>
const T ListIterator<T>::operator*(){
    return ptr->getData();
}

template<class T>
bool ListIterator<T>::operator ==(ListIterator<T> rSide){
    return ptr == rSide.ptr;
}
template<class T>
bool ListIterator<T>::operator !=(ListIterator<T> rSide){
    return (ptr != rSide.ptr);
}
template<class T>
ListIterator<T>::~ListIterator() {
    // TODO Auto-generated destructor stub
}

namespace MyNameSpace{
template<class T>
class LinkedList {
public:
    typedef ListIterator<T> Iterator;
    LinkedList();
    const Iterator begin(){return Iterator(head);}
    const Iterator end(){return Iterator(NULL);}
    int getCount();
    const T front();
    const T back();
    void orderInsert(const T &theData);
    void headInsert(const T &theData);
    void tailInsert(const T &theData);
    void deleteNode(int pos);
    void print();
    virtual ~LinkedList();
private:
    ListNode<T>* getNodeReference(int pos);
    bool locate(const T &data,ListNode<T>* &before);
    ListNode<T> *head,*tail;
};

}

namespace MyNameSpace{

template <class T>
LinkedList<T>::LinkedList():head(NULL),tail(NULL){

}
template <class T>
LinkedList<T>::~LinkedList() {
    ListNode<T> *p,*q;
    p = head;
    while(p != NULL){
        q = p;
        p = p->getLink();
        delete q;
    }
}
template <class T>
int LinkedList<T>::getCount(){
    ListNode<T> *p;
    p = head;
    int size = 0;
    while(p != NULL){
        p = p->getLink();
        size++;
    }
    return size;
}
template <class T>
const T LinkedList<T>::front(){
    return head->getData();
}
template <class T>
const T LinkedList<T>::back(){
    return tail->getData();
}
template <class T>
void LinkedList<T>::orderInsert(const T& theData){
    ListNode<T> *before;
    bool in = locate(theData,before);
    if(in) return;
    if(before == NULL){
        headInsert(theData);
    }else{
        ListNode<T> *node = new ListNode<T>(theData,NULL);
        node->setLink(before->getLink());
        before->setLink(node);
        if(before == tail) tail = node;
    }
}
template <class T>
void LinkedList<T>::headInsert(const T& theData){
    ListNode<T> *node = new ListNode<T>(theData,NULL);
    node->setLink(head);
    if(head) head = node;
    else head = tail = node;
}
template <class T>
void LinkedList<T>::tailInsert(const T &theData){
    ListNode<T> *node = new ListNode<T>(theData,NULL);
    if(tail){
        tail->setLink(node);
        tail = node;
    }else{
        head = tail = node;
    }
}
template <class T>
void LinkedList<T>::deleteNode(int pos){
    if(pos < 1 || pos > getCount())return;
    if(pos == 1){
        ListNode<T> *tmp = head;
        head = head->getLink();
        if(!head) tail=NULL;
        delete tmp;
        return;
    }
    ListNode<T> *before = getNodeReference(pos-1);
    ListNode<T> *tmp = before->getLink();
    before->setLink(tmp->getLink());
    delete tmp;
}
template <class T>
void LinkedList<T>::print(){
    for(ListNode<T> *p = head;p!=NULL;p=p->getLink()){
        printf("%d",p->getData());
    }
}
template <class T>
ListNode<T>* LinkedList<T>::getNodeReference(int pos){
    ListNode<T> *p = head;
    for(int i = 1; i < pos; i++){
        p = p->getLink();
    }
    return p;
}
template <class T>
bool LinkedList<T>::locate(const T &data,ListNode<T>* &before){
    ListNode<T> *p,*q;
    p = q = head;
    while(p && p->getData() < data){
        q = p;
        p = p->getLink();
    }
    before = q;
    return (p) && (p->getData() == data);
}
}

#include <cstdio>
#include "LinkedList.cpp"
using MyNameSpace::LinkedList;
int main(int argc,char *argv[]){
    LinkedList<int> *list = new LinkedList<int>();
    for(int i = 0; i < 10; i++)
        list->orderInsert(i);
    list->tailInsert(10);
    list->orderInsert(11);
    for(LinkedList<int>::Iterator iter = list->begin(); iter != list->end(); iter++){
        printf("%d ",*iter);
    }
    printf("\n");
    delete list;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值