数据结构—迭代器

List.h头文件源代码

#ifndef _LIST_H_

#define _LIST_H_

 

//迭代器的前置声明

template

class ListInterator;

 

//链表类的前置声明

template

class List;

 

template

class ListNode    //节点类

{

    friend class List;

    friend class ListInterator;

    private:

        Type data;    //节点存储数据

        ListNode* link;    //指向下一个节点的指针

        ListNode(Type);        //节点的构造函数

};

 

template

class List    //链表类

{

    friend class ListInterator;

    public:

        List(){first=0;};    //链表的构造函数

        void Insert(Type);    //插入节点

        void Delete(Type);    //删除节点

        void Invert();    //反转链表

        void Concatenate(List);    //合并链表

        void Show();

        

    private:

    ListNode* first;

};

 

 

template

class ListInterator    //链表迭代器类

{

    public:

        ListInterator(const List& l):list(l),current(l.first){}

        bool NotNull();    //判断当前节点是否为空

        bool NextNotNull();    //当前节点的下一个节点是否为空

        Type* First();

        Type* Next();

    private:

        const List& list;

        ListNode* current;

};

 

//节点构造函数的实现

template

ListNode::ListNode(Type elemnt)

{

    data=elemnt;

    link=0;

}

 

//链表类插入函数的实现

template

void List::Insert(Type k)

{

    ListNode* newnode=new ListNode(k);

    newnode->link=first;    //原来的头指针后移

    first=newnode;        //插入的新的节点指针成为头指针

    

}

 

template

void List::Delete(Type k)

{

    ListNode* previous=0;    //保存前一个节点

    ListNode* 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

void List::Invert()

{

    ListNode* p=first;    //声明两个指针,这个指针指向头节点

    ListNode* q=0;    //这个指针指向尾节点

    while(p)

    {

        ListNode* r=q;

        q=p;

        p=p->link;

        q->link=r;

    }

    first=q;

}

 

//链表类中实现两个链表的连结

template

void List::Concatenate(List b)

{

    if(!first)

    {

        first=b.first;

        return;

    }

    if(b.first)

    {

        ListNode* p;

        for(p=first;p->link;p=p->link);    //空循环,寻找第一个链表的尾节点

        p->link=b.first;

        

    }

}

 

 

template

void List::Show()

{

    for(ListNode*current=first;current;current=current->link)

    {

        std::cout<<current->data;

        if(current->link)

            std::cout<<" -> ";

    }

    std::cout<<std::endl;

}

 

template

bool ListInterator::NotNull()

{

    if(current)

        return true;

    else

        return false;

}

 

template

bool ListInterator::NextNotNull()

{

    if(current&&current->link)

        return true;

    else

        return false;

}

 

template

Type* ListInterator::First()

{

    if(list.first)

        return &list.first->data;

    else

        return 0;

}

 

template

Type* ListInterator::Next()

{

    if(current)

    {

        current=current->link;

        return &current->data;

    }

    else

        return 0;

}

 

#endif

主程序源代码:

#include

#include

#include"List.h"

 

using namespace std;

 

int main()

{

    list listIntegers;

    listIntegers.push_front(5);

    listIntegers.push_front(15);

    listIntegers.push_front(25);

    listIntegers.push_front(35);

    list::iterator i=listIntegers.begin();    //定义链表的迭代器

    while(i!=listIntegers.end())

    {

        cout<<*i<<" -> ";

        ++i;

    }

    cout<<endl;

    List intList;

    intList.Insert(5);

    intList.Insert(15);

    intList.Insert(25);

    intList.Insert(35);

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

    ListInterator li(intList);

    if(li.NotNull())

    {

        cout<<*li.First();

        while(li.NextNotNull())

            cout<<" -> "<<*li.Next();

    }

    cout<<endl;

    intList.Delete(15);

    intList.Show();

    List charList;

    charList.Insert('a');

    charList.Insert('b');

    charList.Insert('c');

    charList.Insert('d');

    charList.Show();

    charList.Invert();

    charList.Show();

    List char2List;

    char2List.Insert('e');

    char2List.Insert('f');

    char2List.Show();

    char2List.Invert();

    char2List.Show();

    charList.Concatenate(char2List);

    charList.Show();

    return 0;

}

运行结果:

 

数据结构—迭代器

转载于:https://www.cnblogs.com/liusuanyatong/p/11259794.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值