c语言循环拆分成和,C语言拆分循环链表程序

创建一个循环链表,并将这个循环链表拆分成为两个循环链表的示例程序,将以下代码保存到一个源文件中: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]

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382940.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pic单片机c语言读eeprom,PIC16F877单片机内部EEPROM读写实例

;PIC16F877单片机内部EEPROM读写实例****************************************************************************************; This is a program to test the function of reading&writting for EEPROM.; YouCANOBServe the value of register(30H--?) buy changin…

C语言运行gis空间叠加分析,GIS空间叠加分析与缓冲区分析.doc

《地理信息系统》报告专 业 资源环境与城乡规划管理 姓 名 成 绩班 级 学 号 日 期 2014/6/20目录TOC \o "1-2" \h \u 14469 一、题目 23290 二、设计目的27200 三、设计背景2四、设计内容27200 四、步骤与过程27200 五、专题地图 37521 四、总结分析 9题目佛山市顺德…

组件文件已损坏或android内部模块,android - Android Q更新后,模块化系统组件在托管配置文件中不可用 - 堆栈内存溢出...

在从工作配置文件配置的设备设置应用中将操作系统从Android 9升级到10后&#xff0c;请停止运行。java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.applications.InstalledAppDetailsTop}: java.lang.NullPoin…

android文本复制自定义剪切板,android 剪切板-文本复制、粘贴

1. 粘贴&#xff0d;文本保存到剪切板中ClipboardManager clipboardManager (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);//创建ClipData对象ClipData clipData ClipData.newPlainText("orderNo", txt);//添加ClipData对象到剪切板中…

android保持服务不休眠,Android开发保持屏幕常亮和CPU不休眠唤醒状态

安卓手机 APP 开发&#xff0c;有的时候需要屏幕长时间亮着&#xff0c;也就是不锁屏&#xff0c;这时CPU会一直处于不休眠唤醒状态。下面介绍两种方法。方法一&#xff1a;通过 PowerManager 实现。此种方法会在软件安装时用户可以看到屏幕选项。首先&#xff0c;在 AndroidMa…

android 磁场传感器 罗盘,Android开发获取重力加速度和磁场强度的方法

本文实例讲述了Android开发获取重力加速度和磁场强度的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;Android获取重力加速度和磁场强度主要依靠&#xff1a;Sensor.getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)输入数据&am…

android spinner位置,在Android中的Spinner中没有选择位置0

我创建了一个有三个项目的微调器日常每周每月一次我在我的java文件中执行了以下操作&#xff1a;navSpinner new ArrayList();navSpinner.add(new SpinnerNavItem(getResources().getString(R.string.dailyview)));navSpinner.add(new SpinnerNavItem(getResources().getStrin…

android tombstone发生过程,Android Tombstone解决步骤

这周和同事一起解了个tombstone的bug, 记录下分析的过程&#xff0c;免得以后又忘记。。。1>log的分析pid: 122, tid: 14745, name: Binder_2 >>> /system/bin/mediaserver <<<signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000058eax 00000…

优酷android手机客户端for,优酷手机客户端

优酷手机客户端官方最新版是官方出品的最新版播放软件&#xff0c;该软件上面汇聚了其他视频app上没有的精彩节目&#xff0c;全国各大精彩剧集与电影免费首播&#xff0c;更有独特高清蓝光画质播放&#xff0c;想要体验的朋友可以来欣赏一下!软件特点1、高清流畅播放2、国内外…

html在线编辑器 asp.net,ASP.NET网站使用Kindeditor富文本编辑器配置步骤

1. 下载编辑器下载 KindEditor 最新版本&#xff0c;下载页面: http://www.kindsoft.net/down.php2. 部署编辑器解压 kindeditor-x.x.x.zip 文件&#xff0c;将editor文件夹复制到web目录下3、在网页中加入(ValidateRequest"false")4、引入脚本文件(XXX部分需要修改)…

html表格中绑定显示xml文档内容的简单实例,JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)...

本文实例讲述了JS读取XML文件数据并以table形式显示数据的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;先看xml文件&#xff1a;张秋丽女 18李文才男 31李斯文男 22马英女 25孙红雷男 32欧阳俊雄男 28江琳女 23小小女 22aspx页面代码&#xff1a;function load…

html中通过定位 实现下拉,JS+CSS相对定位实现的下拉菜单

本文实例讲述了JSCSS相对定位实现的下拉菜单。分享给大家供大家参考。具体如下&#xff1a;这里使用的是相对定位&#xff0c;不过效果还可以&#xff0c;用时候再修整一下&#xff0c;这个只是实现了大概功能&#xff0c;还有许多细节没有修饰。运行效果截图如下&#xff1a;在…

html页面包含头文件,Web前端技术:HTML部分---Head标签中包含的头文件标签,body标签包含的内部标签...

1、Head标签中包含的 头文件标签的作用&#xff1a;(1)title标签&#xff1a;定义网页的标题。(2)meta标签&#xff1a;一般用于定义页面的特殊信息&#xff0c;例如页面的关键字、页面描述等(3)link标签&#xff1a;用于引入外部样式文件(CSS 文件)。(4)style标签&#xff1a;…

爬虫图片href是html图片,xpath爬虫实例,爬取图片网站百度盘地址和提取码

某套图网站&#xff0c;套图以封面形式展现在页面&#xff0c;需要依次点击套图&#xff0c;点击广告盘链接&#xff0c;最后到达百度网盘展示页面。这一过程通过爬虫来实现&#xff0c;收集百度网盘地址和提取码&#xff0c;采用xpath爬虫技术1、首先分析图片列表页&#xff0…

HTML如何做个播放器图表,Web绘图神器之ECharts-ts文件播放器

前言最近在做一个项目需要用到大量的图形报表来展示数据。就去对比了一些前端图形报表框架&#xff0c;有Highcharts、Echarts、Three.js。发现Three.js比较笨重&#xff0c;不太适合数据展示&#xff0c;做前端动画还是比较好。而highcharts、echarts比较轻量级拿来就用比较方…

微型计算机主存可以分为,计算机基础试题 (含答案)

计算机基础试题 (含答案)一、填空题(每空1分&#xff0c;共30分)1、计算计的软件系统通常分成______软件和______软件。2、字长是计算机______次能处理的______进制位数。3、1KB______B;1MB______KB。4、计算机中&#xff0c;中央处理器CPU由______和______两部分组成。5、CPU按…

90年代微型计算机,版本控制如何在80年代和90年代的当今微型计算机上工作?

您必须在当时的通用基础结构中看到这一点。在80年代初期&#xff0c;IBM发布了“个人计算机”&#xff0c;您可以从字面上理解它。开发PC应用程序的最常见方法是一个人创建某些东西并试图出售它。因此&#xff0c;每个发行版本一张软盘可能很常见。您可以购买一些漂亮的彩色标签…

全国英语计算机9月统考2019,2019年9月网络教育统考《计算机应用基础》模拟题6...

本文为大家提供2019年9月网络教育统考《计算机应用基础》模拟题6&#xff0c;有需要的考生请自取。网络教育本科全国统考《计算机应用基础》模拟题6一、单选题1、第一台电子数字计算机诞生于 ______。A &#xff1a; 麻省理工学院B &#xff1a; 哈佛大学C &#xff1a; 宾夕法…

计算机网络原码反码补码,计算机的原码和反码及补码到底是什么

数据在计算机里面都是以0和1存储和运算的&#xff0c;这是冯诺依曼体系的基础。比如一个数在计算机中若有正负之分&#xff0c;则用一个数的最高位(符号位)用来表示它的正负&#xff0c;其中0表示正数&#xff0c;1表示负数。原码就是整数绝对值的二进制形式&#xff0c;为了解…

计算机组装活动口号,广教育、多技能、求发展喜迎计算机系第五届电脑文化节...

在深入学习实践科学发展观&#xff0c;改革教育教学模式&#xff0c;迎接高职院校人才培养评估工作的今天&#xff0c;为贯彻以人为本理念&#xff0c;全方位服务学生&#xff0c;培养学生综合技能&#xff0c;本学期计算机系举办第五届电脑文化节&#xff0c;举办电脑文化节是…