单向循环链表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;}
阅读全文
0 0
- 单向循环链表C/C++版
- c实现逆转单向循环链表
- 单向循环链表C语言实现
- 【C语言】单向循环链表
- c语言实现单向循环链表
- 单向循环链表C语言实现
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 循环单向链表
- 单向循环链表
- 循环单向链表
- 单向链表循环
- 循环单向链表
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 微信小程序仿摩拜单车
- fdsfds
- H
- 【PAT】【Advanced Level】1020. Tree Traversals (25)
- [mysql]如何重置或设置mysql的自增id值
- 单向循环链表C++
- POJ 3258(二分——最大化最小值)
- POJ 3258(二分——最大化最小值)
- 动态规划的优化
- struts2链接数据库的简单登陆验证
- input 框上传多个文件以及文件校验
- 基于MATLAB利用模拟退火算法解决费马点问题
- 数据结构之AOV网与拓扑排序
- 新手上路之oracle 增,删,改,查操作 笔记