Segmentation Fault in Double Chained Lists

Asked

Viewed 65 times

1

I’m having a problem implementing double-chained lists, my code is as follows::

#include <stdio.h>
#include <stdlib.h>
struct MoveList
{
    int curRow, curCol, newRow, newCol, isEat;
    struct MoveList *next, *prev;
};

struct MoveList *getNewNode(int cRow, int cCol, int nRow, int nCol, int isEat)
{
    struct MoveList *newNode = (struct MoveList *)malloc(sizeof(struct MoveList));
    newNode->curCol = cCol;
    newNode->curRow = cRow;
    newNode->newCol = nCol;
    newNode->newRow = nRow;
    newNode->isEat = isEat;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
};

struct MoveList *inserir(int cRow, int cCol, int nRow, int nCol, int isEat, struct MoveList *lista)
{
    struct MoveList *newNode = getNewNode(cRow, cCol, nRow, nCol, isEat);

    if(lista == NULL) return newNode;

    newNode->prev = lista;
    lista->next = newNode;
    return newNode;
}

struct MoveList *merge(struct MoveList *l1, struct MoveList *l2)
{
    if(l1 == NULL && l2 == NULL) return NULL;
    if(l1 == NULL) return l2;
    if(l2 == NULL) return l1;

    struct MoveList *tmp1 = l1, *tmp2 = l2;

    while(tmp1->next != NULL) tmp1 = tmp1->next;
    while(tmp2->prev != NULL) tmp2 = tmp2->prev;

    tmp1->next = tmp2;
    tmp2->prev = tmp1;

    while(tmp1->next != NULL) tmp1 = tmp1->next;

    return tmp1;
}

void imprimir(struct MoveList *lista)
{
    struct MoveList *temp = lista;

    if(temp == NULL) return;

    while(temp->prev != NULL) temp = temp->prev;

    while(temp!=NULL)
    {
        printf("%d:%d -> %d:%d\n", temp->curRow, temp->curCol, temp->newRow, temp->newCol);
        temp = temp->next;
    }
}

int main()
{
    struct MoveList *l1, *l2;
    int x;
    for(x=0;x<5;x++)
    {
        l1 = inserir(x,x,x+1,x+1,0,l1);
        l2 = inserir(x,x,x+1,x+1,0,l2);
    }
    imprimir(l1);
}

The error is in the line "while(temp->Prev != NULL) temp = temp->Prev;" within the print function.

If I comment the line "L2 = insert(x,x,x+1,x+1,0,L2);" within the main function, the code works.

Someone can help me solve this problem, thank you in advance.

1 answer

1

You do not initialize L1 and L2 in main to NULL - thus calling to inserir pdoe assume that there is already a list, and simply try to put a pointer to the new node in the middle of the random memory, pointed by a l1 not initialized. This alone would cause an error. Where L1 and L2 point without the initialized Srem valves is random - it may be that the inserir in itself does not stop, but that the initial value of l1and l2 be the same and this causes some other mistake later.

Another error that seems to have there is in function inserir in itself - the code assumes that the address it receives is always the last node of a list, and never the beginning, or the middle. it is better to specify the documentation of the method.

Anyway, not initializing L1 and L2 to NULL is a very big error that can cause the described comortamento.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.