在我的代码中,有2个列表。我想分开它们:偶数和奇数 我写了一些东西。但是,它没有打印我想要的东西。我该怎么办?
if((list->data) % 2 == 1){
oddList = list;
list = list->nextPtr;
oddList->nextPtr = NULL;
printList(oddList);
} else {
evenList = list;
list = list->nextPtr;
evenList->nextPtr = NULL;
}
list = list->nextPtr;
结果是:
list1 : 4 17 17 19 21 28 31
list2 : 5 5 10 14 19 31 34 35
oddList : 5 17 19 31 31
evenlist :
但oddList
必须是:5 5 17 17 19 19 21 31 31 35
答案 0 :(得分:3)
您正在推进list
两次
if ((list->data) % 2 == 1) {
oddList = list;
list = list->nextPtr; // first advance
oddList->nextPtr = NULL;
printList(oddList);
} else {
evenList = list;
list = list->nextPtr; // first advance
evenList->nextPtr = NULL;
}
list = list->nextPtr; // second advance
}