简体   繁体   English

将元素添加到排序列表

[英]Adding element to a sorted list

i've got a problem with list, more specific - adding new element to it.我的列表有问题,更具体地说 - 向它添加新元素。 This function is to add new element to a sorted list in proper place此功能是将新元素添加到排序列表的适当位置

ListEl* PushFrontsort(ListEl* head, ElType k)
{
    ListEl* x = (ListEl*) malloc(sizeof(ListEl));
    x->key = k;
    x->next = NULL;
    ListEl* y=head;
    ListEl* w=head;
    ListEl* new1 = (ListEl*) malloc(sizeof(ListEl));
    new1=head;
    w=w->next;
    if(head->key >= x->key)
    {
        x->next=head;
        new1=x;
    }
    else
    {
        while(w->key < x->key &&w->next)
        {
            y=w;
            w=w->next;
        }
        y->next=x;
        x->next=w;
    }
    return new1;
}

Type of the element is as following:元素类型如下:

typedef struct ListEl {
    ElType key;
    struct ListEl *next;
} ListEl;

When I try to add new, random element using following instructions:当我尝试使用以下说明添加新的随机元素时:

    int i;
    srand(time(NULL));
    ListEl* head = NULL;
    head = PushFront(head, rand()%100);
    for (i = 0; i < 20; i++)
        head = PushFrontsort(head, rand()%100);

not only sometimes doesn't it add 20 number and stops at 19, but very often program crashes.不仅有时它不添加 20 个数字并在 19 处停止,而且经常程序崩溃。 The whole problem is that I don't know what may cause this randomness of proper executing.整个问题是我不知道是什么导致了正确执行的这种随机性。 If you can give me any advice to improve it, I'd be very grateful.如果您能给我任何建议以改进它,我将不胜感激。 Any help will be appreciated任何帮助将不胜感激

The error only occurs when your second call for ListEl has a value for key larger than your first call.仅当您第二次调用ListElkey大于第一次调用时才会发生错误。 That's why the code was failing ~%50 of the time, because it all depends on which numbers were randomly called.这就是代码在大约 %50 次失败的原因,因为这完全取决于随机调用的数字。 As for "sometimes doesn't add 20 numbers and stop at 19", I'm not sure why this was happening, as I cannot reproduce this.至于“有时不添加 20 个数字并在 19 处停止”,我不确定为什么会发生这种情况,因为我无法重现这一点。

To fix your problem, from this answer, "If you need a pointer of a typedeffed type, declare an instance of one, but keep your typedefs to types so an not to mask levels of indirection."为了解决你的问题,从这个答案,“如果你需要一个类型定义类型的指针,声明一个实例,但将你的类型定义保留为类型,以便不屏蔽间接级别。”

So if you change your code to:因此,如果您将代码更改为:

int i;
srand(time(NULL));
ListEl a = {0, NULL};
ListEl* head = &a; 
head = PushFront(head, rand()%100);
for (i = 0; i < 20; i++)
    head = PushFrontsort(head, rand()%100);

you should no longer have any issue.你应该不再有任何问题。 Also, it would probably be good form to change your typedef to:此外,将 typedef 更改为:

typedef struct _ListEl {
    int key;
    struct _ListEl *next;
} ListEl;

So as to differentiate between the struct type and instance of struct type.从而区分struct类型和struct类型的实例。 To be honest, I'm not exactly sure why this needs to be done, so if someone more knowledgeable can edit/chime in, feel free.老实说,我不确定为什么需要这样做,所以如果有知识渊博的人可以编辑/插话,请随意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM