当我将一个元素添加到包含在链表数组中的链表中时,为什么列表中的所有其他元素都会被删除?

When I add an element to a linked list contained in an array of linked lists why every other element of the list gets deleted?

已更新

这段代码的问题是矩阵转化为链表数组后,看起来链表数组中没有任何值。 我认为问题出在将节点添加到链表数组的特定列表的函数中。

// node
struct node {
    int n;
    struct node* next_ptr;
};

// prototypes
void fill_mat(int mat[][SIZE]);
void mat_to_list(int mat[][SIZE]);
void insertSortedLinkedList(struct node *l, int value);
void print_mat(int mat[][SIZE]);
void print_vet();

// array of pointers
struct node* vet[SIZE];
int visitato[SIZE];

// main function
int main(int argc, const char *argv[]) {
    int mat[SIZE][SIZE];
    int i, j;

    // reset the matrix
    for(i = 0; i < SIZE; ++i) {
        for(j = 0; j < SIZE; ++j) {
            mat[i][j] = 0;
        }
    }

    // generate graph with weights
    srand(time(NULL));
    fill_mat(mat);

    // transform matrix in an array of linked lists
    mat_to_list(mat);
    print_mat(mat);
    printf("\n");
    print_vet();

    return 0;
}


// generate graph
void fill_mat(int mat[][SIZE]) {
    int x, y, z;

    for(x = 0; x < (SIZE * SIZE) / 2;) {
        y = rand() % SIZE;
        z = rand() % SIZE;

        if(mat[y][z] == 0) {
            mat[y][z] = rand() % 10 + 1;
            ++x;
        }
    }
}

// insert in list
void addNode(struct node **st, int d) {
    struct node *temp = *st;

    if(temp == NULL) {
        temp = malloc(sizeof(struct node));
    } else {
        while((temp)->next_ptr != NULL) {
            temp = temp->next_ptr;
        }

        temp->next_ptr = malloc(sizeof(struct node));
        temp = temp->next_ptr;
    }

    temp->n = d; // this must done using strncpy
    temp->next_ptr = NULL;
}

// transform matrix to array of linked lists
void mat_to_list(int mat[][SIZE]) {
    int i, j;

    // reset array
    for(i = 0; i < SIZE; ++i) {
        vet[i] = NULL;
    }

    for(i = 0; i < SIZE; ++i) {

        for(j = 0; j < SIZE; ++j) {
            if(mat[i][j] != 0) {
                    addNode(&(vet[i]), mat[i][j]);
            }
        }
    }
}

// print matrix
void print_mat(int mat[][SIZE]) {
    int i, j
    ;
    for(i = 0; i < SIZE; ++i) {
        for(j = 0; j < SIZE; ++j) {
            printf("%-2d ", mat[i][j]);
        }
        printf("\n");
    }
}

// print array of linked lists
void print_vet() {
    int i;
    struct node* temp;

    for(i = 0; i < SIZE; ++i) {
        printf("ARRAY CELL: %d\n", i);
        temp = vet[i];

        while(temp != NULL) {
            printf("NODE VALUE --> ");
            printf("%d\n", temp->n);
            temp = temp->next_ptr;
        }

        printf("\n");
    }
}

看起来你正在修改原始指针。 迭代器可用于查找最后一个节点。

在您的调用函数中,您可以创建一个迭代器。

 first = (node *) malloc(sizeof(node));
 iterator = first;

在你的函数中,你可以传递迭代器

void addNode(node *iterator, int d) {
    /*Create a new node*/
    newNode = (node *) malloc(sizeof(node));
    newNode->n = d;
    newNode->next_ptr = NULL;

    /*Iterate through your list to find end*/
    if (iterator != 0) {
        while (iterator->next != 0) {
            iterator = iterator->next;
        }
    }
    /*Add item to last link in list*/
    iterator->next = newNode;
}

我不明白哪里有列表数组以及您要显示您的函数版本多长时间。但在任何情况下,正确的函数都可以如下所示

void addNode( struct node **st, int d ) 
{
    while ( *st ) st = &( *st )->next_ptr;

    *st = malloc( sizeof( struct node ) );

    ( *st )->n = d;
    ( *st )->next_ptr = NULL;
}

或以下方式

int addNode( struct node **st, int d ) 
{
    while ( *st ) st = &( *st )->next_ptr;

    *st = malloc( sizeof( struct node ) );

    int success = *st != NULL;

    if ( success )
    {
        ( *st )->n = d;
        ( *st )->next_ptr = NULL;
    }

    return success;
}