代码之家  ›  专栏  ›  技术社区  ›  amr125

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

  •  -1
  • amr125  · 技术社区  · 6 年前

    我得到了一个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<=索引<=number\u元素+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");
    }
    

    但我想知道如何使用 中的变量 插入 作用

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

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

    谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   purec    6 年前
    if ( ((i+1) <= index) ) 
    

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

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