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.