c - 在链表中插入元素 - 索引不存在

标签 c linked-list

我得到了一个 C 代码示例,用于在给定索引处的链接列表中插入新元素。但是使用原始代码时,我总是得到索引不正确的错误,并且我不知道是否我不理解代码,或者代码中是否有错误。

插入操作定义如下:

void insert (list *l, int e, int index) {
    int i;
    node *tmp;
    node *prev;
    i=1;
    prev=l->first;
    while (!end(prev) && (i<index-1)) {
        i++;
        prev=prev->next;
    }
    if ( ((i+1) <= index) ) {
        printf("\n Error: index position doesn't exist\n");
    }
    else {
        tmp = (node *)malloc(sizeof(node));
        if (tmp == NULL) {
            printf("\n Error: Not enough free memory\n");
        }
        else {
            tmp->e = e;
            if (emptyList(*l)) {
                /* empty list */
                tmp->next=NULL;
                l->first=tmp;
            }
            else {
                if (index == 1) {
                    /* no previous element */
                    tmp->next=l->first;
                    l->first=tmp;
                }
                else {
                    /* standard case */
                    tmp->next=prev->next;
                    prev->next=tmp;
                }
            }
        }
    }
}

对于 1 以外的任何索引,我总是会收到索引位置不存在错误。而且我知道有效索引应该在范围 (1 <= index <= number_elements+1 )

如果我修改给出错误的条件如下:

if ( ((i+1) < index) ) {
    printf("\n Error: index position doesn't exist\n");
}

然后它就可以工作,除非索引是列表元素的数量+2,这会导致段错误。

有办法解决这个问题吗?我想出了一种方法,使用辅助函数来计算列表元素的数量:

if ( index > count_elements(l)+1 ) {
    printf("\n Error: index position doesn't exist\n");
}

但我想知道如何使用 insert 函数中的 i 变量来解决这个问题。

这是我使用的运行代码的简短版本:

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

typedef struct tnode {
    int e;
    struct tnode *next;
} node;

typedef struct {
    node *first;
} list;


int end(node *n)
{
       return (n==NULL);
}

int emptyList (list l)
{
       return(l.first==NULL);
}

void createList(list *l)
{
    l->first=NULL;
}


    void insert (list *l, int e, int index) {
        int i;
        node *tmp;
        node *prev;
        i=1;
        prev=l->first;
        while (!end(prev) && (i<index-1)) {
            i++;
            prev=prev->next;
        }
        if ( ((i+1) <= index) ) {
            printf("\n Error: index position doesn't exist\n");
        }
        else {
            tmp = (node *)malloc(sizeof(node));
            if (tmp == NULL) {
                printf("\n Error: Not enough free memory\n");
            }
            else {
                tmp->e = e;
                if (emptyList(*l)) {
                    /* empty list */
                    tmp->next=NULL;
                    l->first=tmp;
                }
                else {
                    if (index == 1) {
                        /* no previous element */
                        tmp->next=l->first;
                        l->first=tmp;
                    }
                    else {
                        /* standard case */
                        tmp->next=prev->next;
                        prev->next=tmp;
                    }
                }
            }
        }
    }

int main(int argc, char **argv)
{

    list l;

    createList(&l);

    printf("insert at index 1\n");
    insert(&l, 10, 1);

    printf("insert at index 1\n");
    insert(&l, 20, 1);

    printf("insert at index 2\n");
    insert(&l, 30, 2);

    return 0;
}

谢谢

最佳答案

if ( ((i+1) <= index) ) 

也许这就是你想要的条件?

if ( ((i+1) <= index) && end(prev)) 

关于c - 在链表中插入元素 - 索引不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49717768/

相关文章:

CUDA:具有共享内存和非 block 大小倍数的矩阵大小的平铺矩阵-矩阵乘法

c - 使用 scanf 时 getchar 不会停止

c - 如何将不同的元素插入数组?

c++ - 带有标题节点的循环双重链接列表

c - 单向链表 - push_back

java - 在java中删除一个节点

java - 链接列表 : Iterator vs List Iterator

c++ - posix 中的健壮 rwlock

C (linux) - 模拟/跳过 scanf 输入

java文件输入数字对链表