(5)循环链表
来源:互联网 发布:node 模块开发 编辑:程序博客网 时间:2024/05/21 16:55
将链表通过指针域首尾相接,即链表尾结点的指针域指向头结点形成的链表叫做循环链表。
由单链表构成叫做单循环链表。
单循环链表在任意一结点都可以访问其他节点,用指针是否为null判断是否到达链表尾。一般使用尾结点的指针来表示,
提高效率。
循环链表操作:
1)Length(L) 返回表L的长度,即表中元素个数
2)Prior(L,i) 取位置i的前驱元素
3)Next(L,i) 取位置i的后继元素
4)Locate(L,x) 这是一个函数,函数值为元素x在L中的位置
5)Insert(L,i,x)在表L的位置i处插入元素x,将原占据位置i的元素及后面的元素都向后推一个位置
6)Delete(L,p) 从表L中删除位置p处的元素
7)Get(L,i) 这是一个函数,函数值为L中位置i处的元素(1≤i≤n)
8)IsEmpty(L) 如果表L为空表(长度为0)则返回true,否则返回false
9)Clear(L)清除所有元素
10)Traverse(L)遍历输出所有元素
11)Update(L,i,x)修改元素
12)退出
13)清空命令行!
14)创建循环链表
代码:(由两个单链表构成循环链表,其中保留一个头结点作为尾结点)
#include<iostream>#include<cstdlib>#include<malloc.h>using namespace std;typedef int dataType;typedef struct Node{dataType data=-1;struct Node *next;}LNode,*LinkedList;//创建链表 LinkedList linkedListCreateT() {int i = 1;LinkedList l = (LNode*)malloc(sizeof(LNode));LinkedList p;l->next = NULL;while (i != 21) {p = (LNode*)malloc(sizeof(LNode));p->data = i;++i;p->next = l->next;l->next = p;}return l;}LinkedList create() {LinkedList p = linkedListCreateT();LinkedList l = linkedListCreateT();LinkedList lb = l;LinkedList pb = p->next;while (l->next != NULL) {l = l->next;}LinkedList le = l;le->next = pb;free(p);while (pb->next != NULL) {pb = pb->next;}LinkedList pe = pb;pe->next = lb;cout << "循环链表创建成功" << endl;return lb;}//表中元素个数int linkedListLength(LinkedList l) {int i=0;LinkedList p=l;p = p->next;while (p!=l) {i++;p=p->next;}return i+1;}//函数值为L中位置i处的元素void linkedListGet(LinkedList l,int i) {int j = 0;LinkedList p=l;while (j!=i) {++j;p = p->next;}cout << i << "位置的元素是:" << p->data << endl; }//取位置i的前驱元素void linkedListPrior(LinkedList l, int i) {int j = 0;LinkedList p=l;while(j!=i-1) {++j;p = p->next;}if (p==l) {cout << "位置:" << i << "的前驱元素为:尾结点" << endl;}else cout << "位置:" << i << "的前驱元素为:" << p->data << endl;}//取位置i的后继元素void linkedListNext(LinkedList l, int i) {int j = 0;LinkedList p=l;while(j!=i+1) {++j;p = p->next;}if (p == l){cout << "位置" << i << "的后驱是:尾结点"<< endl;}else cout << "位置" << i << "的后驱是:" << p->data << endl;}//元素x在L中的位置void linkedListLocate(LinkedList l, dataType x) {int i = 0,flag=0;LinkedList p=l;while(i!=linkedListLength(l)){++i;if (p->data == x) {cout << x << "在表中的位置是:" << i << endl;flag++;}p = p->next;}if(flag==0) cout<<"不在表中"<<endl;}//在表L的位置i处插入元素xLinkedList linkedListInsert(LinkedList l,int i,dataType x) {int j = 0;LinkedList pri=l,s=l;LinkedList p = (LNode*)malloc(sizeof(LNode));p->data = x; while (j != i) {++j;pri = s;s = s->next;}pri->next = p;p->next = s;cout << "插入成功!" << endl;return l;}//删除位置i处的元素LinkedList linkedListDelete(LinkedList l,int i) {int j = 0;LinkedList pri = l, p = l;while(j!=i){j++;pri = p;p = p->next;}pri->next = p->next;free(p);cout << "删除成功!" << endl;return l;}//表是否为空void linkedListEmpty(LinkedList l) {if (l->next==l) {cout << "是空表!" << endl;}else cout << "不是空表!" << endl;}//清空表LinkedList linkedListClear(LinkedList l) {LinkedList p=l,pri=l;p = p->next;while (p!=l) {pri = p;p = p->next;free(pri);}l->next = l;cout << "清空完成!" << endl;return l;}//遍历输出所有元素void linkedListTraverse(LinkedList l) {LinkedList p=l;p=p->next;if (p!=l) {cout << "遍历结果是:";while (p != l) {cout << p->data << " ";p = p->next;}cout << endl;}else {cout << "空表不能遍历!" << endl;}}//将位置i的元素修改为xLinkedList sqlListUpdate(LinkedList l,int i,dataType x) {LinkedList p=l;int j = 0;while(j!=i){++j;p = p->next; }p->data = x;cout << "修改成功!" << endl;return l;}void print() {cout <<"*********************************************"<<endl;cout <<"*1.链表的长度 2.取位置i的前驱 *"<< endl;cout <<"*3.取位置i的后驱 4.元素x的位置 *"<< endl;cout <<"*5.位置i处插入元素x 6.删除位置i的元素 *"<< endl;cout << "*7.取位置i的元素 8.表是否为空 *"<< endl;cout << "*9.清空表 10.遍历表 *"<< endl;cout << "*11.修改位置i的元素 12.退出 *"<< endl;cout << "*13.清空命令行 14创建循环链表 *"<< endl;cout << "*********************************************"<< endl;}void main() {int m=0,i = 0,n=0;LinkedList l = create();print();while (1) {cin >> i;switch (i){case 1:cout <<"链表的长度为:"<<linkedListLength(l) << endl; break;case 2:cin >> m;linkedListPrior(l, m); break;case 3:cin >> m; linkedListNext(l, m); break;case 4:cin >> m; linkedListLocate(l, m); break;case 5:cin >> m >> n;l=linkedListInsert(l, m, n); break;case 6:cin >> m;l=linkedListDelete(l, m); break;case 7:cin >> m; linkedListGet(l, m);break;case 8:linkedListEmpty(l);break;case 9:l = linkedListClear(l);break;case 10:linkedListTraverse(l);break;case 11:cin >> m; cin >> n;l = sqlListUpdate(l,m,n); break;case 12:exit(0);case 13:system("cls"); print();break;case 14:l = create(); break;default:cout << "数字超出范围,请重新输入。" << endl; break;}}}
0 0
- (5)循环链表
- VJ --2056,循环链表,当5个人循环时,要循环到4
- 循环链表实现循环队列
- 循环链表与循环队列
- 第二章(5).双向循环链表
- 数据结构与算法(Java描述)-5、循环链表仿真链表以及循环链表应用
- 循环链表&线性表(5.15)
- 约瑟夫环(循环链表)
- 面试题2(循环链表)
- 面试题(循环链表)
- Joseph问题(循环链表)
- 循环队列模版(链表实现)
- 多项式类(循环链表)
- 循环链表(约瑟夫环)
- 双向链表(非循环)
- 循环链表(java实现)
- 约瑟夫环问题(循环链表)
- 约瑟夫问题(循环链表)
- Java实现字符串倒序输出的常用方法小结
- laravel-5.3(2) 路由配置
- Atitit 修改密码的功能流程设计 attilax总结
- Ehcache是现在最流行的纯Java开源缓存框架
- 软件无线电 SDR LTE平台简介---OAI、srsLTE、OpenLTE与Amarisoft
- (5)循环链表
- CF - 711D 搜索环
- Shell脚本local变量内存泄露
- hdu3820 Golden Eggs(最小割)
- 数组指针和指针数组
- 生产者/消费者模式(阻塞队列) 一个经典的并发模型
- 【PAT甲级】1062. Talent and Virtue (25)
- libcurl windows编译,运行配置
- 源码安装yui compressor