复习下C 链表操作(单向循环链表、查找循环节点)
循环链表 稍复杂点。
肯能会有0 或 6 字型的单向循环链表。 接下来创建 单向循环链表 并 查找单向循环链表中的循环节点。
这里已6字型单向循环链表为例。
//创建 循环链表
Student * CreateCircleLink_Table(void){
int i = ;
Student *head = NULL;
head = (Student *)malloc(sizeof(Student));
head->name[]='\0';
head->point = ;
head->stu = NULL;
//循环节点
Student *circleStu = NULL;
Student *temp = NULL;
Student *currentNode = head;
while (i<=) { temp = (Student *)malloc(sizeof(Student));
strncpy(temp->name,"Node",sizeof(temp->name));
temp->point = i;
temp->stu = NULL; currentNode->stu = temp;
currentNode = temp; //循环节点
if (i==) {
circleStu = currentNode;
} i++;
} //最后 合并循环节点
currentNode->stu = circleStu; return head;
} //已知循环节点 查询 主要为了验证循环链表是否可用
void SelectCircleLinkTable(Student *student){
Student *next = student->stu;
int i = ;
Student *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->stu;
}
} //查找循环链表中循环节点
Student * SelectCircleNodeInLinkTable(Student *student) {
Student *fast = student;
Student *slow = student; Student *circleStu = NULL;
while (fast->stu) {
fast = fast->stu->stu;//快指针节点 为慢指针节点的2倍
slow = slow->stu; if (fast==NULL) { //不存在循环节点
return NULL;
}
if (fast == slow) {//快慢指针相遇。找到循环节点
circleStu = fast;
break;
} } if (fast==NULL) { //不存在循环节点
return NULL;
}
printf("相遇节点为==%d\n",circleStu->point);
fast=student;
while (fast!=slow) {
slow=slow->stu;
fast=fast->stu;
}
return fast;
} int main(void){
char sf[]; // /**
// * 创建单向链表
// */
// int num;
// printf ("请输入学生人数\n");
// scanf("%d",&num);
// Student *link_stu = CreateLink_Table(num);
//
//
// /**
// * 单向链表插入节点
// */
// printf ("请插入节点内容 在 已存在节点名字的后面,如 已存在节点名字|待插入名字|待插入分数 \n");
// scanf("%s",sf);
//
// link_stu = insertStudentLinkTable(link_stu,sf);
//
// /**
// * 反转单向链表
// */
// printf("反转链表Y|N \n");
// scanf("%s",sf);
// if (strcmp(sf,"Y")==0) {
// Student *newLt= ReversionStudentLinkTable(link_stu);
// //查询
// selectStudent(newLt);
// } /**
* 创建循环链表
*/
Student *student = NULL;
printf("开始创建循环链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
student = CreateCircleLink_Table();
} printf("已知情况查询循环链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
SelectCircleLinkTable(student);
} printf("未知情况查询循环链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
Student *circleStu = SelectCircleNodeInLinkTable(student);
printf("=====循环节点==%d\n",circleStu->point);
} return ;
}
参考这便:http://blog.csdn.net/wenqian1991/article/details/17452715
复习下C 链表操作(单向循环链表、查找循环节点)的更多相关文章
- 复习下C 链表操作(双向循环链表,查找循环节点)
双向循环链表 和 单向循环链表 查找循环节点 思路都是一样. 快慢指针查找法. 理论可参考 c 链表之 快慢指针 查找循环节点 typedef struct Student_Double { ]; ...
- c 链表之 快慢指针 查找循环节点
参考:http://blog.csdn.net/wenqian1991/article/details/17452715 上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上 ...
- c 链表之 快慢指针 查找循环节点(转)
上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上面文章的分析.仔细推倒一下 , 一般设置 快指针 速度是 慢指针的2倍.及 快指针每次遍历两个指针, 慢指针每次遍历1个指 ...
- 复习下C 链表操作(单向链表)
Object-C 作为C 的包装语言(运行时.消息机制).如果不熟悉C 的话实在玩得太肤浅. 随便深入oc 内部都会接触到C. runtime .GCD.Block.消息机制... 所有强大的功能无不 ...
- 复习下C 链表操作(双向链表)
双向链表 创建.删除.反转.插入 //struct #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- linux下的文本操作之 文本查找——grep
摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...
- JAVA 链表操作:循环链表
主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...
- linked-list-cycle-ii——链表,找出开始循环节点
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- linux 内核的链表操作(好文不得不转)
以下全部来自于http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 无任何个人意见. 本文详细分析了 2.6.x 内 ...
随机推荐
- 【转】Flash:同志们,这些知识点你们知道多少?(一些必备的Flash开发知识点)
1.理解flash的显示列表 2.理解事件冒泡,理解鼠标事件等 3.理解flash的性能瓶颈和大多数影响性能的地方4.理解帧跑道模型,知道timer和ent ...
- 使用VTK与Python实现机械臂三维模型可视化
三维可视化系统的建立依赖于三维图形平台, 如 OpenGL.VTK.OGRE.OSG等, 传统的方法多采用OpenGL进行底层编程,即对其特有的函数进行定量操作, 需要开发人员熟悉相关函数, 从而造成 ...
- PHP视频学习一 mysql
设置mysql.exe文件目录到环境变量Path中去,可能在cmd下面使用mysql,wind7试了一下好像要重启以后才生效 什么是数据库 数据库就是存储数据的地方,是保存在计算机(硬盘/内存)中的数 ...
- LeetCode——Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Axure快速原型教程01--原型说明下载和安装
Axure是一个快速画原型的工具 什么是原型? 估计进来的朋友应该都清楚,原型通俗来讲,不仅仅是在软件开发中使用,在很多行业中也需要用的,比如服装设计,建筑 ...
- Android开发环境——模拟器AVD相关内容汇总
Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT相关 ...
- libev ev_init分析
/* these may evaluate ev multiple times, and the other arguments at most once */ /* either use ev_in ...
- Asynchronous and non-Blocking I/O 翻译[收藏好文]
http://www.tornadoweb.org/en/stable/guide/async.html Real-time web features require a long-lived mos ...
- 基本的RAID介绍
RAID是一个我们经常能见到的名词.但却因为很少能在实际环境中体验,所以很难对其原理 能有很清楚的认识和掌握.本文将对RAID技术进行介绍和总结,以期能尽量阐明其概念. RAID全称为独立磁盘冗余阵列 ...
- Linux下mysql的远程连接(转)
转载:http://www.cnblogs.com/fnlingnzb-learner/p/5830661.html 如果Mysql是按上篇的方法进行安装和设置的话,那进行远程连接就会稍微简单一点.我 ...