使用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

目录
相关文章
|
4月前
|
C++
【链表】还不会用C++实现链表?一文教会你各种链表的实现
【链表】还不会用C++实现链表?一文教会你各种链表的实现
230 0
|
4月前
|
存储
数据结构第二课 -----线性表之单向链表
数据结构第二课 -----线性表之单向链表
|
1月前
|
存储 JavaScript 前端开发
JavaScript实现单向链表
JavaScript实现单向链表
15 0
|
2月前
|
存储 C++
C++的list-map链表与映射表
```markdown C++ 中的`list`和`map`提供链表和映射表功能。`list`是双向链表,支持头尾插入删除(`push_front/push_back/pop_front/pop_back`),迭代器遍历及任意位置插入删除。`map`是键值对集合,自动按键排序,支持直接通过键来添加、修改和删除元素。两者均能使用范围for循环遍历,`map`的`count`函数用于统计键值出现次数。 ```
26 1
|
3月前
|
存储 算法
【单向链表】数据结构——单向链表的介绍与代码实现&笔记
【单向链表】数据结构——单向链表的介绍与代码实现&笔记
|
3月前
|
存储 C++
C++的list-map链表与映射表
这篇教程介绍了C++中`list`链表和`map`映射表的基本使用。`list`链表可通过`push_front()`、`push_back()`、`pop_front()`和`pop_back()`进行元素的添加和删除,使用迭代器遍历并支持在任意位置插入或删除元素。`map`是一个键值对的集合,元素自动按键值排序,可使用下标操作符或`insert()`函数插入元素,通过迭代器遍历并修改键值对,同时提供`count()`方法统计键值出现次数。教程中包含多个示例代码以帮助理解和学习。
29 2
|
3月前
|
算法 C语言
数据结构——单向链表(C语言版)
数据结构——单向链表(C语言版)
37 2
|
3月前
|
Java
单向环形链表-约瑟夫问题(java)
单向环形链表-约瑟夫问题(java)
|
3月前
|
存储 算法 前端开发
【C/数据结构与算法】:链表的实现(单向链表+双向链表)
【C/数据结构与算法】:链表的实现(单向链表+双向链表)
21 0