循环链表 稍复杂点。

肯能会有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 链表操作(单向循环链表、查找循环节点)的更多相关文章

  1. 复习下C 链表操作(双向循环链表,查找循环节点)

    双向循环链表  和 单向循环链表 查找循环节点 思路都是一样. 快慢指针查找法. 理论可参考 c 链表之 快慢指针 查找循环节点 typedef struct Student_Double { ]; ...

  2. c 链表之 快慢指针 查找循环节点

    参考:http://blog.csdn.net/wenqian1991/article/details/17452715 上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上 ...

  3. c 链表之 快慢指针 查找循环节点(转)

    上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上面文章的分析.仔细推倒一下 , 一般设置 快指针 速度是 慢指针的2倍.及 快指针每次遍历两个指针, 慢指针每次遍历1个指 ...

  4. 复习下C 链表操作(单向链表)

    Object-C 作为C 的包装语言(运行时.消息机制).如果不熟悉C 的话实在玩得太肤浅. 随便深入oc 内部都会接触到C. runtime .GCD.Block.消息机制... 所有强大的功能无不 ...

  5. 复习下C 链表操作(双向链表)

    双向链表 创建.删除.反转.插入 //struct #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  6. linux下的文本操作之 文本查找——grep

    摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...

  7. JAVA 链表操作:循环链表

    主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...

  8. linked-list-cycle-ii——链表,找出开始循环节点

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  9. linux 内核的链表操作(好文不得不转)

    以下全部来自于http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 无任何个人意见. 本文详细分析了 2.6.x 内 ...

随机推荐

  1. Flash:彻底理解crossdomain.xml、跨swf调用。

    安全域.crossdomain.xml,到处都有各种各种零碎的基础解释,所以这里不再复述这些概念. 本文目的是整理一下各种跨域加载的情况.什么时候会加载crossdomain,什么时候不加载.   1 ...

  2. 使用Oracle Data Integrator Studio创建资料档案库

    一.Creating the Database Schema /*第1步:创建临时表空间 */ create temporary tablespace user_temp tempfile 'C:\a ...

  3. 【shell】数据文件分割

    有时候我们必须把数据文件分割为更小的文件,这样方便我们邮件发送或者查看文件内容.split命令则可以用来分割文件. 一.根据大小来分割文件 1.一般分割 例如:现在有文件tmp.log,大小为:368 ...

  4. POJ1013 称硬币

    题目链接:http://poj.org/problem?id=1013 题目大意 有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三 ...

  5. 拦截导弹问题(NOIP1999)

    某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统,但是这种拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度, 但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹来袭,由于该 ...

  6. nfs远程挂载问题记录

    问题描述: mount: wrong fs type, bad option, bad superblock on x.x.x.x:/xxxx_domain/update,missing codepa ...

  7. Arduino——My-Clock项目 发布时间:2018-12-31

    技术:Arduino.光敏传感器.DHT11.DS1302.OLED显示屏   概述 项目My-Clock是一个环境监测时钟,接入光敏传感器和温湿度传感器监测环境信息,加入DS1302模块用于获取时间 ...

  8. Javascript + Dom知识点总结

    Javascript + Dom知识点总结 1.用Javascript声明数组和字典的方式 // 数组声明 var arr = new Array(); arr["0"] = &q ...

  9. lua闭包

    function MakeCounter() return function() t = t + return t end end local func = MakeCounter() , do pr ...

  10. Zabbix检测Mysql数据库的主从同步

    在高并发网站架构中,MySQL数据库主从同步是不可或缺的,不过经常会发生由于网络原因或者操作错误,MySQL主从经常会出现不同步的情况,那么如何监控MySQL主从同步,也变成检测网站正常运行的重要环节 ...