我正在努力解决这个问题:我想将一个字符串插入到已排序的字符串链接列表中,但由于某种原因,它不起作用. 以下是代码:

void insert(node** head, const char* word){

  node* newElem = malloc(sizeof(node));
  newElem->data = malloc(sizeof (char)*(wordLenght+1));
  strcpy(newElem->data, word);

  if (*head == NULL || strcmp(newElem->data, (*head)->data) < 0){
      newElem->next = *head;
      *head = newElem;
      return;
  }

  nodo* cursor = *head;
  while (cursor->next != NULL && strcmp(newElem->data, cursor->data) < 0){
      cursor = cursor->next;
  }

  newElem->next = cursor->next;
  cursor->next = newElem;
}

我试过用这套琴弦

7DJL,-kiF, 8F4r, 7D7d, -D7w, -b7f

但这并没有奏效. 输出应为:

-D7w, -b7f, -kiF, 7D7d, 7DJL, 8F4r

谢谢你的帮助!

推荐答案

我不知道wordLenght是什么.但在任何情况下,在函数中使用此名称都没有意义,只会使函数变得不清楚,因为该名称没有在函数中定义.

不需要将函数分成两部分.这使得函数容易出错.

此外,While语句的条件

while (cursor->next != NULL && strcmp(newElem->data, cursor->data) < 0){

是不正确的.

如果此表达式

strcmp(newElem->data, cursor->data) < 0

计算结果为True,则需要中断循环.

还有一个打字错误

nodo* cursor = *head;

看来你的意思是

node* cursor = *head;

该函数可能如下所示

int insert( node** head, const char* word )
{
    node *newElem = malloc( sizeof( node ) );
    int success = newElem != NULL;

    if ( success )
    {
        success = ( newElem->data = malloc( strlen( word ) + 1 ) ) != NULL;

        if ( success )
        {
            strcpy( newElem->data, word );

            while ( *head != NULL && !( strcmp( word, ( *head )->data ) < 0 ) )
            {
                head = &( *head )->next;
            }

            newElem->next = *head;
            *head = newElem;
        }
        else
        {
            free( newElem );
        }
    }

    return success;
}

这是一个演示程序.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
    char *data;
    struct node *next;
} node;

int insert( node** head, const char* word )
{
    node *newElem = malloc( sizeof( node ) );
    int success = newElem != NULL;

    if ( success )
    {
        success = ( newElem->data = malloc( strlen( word ) + 1 ) ) != NULL;

        if ( success )
        {
            strcpy( newElem->data, word );

            while ( *head != NULL && !( strcmp( word, ( *head )->data ) < 0 ) )
            {
                head = &( *head )->next;
            }

            newElem->next = *head;
            *head = newElem;
        }
        else
        {
            free( newElem );
        }
    }

    return success;
}

void display( const node *head )
{
    for ( ; head; head = head->next )
    {
        printf( "\"%s\" -> ", head->data );
    }

    puts( "null" );
}

int main (void) 
{
    node *head = NULL;
    const char * data[] =
    {
        "7DJL", "-kiF", "8F4r", "7D7d", "-D7w", "-b7f"
    };
    const size_t N = sizeof( data ) / sizeof( *data );

    for ( size_t i = 0; i < N; i++ )
    {
        insert( &head, data[i] );
    }

    display( head );
}

程序输出为

"-D7w" -> "-b7f" -> "-kiF" -> "7D7d" -> "7DJL" -> "8F4r" -> null

C++相关问答推荐

C中空终止符后面的数字?

通过管道将一个子系统的标准输出发送到另一个子系统的标准输出

在函数中使用复合文字来初始化C语言中的变量

当输入负数时,排序算法存在问题

Make Node函数.S有什么问题吗?

1处的解析器错误:yacc语法的语法错误

X64:并发写入布尔数组

在WSL关闭/重新启动后,是什么原因导致共享对象依赖关系发生更改?

Square不与Raylib一起移动

用C++从外部ELF符号读取值

Caesar密码调试:输出文本末尾的问号和随机字符

从不兼容的指针类型返回&&警告,但我看不出原因

在C中使用无符号整数模拟有符号整数

计算SIZE_MAX元素的长数组的大小

我编写这段代码是为了判断一个数字是质数、阿姆斯特朗还是完全数,但由于某种原因,当我使用大数时,它不会打印出来

传递给函数的 struct 中的数组

UpDown控制与预期相反

将char*铸造为空**

使用 _Atomic float 时,MSVC 编译的代码会命中调试断言

M1 Mac 上的 jmp_buf 如何解码?