单向循环链表

CircularLsit.h

#pragma once
//循环链表
template<class Type>
class List;

template<class Type>
class ListIterator;

//链表节点
template<class Type>
class ListNode
{
public:
    friend class List<Type>;
    friend class ListIterator<Type>;
private:
    Type data;
    ListNode *link;
    ListNode(Type);
    ListNode(){};//默认构造函数
};

//循环链表
template<class Type>
class List
{
    friend class ListIterator<Type>;
public:
    List();
    void Insert(Type);
    void Delete(Type);

private:
    ListNode<Type> *first;
};

template<class Type>
class ListIterator
{
public:
    ListIterator(const List<Type>& l) :list(l), current(l.first->link)
    {
    }
    //用来判断链表迭代器所指向的链表里的当前的节点是否为空
    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 != list.first)
    {
        return true;
    }
    else
    {
        return false;
    }

}

//用来判断当前节点的下一个节点是否为空
template<class Type>
bool ListIterator<Type>::NextNotNull()
{
    if (current->link != list.first)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//指向链表的第一个节点,用来返回链表的第一个节点的指针
template<class Type>
Type* ListIterator<Type>::First()
{
    if (current != list.first)//当前节点不等于first,因为first是空的
    {
        return &current->data;
    }
    else
    {
        return 0;//没有数据
    }
}

//返回当前节点的下一个节点的指针
template<class Type>
Type* ListIterator<Type>::Next()
{
    current = current->link;
    if (current == list.first)//current在循环时候,如果等于first(当current位于最后一个元素时候移动一下则就可能等于first),则让其再移动一次
    {
        current = current->link;
    }

    return &current->data;
}

/
template<class Type>
ListNode<Type>::ListNode(Type element) :data(element)
{
    link = 0;
}


/
template<class Type>
List<Type>::List()
{
    //first = 0;//单项链表初始化方式
    first = new ListNode < Type > ;//new一个空节点
    first->link = first;// 刚开始链表中没有数据,所以first的下一个节点为本身
}

template<class Type>
void List<Type>::Insert(Type k)
{
    ListNode<Type> *newnode = new ListNode<Type>(k);
    newnode->link = first->link;//新的节点link指向表头的下一个
    first->link = newnode;
}

template<class Type>
void List<Type>::Delete(Type k)
{
    ListNode<Type> *previous = first;//从first开始,(前一个
    ListNode<Type> *current;//用来做循环
    for (current = first->link;//因为first是空表头,没有数据,所以从它的下一个开始
        (current!=first) && current->data != k; //循环条件:当前不等于first(防止循环一圈重复循环)且当前节点data不等于要找的节点,则继续循环
        previous = current, current = current->link //步长,满足继续循环情况下,在增加步长之前,记录前一个节点
        )
    {
        //什么都不做,空循环。找到要被删除的节点
    }

    //如果current不等于first,找到了节点;如果等于first则说明没找到
    if (current!=first )
    {
        previous->link = current->link;
        delete current;
    }
}


Main.cpp:

#include <iostream>
#include "List.h"
#include <list>
using namespace std;

int main()
{
    List<int> intList;
    intList.Insert(5);
    intList.Insert(15);
    intList.Insert(25);
    intList.Insert(35);
    cout << "my iterator\n";
    ListIterator<int> li(intList);
    
    if (li.NotNull())//检查一下链表是否为空
    {
        cout << *li.First();
        while (li.NextNotNull())
        {
            cout << "->" << *li.Next();
        }
        cout << endl;
    }
    //证明是循环链表
    cout << "测试一下循环" << endl;
    ListIterator<int> iter(intList);
    cout << *iter.First() << endl;
    cout << *iter.Next() << endl;
    cout << *iter.Next() << endl;
    cout << *iter.Next() << endl;
    cout << *iter.Next() << endl;
    cout << *iter.Next() << endl;
    cout << *iter.Next() << endl;

    
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值