使用C++实现的单向循环链表

简介: 版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/44682285 不多说直接上代码。
版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/44682285

不多说直接上代码。

注意:其中的Exception类请参考我的博客《使用C++实现的单向链表》

#ifndef CIRCULARCHAIN_H
#define CIRCULARCHAIN_H

#include "Exception.h"
#include 

using namespace std;

template
class Chain;

template
class ChainNode{
    friend class Chain;
private:
    T data;
    ChainNode* next;
};

template
class Chain{
public:
    Chain(){
        head = new ChainNode();
        head->data = NULL;
        head->next = head;
    }

    ~Chain();
    int Length();
    bool isEmpty(){return head->next == head;}
    bool Find(int k,T& x) const;
    int Search(const T& x);
    Chain& Delete(int k,T& x);
    Chain& Insert(int k,const T& x);
    void print();
private:
    ChainNode* head;
};

template
Chain::~Chain()
{
    if(head->next == head)
        return;

    ChainNode* current = head->next;

    while(current != head){
       head->next = current->next;
        delete current;
        current = head->next;
    }
}

template
int Chain::Length()
{
    int index = 0;
    ChainNode *current = head;
    while(current->next !=  head){
        index++;
        current = current->next;
    }

    return index;
}

template
bool Chain::Find(int k, T &x) const
{
    if(k < 1)
        throw OutOfBounds();

    if(head->next == head)
        throw OutOfBounds();

   ChainNode* current = head->next;
    for(int i = 1;i < k && current  != head; i++){
        current = current->next;
    }

    if(current == head)
        return false;

    x = current->data;
    return true;
}

template
int Chain::Search(const T &x)
{
    if(head->next == head)
        return -1;

    ChainNode* current = head->next;

    int index = 1;

    while(current->next != head && current->data != x){
        current = current->next;
        index++;
    }

    if(current == head)
       return -1;

    return index;
}

template
Chain& Chain::Delete(int k, T &x)
{
    if(k < 1 || head->next == head)
        throw OutOfBounds();

    ChainNode* p = head->next;

    ChainNode* q = head;

    for(int i = 1;i < k && p != head; i++){
        q = p;
        p = p->next;
    }

    if(p == head)
        throw OutOfBounds();

    q->next = p->next;
    x = p->data;

    delete p;

    return *this;
}

template
Chain& Chain::Insert(int k, const T &x)
{
    if(k < 0)
            throw OutOfBounds();

        if(k > 0 && head->next == head)
            throw OutOfBounds();

        ChainNode* temp = new ChainNode();
        temp->data = x;

        ChainNode* p = head;

        int i;
        for(i = 0;i < k && p->next != head;i++){
            p = p->next;
        }

        if(i < k-1)
            throw OutOfBounds();

        if(k == 0){
            temp->next = head->next;
            head->next = temp;
        }else{
            temp->next = p->next;
            p->next = temp;
        }

        return *this;
}

template
void Chain::print()
{
    if(head->next == head)
        return;

    ChainNode* current = head->next;
    while(current != head){
        std::cout << current->data << " ";
        current = current->next;
    }

    std::cout << std::endl;
}

#endif // CIRCULARCHAIN_H
AI 代码解读

目录
打赏
0
0
0
0
2
分享
相关文章
Python 实现单向链表,和单向链表的反转
链表是一种数据结构,每个节点存储相邻节点的位置信息。单链表中的节点仅存储下一节点的位置。通过Python实现单链表,定义`ListNode`类并关联节点可创建链表。例如,创建A-&gt;B-&gt;C的链表后,可通过反转函数`reverse`将链表反转为CBA。代码展示了如何实现和操作单链表。
Python 实现单向链表,和单向链表的反转
公司监控上网软件架构:基于 C++ 链表算法的数据关联机制探讨
在数字化办公时代,公司监控上网软件成为企业管理网络资源和保障信息安全的关键工具。本文深入剖析C++中的链表数据结构及其在该软件中的应用。链表通过节点存储网络访问记录,具备高效插入、删除操作及节省内存的优势,助力企业实时追踪员工上网行为,提升运营效率并降低安全风险。示例代码展示了如何用C++实现链表记录上网行为,并模拟发送至服务器。链表为公司监控上网软件提供了灵活高效的数据管理方式,但实际开发还需考虑安全性、隐私保护等多方面因素。
78 0
公司监控上网软件架构:基于 C++ 链表算法的数据关联机制探讨
【C++数据结构——线性表】单链表的基本运算(头歌实践教学平台习题)【合集】
本内容介绍了单链表的基本运算任务,涵盖线性表的基本概念、初始化、销毁、判定是否为空表、求长度、输出、求元素值、按元素值查找、插入和删除数据元素等操作。通过C++代码示例详细解释了顺序表和链表的实现方法,并提供了测试说明、通 - **任务描述**:实现单链表的基本运算。 - **相关知识**:包括线性表的概念、初始化、销毁、判断空表、求长度、输出、求元素值、查找、插入和删除等操作。 - **测试说明**:平台会对你编写的代码进行测试,提供测试输入和预期输出。 - **通关代码**:给出了完整的C++代码实现。 - **测试结果**:展示了测试通过后的预期输出结果。 开始你的任务吧,祝你成功!
275 5
|
9月前
|
【初阶数据结构】深入解析单链表:探索底层逻辑(无头单向非循环链表)(一)
【初阶数据结构】深入解析单链表:探索底层逻辑(无头单向非循环链表)
102 1
|
9月前
|
数据结构与算法学习六:单向环形链表应用实例的约瑟夫环问题
这篇文章通过单向环形链表的应用实例,详细讲解了约瑟夫环问题的解决方案,并提供了Java代码实现。
121 0
【初阶数据结构】深入解析单链表:探索底层逻辑(无头单向非循环链表)(二)
【初阶数据结构】深入解析单链表:探索底层逻辑(无头单向非循环链表)
C++的list-map链表与映射表
```markdown C++ 中的`list`和`map`提供链表和映射表功能。`list`是双向链表,支持头尾插入删除(`push_front/push_back/pop_front/pop_back`),迭代器遍历及任意位置插入删除。`map`是键值对集合,自动按键排序,支持直接通过键来添加、修改和删除元素。两者均能使用范围for循环遍历,`map`的`count`函数用于统计键值出现次数。 ```
176 1
JavaScript实现单向链表
JavaScript实现单向链表
68 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问