簡體   English   中英

將字符串插入到 C 中的字符串排序鏈表中

[英]Insert a string into a sorted linked list of strings in C

我正在努力解決這個問題:我想將一個字符串插入到一個排序的字符串鏈表中,但由於某種原因它不起作用。 這是代碼:

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

它沒有用。 output 應該是:

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

謝謝您的幫助!

我不知道wordLenght是什么。 但無論如何在 function 中使用此名稱沒有任何意義,只會使 function 不清楚,因為該名稱未在 function 中定義。

無需將 function 分成兩部分。 它使 function 容易出錯。

此外,while 語句的條件

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

是不正確的。

如果這個表達式

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

評估為 true 您需要中斷循環。

還有一個錯字

nodo* cursor = *head;

看來你的意思

node* cursor = *head;

function可以看如下方式

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 );
}

程序 output 是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM