【问题标题】:A C function that inserts number into a linked list in ascending order将数字按升序插入链表的 C 函数
【发布时间】:2020-09-26 06:07:34
【问题描述】:

这是我的功能:

void IntListInsertInOrder (IntList L, int v)
{
    struct IntListNode *n = newIntListNode (v);
    if (L->first == NULL) { //case a, empty list
        L->first = L->last = n;
        L->size ++;
        return;
    }
    else if (v <= L->first->data) { // case b, smallest value
        n->next = L->first;
        L->first = n;
    }
    else if (v >= L->last->data) { // case c, largest value
        L->last->next = n;
        L->last = n;
    }
    else if (v > L->first->data && v <= L->first->next->data) { // case d, second-smallest value
        n->next = L->first->next;
        L->first->next = n;
    }
    else { //case f, value in the middle
        struct IntListNode *curr = L->first;
        while (curr->next->data < v) {
            curr = curr->next;
        }
        n->next = curr->next;
        curr->next = n;
    }
    L->size ++;
    return;
}

当我将 10 个数字的随机列表放入其中时,3/10 排序正确。错误似乎在最后一部分,但它看起来与我在网上找到的解决方案完全一样。

【问题讨论】:

  • 在此链接中查看add_ordered() function。 (您的 firstlast 在链接函数中似乎是 headtail
  • 构成不正确列表的插入顺序是什么?举一个例子
  • @Yuyan_Li 显示列表是如何定义的。

标签: c linked-list insert singly-linked-list function-definition


【解决方案1】:

好的,我想通了。我忘了在最后一个while循环的条件下添加&amp;&amp; curr-&gt; != NULL。在我添加后所有测试用例都通过了。

【讨论】:

  • 这怎么可能是个问题?到达最后的“while”循环意味着插入的元素严格大于第二个元素并且严格小于最后一个元素,这意味着“curr->next”永远不会为空。在最坏的情况下,curr->next 最终会达到严格更大的“L->last”。所以你的推理有问题
  • 我一直在想这个问题,不知何故它神奇地解决了这个问题,虽然我不知道为什么。
【解决方案2】:

你的函数太复杂了,if条件很多,容易出错,不可读。

您没有显示列表定义,但我猜您有一个双边单链表,因为代码中没有任何地方引用名为 prev 的数据成员。

这是一个演示程序,展示了如何简单地定义函数。

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

typedef struct Node
{
    int data;
    struct Node *next;
} Node;

typedef struct List
{
    Node *head;
    Node *tail;
    size_t size;
} List;

int insert_in_order( List *list, int data )
{
    Node *new_node = malloc( sizeof( Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;

        Node **current = &list->head;

        while ( *current != NULL && !( data < ( *current )->data ) )
        {
            current = &( *current )->next;
        }

        new_node->next = *current;

        if ( *current == NULL )
        {
            list->tail = new_node;
        }

        *current = new_node;

        ++list->size;
    }

    return success;
}

void clear( List *list )
{
    while ( list->head != NULL )
    {
        Node *current = list->head;
        list->head = list->head->next;
        free( current );
    }
}

void display( const List *list )
{
    printf( "There are %zu nodes in the list\n", list->size );
    printf( "They are: " );

    for ( const Node *current = list->head; current != NULL; current = current->next )
    {
        printf( "%d -> ", current->data );
    }

    puts( "null" );
}

int main(void) 
{
    List list = { .head = NULL, .tail = NULL, .size = 0 };

    srand( ( unsigned int )time( NULL ) );

    const size_t N = 10;

    for ( size_t i = 0; i < N; i++ )
    {
        insert_in_order( &list, rand() % ( int )N );
    }

    display( &list );

    clear( &list );

    return 0;
}

程序输出可能看起来像

There are 10 nodes in the list
They are: 1 -> 2 -> 3 -> 3 -> 6 -> 6 -> 7 -> 8 -> 8 -> 9 -> null

【讨论】:

  • 这是一个大学实验室的挑战,所以除了这个函数我不能改变任何东西。感谢您提供这个令人惊叹的解决方案,我觉得我在阅读您的代码时学到了很多东西。
猜你喜欢
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
相关资源
最近更新 更多