数据结构与算法(C语言版)__循环链表

单向循环链表
带有表头结构的循环链表
这里写图片描述

链表的最后一个节点指向第一个节点
第一个节点是表头结构,不保存数据的。做链表有两种做法,一种是带有表头结构,一种是不带表头结构的,那么循环链表就是使用带表头结构的,表头就是一个节点,但是节点里面没有数据。空循环链表自己指向自己,空循环链表里面加入数据时就是循环链表。

今天依然使用前面的做好的代码基础上写:

在VS2013中新建项目,在头文件中加入MyList.h在原文件中加入main.cpp

//MyList.h
#ifndef MYLIST_H
#define MYLIST_H

#include<iostream>

//循环链表
template<class Type> class List;
template<class Type> class ListIterator;
template<class Type>
class ListNode{
    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(){ first = new ListNode<Type>; first->link = first; };
    void Delete(Type);
    void Insert(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)return &current->data;
    else return 0;
}

template<class Type>
Type* ListIterator<Type>::Next(){
    current = current->link;
    if (current == list.first) current = current->link;//当下一个是first时,让他在下一个
    return &current->data;
}
//插入是往最前面插入
template<class Type>
void List<Type>::Insert(Type k){
    ListNode<Type> *newnode = new ListNode<Type>(k);//k是存放节点里面的数据;
    newnode->link = first->link;
    first->link = newnode;
}

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

template<class Type>
void List<Type>::Delete(Type k){
    ListNode<Type> *previous = first;//前一个
    ListNode<Type> *current;
    for (current = first->link; (current != first) && current->data != k; previous = current, current = current->link){
        ;//什么都不做,空循环,找到要被删除的节点
    }
    if (current != first){
        /*if (previous){
            previous->link = current->link;
        }
        else{
            first = first->link;
        }*/
        previous->link = current->link;
        delete current;
    }
}

#endif
//main.cpp
#include<iostream>
#include<list>//C++STL中的链表
#include "MyList.h"
using namespace std;

int main(){
    cout << "测试:" << endl;
    cout << "这是我的链表和迭代器:" << endl;
    List<int> initList;
    initList.Insert(5);
    initList.Insert(15);
    initList.Insert(25);
    initList.Insert(35);
    ListIterator<int>li(initList);
    //迭代是一种手段,可以一个一个的处理数据,想怎么处理就怎么处理
    if (li.NotNull()){
        cout << *li.First();
        while (li.NextNotNull())
            cout << " -> " << *li.Next();
        cout << endl;
    }

    cout << "测试循环" << endl;
    cout << *li.First() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    cout << *li.Next() << endl;
    system("pause");
    return 0;
}

总结:循环链表在特殊的算法中会用到,比如著名的约瑟夫问题就要用到循环链表来解决。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值