创建一个循环链表,并将这个循环链表拆分成为两个循环链表的示例程序,将以下代码保存到一个源文件中:split_circular_linked_list.c, 如下所示 –

#include #include struct node { int data; struct node *next; }; struct node *even = NULL; struct node *odd = NULL; struct node *list = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); struct node *current; link->data = data; link->next = NULL; if (list == NULL) { list = link; list->next = link; return; } current = list; while (current->next != list) current = current->next; // Insert link at the end of the list current->next = link; link->next = list; } void display(struct node *head) { struct node *ptr = head; printf("[head] =>"); //start from the beginning while (ptr->next != head) { printf(" %d =>", ptr->data); ptr = ptr->next; } printf(" %d =>", ptr->data); printf(" [head]n"); } void split_list() { int count = 0; // Allocate memory for new node; struct node *list1; struct node *link; struct node *current; list1 = list; while (list1->next != list) { struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = list1->data; link->next = NULL; if (list1->data % 2 == 0) { if (even == NULL) { even = link; even->next = link; list1 = list1->next; continue; } else { current = even; while (current->next != even) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = even; } list1 = list1->next; } else { if (odd == NULL) { odd = link; odd->next = link; list1 = list1->next; continue; } else { current = odd; while (current->next != odd) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = odd; } list1 = list1->next; } } // Lets handle the last node link = (struct node*) malloc(sizeof(struct node)); link->data = list1->data; link->next = NULL; if (list1->data % 2 == 0) { current = even; while (current->next != even) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = even; } else { current = odd; while (current->next != odd) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = odd; } } int main() { int i; for (i = 1; i <= 10; i++) insert(i); printf("Complete list: n"); display(list); split_list(); printf("nOdd : "); display(odd); printf("Even : "); display(even); return 0; }

执行上面程序,得到以下结果 –

Complete list: [head] => 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8 => 9 => 10 => [head] Odd : [head] => 1 => 3 => 5 => 7 => 9 => [head] Even : [head] => 2 => 4 => 6 => 8 => 10 => [head]

¥ 我要打赏 纠错/补充 收藏

c语言循环拆分成和,C语言拆分循环链表程序相关推荐

  1. c语言循环设计思想结构,C语言程序设计_05循环结构程序设计讲述.pptx

    C语言程序设计_05循环结构程序设计讲述 第5章 循环结构程序设计5.1 while循环结构5.2 do-while循环结构5.3 for循环结构5.4 与循环有关的控制语句5.5 3种循环语句的比较 ...

  2. C语言循环结构素数判断,C语言实验之判断素数(循环结构java)方法讲解

    C语言实验之判断素数(循环结构java)方法讲解 Problem Description 从键盘上输入任意一个正整数,然后判断该数是否为素数. 如果是素数则输出"This is a prim ...

  3. c语言循环10次代码,C语言教学(七-上)for循环

    原标题:C语言教学(七-上)for循环 今天带大家了解C语言for循环的用法,我们知道计算机对每行代码的处理只有一次,如果我们需要重复进行某一步骤的时候,那不是需要重复敲这一段代码吗,要是需要重复一千 ...

  4. c语言循环嵌套说课,C语言FOR循环说课稿.doc

    C语言FOR循环说课稿.doc C 语言 FOR 循环说课稿 各位评委老师上午好,我今天说课的内容是"FOR 循环" (板书) ,下面我将从教材的 地位及作用.学生学情.教学目标. ...

  5. c语言循环队列的销毁,C语言循环队列

    参考自维基百科: 含测试代码,详细注释: #include #include #include /*循环队列 C语言实现 *2011-04-28 *liliming123@sina.com */ #i ...

  6. c语言循环中按键跳出,C语言跳出循环

    C语言跳出循环 C语言在程序员中备受青睐,成为最近25年使用最为广泛的编程语言.那么大家知道C语言跳出循环是怎么回事呢?下面一起来看看! 使用while或for循环时,如果想提前结束循环(在不满足结束 ...

  7. c语言循环语句相关摘要,C语言中循环语句的应用研究

    为了帮助初学者尽快掌握C语言的循环语句,通过实例较详细的分析了循环语句的结构.功能和应用方法,以便读者能在实际应用中能够合理地选择循环语句,编出满足需要的程序来. . 26 0 价值工程 C语言中循环 ...

  8. c语言循环嵌套寻找最大数,C语言的大海里:嵌套的循环语句的重要程序竟然是这几个?...

    C语言中最经典的也就是嵌套循环语句了,大家也都知道循环语句有for循环.while循环和do-while循环,其中以for循环最为出名,这次的嵌套的循环语句的经典程序也是围绕着for循环展开的. 在了 ...

  9. c语言循环的嵌套案例,C语言嵌套循环

    C编程语言允许使用一个循环内嵌套的另一个循环.下面的内容展示几个例子来说明这个概念. 语法 在C语言中嵌套for语句循环的语法如下: for(init;condition;increment){for ...

最新文章

  1. 中文NER任务简析与深度算法模型总结和实战展示 转 作者原创的不错,很有水平,需要研读
  2. Android数据存储
  3. ThinkPHP 数据库操作之数据表模型和基础模型 ( Model )
  4. C语言怎么输出百分号%
  5. matlab中腐蚀图像的编写,Matlab实现二值图像的腐蚀算法源代码
  6. (转) RabbitMQ学习之spring整合发送同步消息(注解实现)
  7. 会涨价么?苹果iPad和MacBook拟采用三星OLED屏幕
  8. python期末复习卷_【期末复习卷A】六年级科学上册期末复习试题
  9. Rapid7 部分源代码遭泄露,成 Codecov 供应链攻击第四个受害者
  10. clickhouse创建数据库以及表
  11. Java字符串操作及处理
  12. 机器学习、⼈⼯智能、深度学习是什么关系?
  13. java的return用法
  14. 电驴创始人Jed McCaleb的传奇人生
  15. 电视盒子刷鸿蒙系统,当贝市场亲测有效三款获取电视和盒子root权限的工具应用...
  16. Kettle Spoon入门教程
  17. 基于51单片机---利用霍尔元件无接触式测量直流电机速度
  18. 微信自动跳转浏览器打开APP(APK)下载链接
  19. Windows2003 IIS6 + PHP-5.3.6-nts-Win32-VC9-x86.zip
  20. F 魏迟燕的自走棋(思维+贪心+并查集维护联通块/左部点配对边<=2的匈牙利)

热门文章

  1. 基于 vue 的验证码组件
  2. 2018091-2 博客作业
  3. mysql联合索引与Where子句优化浅析
  4. javascript的浏览器Bom详解,window、location、history对象
  5. [置顶] Android的IPC访问控制设计与实现
  6. 分割范围Codeforces Round #181 (Div. 2)
  7. SQL Server与Oracle、DB2三种数据库比较
  8. Atlas 不仅仅是异步
  9. 600分理科选计算机专业,天津600分左右,计算机或电子信息专业,怎么选院校?...
  10. 计算机二级高级应用考题,2016计算机二级MSOFFICE高级应用考试真题