首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用两个或多个节点对链接列表进行排序

使用两个或多个节点对链接列表进行排序
EN

Stack Overflow用户
提问于 2014-03-01 14:14:29
回答 2查看 52关注 0票数 1

我试图在C中对链接列表进行排序,我的结构有" time“字段,我想按时间按升序排序。

但是我不能在两个或更多元素的末尾添加新节点,例如,当我尝试这样做时:7、6、2、9(这是每个事件的“时间”),我的代码排序为2, 6,7,但在'9‘时,我的终端就会停下来回答。

好吧,提前谢谢。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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;

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-01 14:43:22

问题的一部分是

while ((aux->prox!=NULL)||(aux->prox->time<=time))

我想你是说

while ((aux->prox!=NULL)&&(aux->prox->time<=time))

我没有寻找其他的问题。

再见,

弗朗西斯

票数 0
EN

Stack Overflow用户

发布于 2014-03-01 14:48:52

我在这里看到一个错误:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
else if (aux->prox == NULL)
{
    if (aux->time <= time)
    {
        aux->prox = newelement;
        newelement->prox = NULL;
    }
    else
    {
        *list = newelement;
        newelement->prox = aux;
    }
}

它应该是

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    else if (aux->prox == NULL)
    {
        if (aux->time <= time)
        {
            aux->prox = newelement;
            newelement->prox = NULL;
        }
        else
        {
            newelement->prox = aux;
            *list = newelement;
        }
    }

否则,在复制之前,您将覆盖*list所指向的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22121088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文