使用2个或更多节点对链表进行排序(Sorting a linked list with 2 or more nodes)
我正在尝试在C中对链表进行排序,我的结构具有“时间”字段,我想按时间按升序排序。
但是我不能在2个或更多元素的情况下添加新节点,0或1代码可以工作,例如,当我尝试这个时:7,6,2,9(这些是每个事件的“时间”) ,我的代码排序2,6,7,但在'9'时,我的终端只是停下来回答。
好吧,提前谢谢。
#include <stdio.h> #include <stdlib.h> #include <string.h> // My struct typedef struct event_t { double time; char description[50]; int id_origin, id_dest; struct event_t *prox; } event_t; bool event_sort (event_t **list, double time, char description[], int id_origin, int id_dest) { event_t *newelement = (event_t*)malloc(sizeof(event_t)); event_t *aux = *list; if (newelement!=NULL) { newelement->time = time; strcpy (newelement->description, description); newelement->id_origin = id_origin; newelement->id_dest = id_dest; // Here I check if the list is empty if (*list==NULL) { *list = newelement; newelement->prox = NULL; } // Here I check if the list has one element else if (aux->prox == NULL) { if (aux->time <= time) { aux->prox = newelement; newelement->prox = NULL; } else { *list = newelement; newelement->prox = aux; } } // case if the list have two or more nodes else { if (aux->time >= time) { *list = newelement; newelement->prox = aux; } else { while ((aux->prox!=NULL)||(aux->prox->time<=time)) { aux = aux->prox; } newelement->prox = aux->prox; aux->prox = newelement; } } return true; } else { return false; } } int main (int argc, char *argv[]) { event_t *list = NULL, aux; int number, i; printf ("Enter the number of events: "); scanf ("%d", &number); printf ("\n"); for (i=0; i<number; i++) { printf ("Event %d\n", i+1); printf ("Enter the time: "); scanf ("%lf", &aux.time); printf ("Enter the description: "); scanf ("%s", aux.description); printf ("Enter the id origin: "); scanf ("%d", &aux.id_origin); printf ("Enter the id dest: "); scanf ("%d", &aux.id_dest); printf ("\n"); event_sort (&list, aux.time, aux.description, aux.id_origin, aux.id_dest); } return 0; }
I'm trying to sort a linked list in C, my struct has the field "time", and I want to sort in ascending order by time.
But I cant add new node at the end in case of 2 or more elements, 0 or 1 the code works, for example, when I try this: 7, 6, 2, 9 (these are the "times" of each event), my code sort 2,6,7, but when in '9' my terminal just stop to answering.
Well, thanks in advance.
#include <stdio.h> #include <stdlib.h> #include <string.h> // My struct typedef struct event_t { double time; char description[50]; int id_origin, id_dest; struct event_t *prox; } event_t; bool event_sort (event_t **list, double time, char description[], int id_origin, int id_dest) { event_t *newelement = (event_t*)malloc(sizeof(event_t)); event_t *aux = *list; if (newelement!=NULL) { newelement->time = time; strcpy (newelement->description, description); newelement->id_origin = id_origin; newelement->id_dest = id_dest; // Here I check if the list is empty if (*list==NULL) { *list = newelement; newelement->prox = NULL; } // Here I check if the list has one element else if (aux->prox == NULL) { if (aux->time <= time) { aux->prox = newelement; newelement->prox = NULL; } else { *list = newelement; newelement->prox = aux; } } // case if the list have two or more nodes else { if (aux->time >= time) { *list = newelement; newelement->prox = aux; } else { while ((aux->prox!=NULL)||(aux->prox->time<=time)) { aux = aux->prox; } newelement->prox = aux->prox; aux->prox = newelement; } } return true; } else { return false; } } int main (int argc, char *argv[]) { event_t *list = NULL, aux; int number, i; printf ("Enter the number of events: "); scanf ("%d", &number); printf ("\n"); for (i=0; i<number; i++) { printf ("Event %d\n", i+1); printf ("Enter the time: "); scanf ("%lf", &aux.time); printf ("Enter the description: "); scanf ("%s", aux.description); printf ("Enter the id origin: "); scanf ("%d", &aux.id_origin); printf ("Enter the id dest: "); scanf ("%d", &aux.id_dest); printf ("\n"); event_sort (&list, aux.time, aux.description, aux.id_origin, aux.id_dest); } return 0; }
原文:https://stackoverflow.com/questions/22121088
最满意答案
您应该使用Codescape MIPS SDK 。
You should use Codescape MIPS SDK.
相关问答
更多-
几个选项: 与Tizen人交谈,看他们是否有预编译的工具链与您的系统相匹配 直接在设备上构建代码 使用crosstool-ng构建一个新的交叉编译器,但使用旧的glibc版本 升级你的系统是因为glibc-2.13(以及glibc-2.17)非常古老,并且有大量已知的安全漏洞,包括远程攻击。 基于那些glibc版本开发和部署任何东西是一个可怕的可怕错误。 few options: talk to the Tizen people to see if they have pre-compiled toolch ...
-
特拉维斯的MIPS交叉编译(MIPS Cross Compilation on Travis)[2022-12-07]
看来我不能使用容器,因为这个要求,我放弃了,只是简单地使用: sudo: true dist: trusty addons: apt: packages: - u-boot-tools before_install: - sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu/ xenial main universe" - sudo apt-get -qq update - sudo apt-ge ... -
经过大量的图书馆和工具链的讨论,我成功地完成了这项工作。 您需要首先设置交叉编译器来为ARM926J CPU编译ARMv5可执行文件。 为什么选择ARMv5和ARM926J? 因为我正在使用SAM9N12,它使用ARM926J代码(J代表运行本机JVM的Jazelle),该核心是ARMv5架构。 您可以从基于ARM的基于启动板的GCC仓库获取交叉编译器,也可以使用Crosstools-NG自行创建交叉编译器。 建议使用Crosstools-NG,因为您可以使用自己的标准C库(与uClib或musl相比,GN ...
-
显然,自从Boost 1.61对Boost.Build进行某些更改时,如在此故障单中所述,在Windows上进行交叉编译时会破坏Cygwin路径分辨率。 一种可能的解决方案( 从票据中解决 )是将cygwin.jam文件打包到tools \ build \ src \文件夹中。 只需从第63行开始 if $(head) = / 至 if $(head) = "/" || [ MATCH "^([a-zA-Z]:/)$" : $(head) ] 现在b2运行没有问题。 :-) Apparently s ...
-
- 嗨,phil999, Cortex-A系列程序员指南版本:2.0 在http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0013a/index.html 从文件中可以知道 -mfpu = vfp指定目标具有VFP硬件。 (与指定选项-mfpu = neon一样。) 因此,根据您的C_FLAGS,选项“-mfpu = vfp”等同于选项“-mfpu = neon”,Linaro的差异可能在选项“-mfloat-abi = softf ...
-
查看strerror_r的手册页 ,我看到: int strerror_r(int errnum, char *buf, size_t buflen); /* XSI-compliant */ char *strerror_r(int errnum, char *buf, size_t buflen); /* GNU-specific */ 注意返回类型的差异。 查看您的错误消息: error: invalid conversion from 'int' t ...
-
无法在cygwin下为raspberry pi交叉工具链构建eglibc(Unable to build eglibc under cygwin for raspberry pi cross-toolchain)[2022-03-06]
对我感到羞耻:我没有正确读取第三个脚本的输出并错过了错误消息: /bin/sh: perl: Command not found. 这可能是因为脚本的输出是: === build script: OK === 我注意到脚本中完全没有错误处理。 安装perl后,我可以继续构建交叉编译环境。 Shame on me: I didn't read properly the output of the third script and missed the error message: /bin/sh: per ... -
什么是编译器工具链?(What is a compiler toolchain?)[2023-01-25]
工具链是用来创建编程产品(可运行/可执行程序等)的一组工具(工具链)。 通常工具链在嵌入式世界中用于交叉编译,这意味着在主机上创建一个程序,最终将运行在另一种目标上 - 因此需要使用特定的编译器,链接器,调试器等来创建它。 为了从头开始创建目标的编译器,您需要使用特定的工具链进行交叉编译 - 即编译器工具链。 因此,通过使用编译器工具链,您将能够为目标系统创建一个编译器。 A tool chain is a set of tools (chain of tools) that are used to cre ... -
为了得到一个完全静态链接的Mono,你必须首先将它的所有依赖关系(和它们的依赖关系递归)作为静态库。 只有这样你才能建立一个真正的静态单声道。 Well, it's been a while since I tried this, but I wanted to report what I learned. For one, I found out that the MIPS processor is 64-bit and not 32-bit like I was initially told for t ...
-
带有MS-Windows工具链的预构建MIPS交叉编译器(Prebuilt MIPS cross compiler with toolchain for MS-Windows)[2023-04-15]
您应该使用Codescape MIPS SDK 。 You should use Codescape MIPS SDK.