简单数据结构之循环链表(C++实现)
2015-06-16 14:49
721 查看
/* ============================================================================ Name : Circular link.cpp Author : ntsk13 beijiwei@qq.com Version : Copyright : GPL Description : circular Link list study, complement by C++ Date : 2015.06.16 ============================================================================ */ #include <iostream> using namespace std; typedef struct { int data; }Elem_t; typedef struct lnode{ Elem_t e; struct lnode * next; }node,*pnode; class Link { public: void init(pnode & L); void clear(pnode & L); int get_length(pnode &L); Elem_t get_elem(pnode &L,int i); void traverse(pnode &L); void insert(pnode &L,Elem_t e,int i); void delete_elem(pnode &L,int i); }; int main(void) { pnode H=NULL;//head p Elem_t zero,one,two,three,four,five; zero.data=0; one.data=1; two.data=2; three.data=3; four.data=4; Link list; list.init(H); list.insert(H,zero,0); list.insert(H,one,1); list.insert(H,two,2); list.insert(H,three,3); list.insert(H,four,4); list.traverse(H); cout<<"len is "<<list.get_length(H)<<endl; five=list.get_elem(H,4); cout<<"================================="<<endl; cout<<"five is "<<five.data<<endl; list.delete_elem(H,4); cout<<"================================="<<endl; list.traverse(H); cout<<"len is "<<list.get_length(H)<<endl; list.insert(H,two,0); list.insert(H,five,2); cout<<"================================="<<endl; list.traverse(H); cout<<"len is "<<list.get_length(H)<<endl; return 0; } void Link::init(pnode & L) { L=new node;//构造一个头结点 (L->e).data =0;//头结点的data作为长度 L->next=L; } void Link::clear(pnode & L) { int i=0; pnode p=L; pnode tmp=NULL; int len=(L->e).data; for(i=0;i<len;i++) { tmp=p->next; delete p; p=tmp; } L=NULL; } int Link::get_length(pnode & L) { return (L->e).data;; } Elem_t Link::get_elem(pnode & L,int i) { int j=0; pnode p=L->next; while(j <i ) { j++; p=p->next; } return p->e; } void Link::traverse(pnode & L) { int i=0; pnode p=L->next;//避免把头指针元素输出 /* 1) 以长度作为循环链表是否结束的依据 * int len= (L->e).data; for(i=0;i< len;i++) { cout<<"The "<<i<<"th elem is "<<(p->e).data<<endl; p=p->next; } */ //2)以 next 指针指向 头指针 作为结束标识 while(p != L) { cout<<"The "<<i<<"th elem is "<<(p->e).data<<endl; p=p->next; } } void Link::insert(pnode & L,Elem_t e,int i)// before i insert { int j=0; pnode p=L; pnode tmp=NULL; for(j=0;j< i;j++) { p=p->next; } tmp=new node; (tmp->e).data=e.data; tmp->next=p->next; p->next=tmp; ( L->e).data++; } void Link::delete_elem(pnode & L,int i) { int j=0; pnode p=L->next; pnode tmp=NULL; for(j=0;j< i-1;j++) { p=p->next; } tmp=p->next;//将要被free的节点 p->next=p->next->next;//下一节点指向 下下一节点 (L->e).data--;//长度减一 delete tmp; }
相关文章推荐
- 线程的数据结构
- 常见排序算法时间对比 分类: C/C++ 数据结构与算法 2015-06-16 10:26 167人阅读 评论(0) 收藏
- 数据结构之---C语言实现二叉树的三叉链表存储表示
- Android 打造任意层级树形控件 考验你的数据结构和设计
- 数据结构--栈和队列
- 数据结构系列之平衡树(AVL构建法)
- 数据结构和算法-008 链表
- Map结构总结
- 求一个数据结构C语言 关于关键字统计源代码
- Android开发中高效的数据结构
- 数据结构算法代码实现——栈和队列(二)
- scu oj 4441 Necklace 2015年四川省赛F题(dp+数据结构)
- 自己写个list玩玩 分类: C/C++ 数据结构与算法 2015-06-15 16:30 91人阅读 评论(0) 收藏
- 数据结构系列之堆排序(基于数组实现)
- 数据结构第二讲:线性结构
- 数据结构实验(四)—— 排序
- 数据结构实验(三)——线性表的查找
- 数据实验结构(二)——栈和队列的设计与实现
- 数据结构实验(一)——顺序表和单链表的设计与实现
- 简单数据结构之链式表(C++实现)