【C++实现循环单链表】

概要:本期学习循环单链表的结构、和普通单链表的区别以及循环单链表的实现。

一、循环单链表长啥样?

循环单链表,根据字面理解就是可以循环的、围成圆圈的单链表。实际上,它的数据结构模型就是首尾相连的单链表,如下图所示:
在这里插入图片描述

二、循环单链表有哪些特点?

循环单链表区别于普通单链表,它的尾结点指向头结点。因此,它可以在从链表的任意位置访问到链表的任意节点

三、循环单链表的实现

0.循环单链表的结构以及初始化
class CyclicSingleLinkedList
{
private:
    class Node{
    public:
        QString data;
        Node *next;
    };
    Node *head;
    int length;

public:
    CyclicSingleLinkedList();
};

CyclicSingleLinkedList::CyclicSingleLinkedList()
{
    head = new Node;
    head->next = head;//循环单链表的尾结点指向头结点
    head->data = "";
    length = 0;
}
1.创建循环单链表
//创建n个节点的循环单链表
void CyclicSingleLinkedList::CreateCyclicSingleLinkedList(int n)
{
    Node *pre = head;
    length = n;
    int i = 0;
    while (i < n) {
        qDebug()<< QStringLiteral("请输入第") << i << QStringLiteral("个结点的数据:");
        Node *cur = new Node;
        string str;
        cin >> str;
        cur->data = QString::fromStdString(str);
        ++i;
        pre->next = cur;
        pre = cur;
        cur->next = head;
    }
}
2.打印循环单链表
//打印循环单链表
void CyclicSingleLinkedList::DisplayCyclicSingleLinkedList()
{
    Node *_node = head->next;//节点1
    if(length == 0 || _node == head)
    {
        qDebug()<<QStringLiteral("链表为空!")<<endl;
        return ;
    }
    int i = 0;
    while(_node)
    {
        qDebug()<<QStringLiteral("第")<<i++<<QStringLiteral("个节点的数据:")<<_node->data<<endl;
        if(_node->next == head)//判断条件不同
        {
            return ;
        }
        _node = _node->next;
    }
}
3.查询节点位置
//查询节点位置
int CyclicSingleLinkedList::GetNodeIndex(CyclicSingleLinkedList::Node *_simpleNode)
{
    Node *_node = head->next;//节点1
    if(length == 0 || _node == head)
    {
        qDebug()<<QStringLiteral("链表为空!")<<endl;
        return -1;
    }

    int i = 0;
    while(_node)
    {
        if(_node->data == _simpleNode->data)
        {
            qDebug()<<QStringLiteral("节点位置为:")<< i <<endl;
            return i;
        }
        if(_node->next == head)
        {
            qDebug()<<QStringLiteral("节点不存在!")<<endl;
            return -1;
        }
        _node = _node->next;
        i++;
    }
}
4.在循环单链表中插入节点
//在指定节点后插入新节点
void CyclicSingleLinkedList::BackInsertNodeToCyclicSingleLinkedList(int index, QString data)
{
    if(index < 0 || index >=length)
    {
        qDebug()<<QStringLiteral("输入位置有误!")<<endl;
        return ;
    }
    Node *_node = new Node;
    if(length == 0)
    {
        _node = head;
    }
    else
    {
        _node = head->next;
    }
    int i = 0;
    while(_node)
    {
        if(i == index)
        {
            Node* _nextNode = _node->next;//记录未插入是index位置的下一节点
            Node *_newNode = new Node;//创建新节点
            _newNode->data = data;
            _node->next = _newNode;
            _newNode->next = _nextNode;
            qDebug()<<QStringLiteral("节点插入成功!")<<endl;
            length++;
            return ;
        }
        _node = _node->next;
        i++;
    }
}
5.删除循环单链表中的指定节点
//删除指定位置节点
void CyclicSingleLinkedList::DeleteNodeFromCyclicSingleLinkedList(int index)
{
    if(index < 0 || index >=length)
    {
        qDebug()<<QStringLiteral("输入位置有误!")<<endl;
        return ;
    }
    Node *_node = new Node;
    if(length == 0)
    {
        qDebug()<<QStringLiteral("链表为空,无法进行删除操作!")<<endl;
        return ;
    }
    else
    {
        _node = head->next;
    }
    int i = 0;
    while(_node)
    {
        if(i == index-1)//当前为index位置前一节点
        {
            Node* _delNode = _node->next;
            Node* _nextNextNode = _node->next->next;//记录index位置的下一节点
            _node->next = _nextNextNode;
            delete _delNode;
            _delNode = NULL;
            qDebug()<<QStringLiteral("节点删除成功!")<<endl;
            length --;
            return ;
        }
        if(_node->next == nullptr)
        {
            qDebug()<<QStringLiteral("节点删除失败!")<<endl;
            return ;
        }
        _node = _node->next;
        i++;
    }
}

结尾

对于循环单链表的介绍就到这,下期继续学习双链表:)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛狂的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值