复习下C 链表操作(双向循环链表,查找循环节点)
双向循环链表 和 单向循环链表 查找循环节点 思路都是一样。 快慢指针查找法。 理论可参考
typedef struct Student_Double
{
char name[];
int point;
struct Student_Double *preStu;
struct Student_Double *nextStu;
} StudentDouble;
StudentDouble * CreateDoubleCircleLink_Table(){ int i = ;
StudentDouble *head = NULL;
head=(StudentDouble *)malloc(sizeof(StudentDouble));
head->name[]='\0';
head->point = ;
head->nextStu = NULL;
head->preStu = NULL; //循环节点
StudentDouble *cirleStu = NULL;
StudentDouble *temp = NULL;
StudentDouble *currentNode = head;
while (i<=) {
temp = (StudentDouble *)malloc(sizeof(StudentDouble));
strncpy(temp->name,"Node",sizeof(temp->name));
temp->point = i;
temp->nextStu = NULL;
temp->preStu = currentNode; currentNode->nextStu = temp;
currentNode = temp; if (i==) {
cirleStu = currentNode;
}
i++;
}
//最后 合并循环节点
currentNode->nextStu=cirleStu;
return head;
}
//已知循环节点情况查询循环 链表,验证是否可用
void SelectDoubleLinkTable(StudentDouble *student){
StudentDouble *next = student->nextStu;
int i = ;
StudentDouble *circleStu = NULL;
while (next) {
if (circleStu!=NULL&&next->point == circleStu->point) {
printf("循环节点%d,结束循环\n",next->point);
break;
}
if (i==) {
circleStu = next;
}
printf("index %d; studentName is %s; point is %d\n",i,next->name,next->point);
i++;
next = next->nextStu;
} }
//未知情况查询循环节点
StudentDouble * SelectCircleNodeInDoubleLinkTable(StudentDouble *head){
//快慢指针查询
StudentDouble *fast = head;
StudentDouble *slow = head; while (fast) {
fast = fast->nextStu->nextStu;
slow = slow->nextStu; if (fast==NULL) {//不是循环链表推出
break;
}
if (fast==slow) {//快慢指针相遇
break;
}
}
if (fast == NULL) {
printf("该链表 不是循环链表\n");
return NULL;
} //查找循环节点
fast = head;
while (fast!=slow) {
fast=fast->nextStu;
slow=slow->nextStu;
}
printf("=====找到循环链表循环节点为%d\n",fast->point);
return fast;
}
int main(void){
char sf[];
//创建双向循环链表
StudentDouble *head = NULL; printf("创建双向循环链表Y|N\n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
head = CreateDoubleCircleLink_Table();
}
printf("已知情况查询循环链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
SelectDoubleLinkTable(head);
}
printf("未知情况查询循环链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
SelectCircleNodeInDoubleLinkTable(head);
}
return ;
}
复习下C 链表操作(双向循环链表,查找循环节点)的更多相关文章
- 复习下C 链表操作(单向循环链表、查找循环节点)
循环链表 稍复杂点. 肯能会有0 或 6 字型的单向循环链表. 接下来创建 单向循环链表 并 查找单向循环链表中的循环节点. 这里已6字型单向循环链表为例. //创建 循环链表 Student * ...
- 复习下C 链表操作(双向链表)
双向链表 创建.删除.反转.插入 //struct #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- c 链表之 快慢指针 查找循环节点
参考:http://blog.csdn.net/wenqian1991/article/details/17452715 上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上 ...
- c 链表之 快慢指针 查找循环节点(转)
上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上面文章的分析.仔细推倒一下 , 一般设置 快指针 速度是 慢指针的2倍.及 快指针每次遍历两个指针, 慢指针每次遍历1个指 ...
- 复习下C 链表操作(单向链表)
Object-C 作为C 的包装语言(运行时.消息机制).如果不熟悉C 的话实在玩得太肤浅. 随便深入oc 内部都会接触到C. runtime .GCD.Block.消息机制... 所有强大的功能无不 ...
- linux下的文本操作之 文本查找——grep
摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...
- JAVA 链表操作:循环链表
主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...
- 后台开发 3个题目 array_chunk, 100块钱找零钱(动态规划 dynamic programming), 双向循环链表 llist 删除节点
1. array_chunk 实现 http://php.net/manual/en/function.array-chunk.php <?php function my_array_chunk ...
- linked-list-cycle-ii——链表,找出开始循环节点
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
随机推荐
- Spring日记_01 之 Eclipse下的Tomcat服务器配置 以及 Springmvc和Servlet的使用
安装Tomcat – window – preferences – Server 右键Tomcat v ...
- macbook安装并破解Clion2018(Pycharm也一样)
一.下载 Clion 并安装 二.修改hosts文件,使用 vim /private/etc/hosts 命令将 0.0.0.0 account.jetbrains.com 粘贴进去(提示: i键是插 ...
- 解决sublime text 安装扩展提示There are no packages available for installation问题
前段时间想给sublime编辑器装个插件,发现总是报这个错误 google后发现是“众所周知”的原因,设置里面的https://packagecontrol.io/channel_v3.json文件被 ...
- Linux 输入子系统驱动程序范例
<按键驱动程序> #include <stdio.h> #include <fcntl.h> #include <linux/input.h> #inc ...
- checkedListBox的使用
. 添加项 checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checkedL ...
- React系列文章:Webpack模块组织关系
现代前端开发离不开打包工具,以Webpack为代表的打包工具已经成为日常开发必备之利器,拿React技术栈为例,我们ES6形式的源代码,需要经过Webpack和Babel处理,才能生成发布版文件,在浏 ...
- BZOJ4313 : 三维积木
不妨设$R$是唯一可以看到的颜色,考虑一维序列的情况. 设$f[i][j][k][x][y]$表示考虑了前$i$个位置,第$i$个位置的高度是$j$,最高高度是$k$,已经用了$x$个$R$,$y$个 ...
- 安装 Keepalived
安装环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# cat /proc/v ...
- python 元组和字典中元素作为函数调用参数传递
模式1. def test1(*args): test3(*args) def test2(**kargs): test3(**kargs) def test3(a, b): print(a,b) ...
- Java API概述
collection of APIs(Application Programming Interface) java.lang — automatically imported into Java p ...