Segmentation Fault in Double-chained Lists

1

I have a problem with the implementation of 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 gives the line "while (temp-> temp! = NULL) temp = temp-> prev;" within the print function.

If I comment the line "l2 = insert (x, x, x + 1, x + 1.0, l2);" inside the main function, the code works.

Someone can help me to solve this problem, grateful already.

    
asked by anonymous 21.07.2016 / 18:13

1 answer

1

You do not initialize l1 and l2 in main to NULL - so you call inserir can assume that a list already exists, and simply try to put a pointer to the new node in the middle of the random memory, pointed by a l1 not initialized. That alone would cause an error. Where l1 and l2 point without the uniched values is random - it may be that the inserir itself does not lock, but that the initial value of l1 and l2 is the same and this causes some other error later.

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

Anyway, not initializing l1 and l2 to NULL is a very big error that can cause the comoration described.

    
21.07.2016 / 19:32