单向循环链表C++

来源:互联网 发布:傲剑降龙数据 编辑:程序博客网 时间:2024/06/05 00:11
#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;}

这里写图片描述