单向循环链表C++

#include <iostream>
using namespace std;
typedef int valuetype;
typedef struct _node{
    valuetype data;
    _node* pnext;
    _node(valuetype e, _node* p = nullptr):data(e),pnext(p){}
}node;

class circlelist{
public:
    circlelist();
    ~circlelist();

    void append(valuetype e);
    void insert(int index, valuetype e);
    void remove(valuetype e);
    void earse(int index);
    valuetype serach(int index);
    int find(valuetype e);
    void replace(valuetype olddata, valuetype newdata);
    void modify(int index, valuetype newdata);

    int empty(){ return m_head == nullptr; }
    int length(){ return m_length; }
    void print();
private:
    node* m_head;
    node* m_tail;
    int m_length;
};

circlelist::circlelist() :m_head(new node(valuetype())), m_tail(m_head), m_length(0){}
circlelist::~circlelist(){
    while (m_head->pnext != m_head){
        node* pdel = m_head->pnext;
        m_head->pnext = pdel->pnext; 
        delete pdel;
    }
    delete m_head;
    m_head = nullptr;

}

void circlelist::append(valuetype e){
    ++m_length; 
    m_tail->pnext = new node(e);
    m_tail = m_tail->pnext;
    m_tail->pnext = m_head; 
}
void circlelist::insert(int index, valuetype e){
    if (index < 0 || index > length()){
        cout << "index is out of range.\n";
    }
    else{
        node* pnew = new node(e);
        node* p = m_head;
        for (int i = 0; i < index; ++i){
            p = p->pnext;
        }
        pnew->pnext = p->pnext;
        p->pnext = pnew;
        if (pnew->pnext == m_head){
            m_tail = pnew;
        }
        ++m_length;
    }
}
void circlelist::remove(valuetype e){
    node* p = m_head;
    while (p->pnext != m_head){
        if (p->pnext->data == e){
            node* pdel = p->pnext;
            p->pnext = pdel->pnext;
            delete pdel;
            --m_length;
        }
        else{
            p = p->pnext;
        }
    }

}
void circlelist::earse(int index){
    if (index < 0 || index > m_length - 1){
        cout << "index is out of range.\n";
    }
    else{
        node* p = m_head;
        for (int i = 0; i < index; ++i){
            p = p->pnext;
        }
        node* pdel = p->pnext;
        p->pnext = pdel->pnext;
        delete pdel;
        --m_length;
    }
}
valuetype circlelist::serach(int index){
    if (index < 0 || index > m_length - 1){
        cout << "index is out of range.\n";
        return valuetype();
    }
    else{
        node* p = m_head;
        for (int i = 0; i < index; ++i){
            p = p->pnext;
        }
        return  p->pnext->data;
    }
}
int  circlelist::find(valuetype e){ 
    node* p = m_head;
    for (int i = 0; i < m_length; ++i){
        if (p->pnext->data == e){
            return i;
        }
        p = p->pnext;
    }
    return  -1; 
}
void circlelist::replace(valuetype olddata, valuetype newdata){
    node* p = m_head;
    for (int i = 0; i < m_length; ++i){
        if (p->pnext->data == olddata){
            p->pnext->data = newdata;
        }
        p = p->pnext;
    }
}
void circlelist::modify(int index, valuetype newdata){
    if (index < 0 || index > m_length - 1){
        cout << "index is out of range.\n";
    }
    else{
        node* p = m_head;
        for (int i = 0; i < index; ++i){
            p = p->pnext;
        }
        p->pnext->data = newdata;
    }
}

void circlelist::print(){
    node* p = m_head;
    while (p->pnext != m_head){
        cout << p->pnext->data << ends;
        p = p->pnext;
    }
    cout << endl;
}

int main(){
    circlelist cli;
    for (int i = 0; i < 10; ++i){
        cli.append(i);
    }
    cli.print();

    cout << "cli 前后插入 -1:"  << endl;
    cli.insert(0, -1);
    cli.insert(cli.length(), -1);
    cli.print();

    cout << "替换所有的-1为-111:"  << endl;
    cli.replace(-1, -111);
    cli.print();

    cout << "移除所有的-111:"  << endl;
    cli.remove(-111);
    cli.print();
    cout << "修改第6个为222:" << endl;
    cli.modify(5, 222);
    cli.print();
    cout << "移除第5个" << endl;
    cli.earse(4);
    cli.print();

    cout << "index 4 is :" << cli.serach(4) << endl
        << "222 index is: " << cli.find(222) << endl;
    return 0;
}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值