我想比較兩個鏈接列表。你能告訴我爲什麼我的代碼不起作用嗎?使用循環比較2個鏈接列表
如果兩個列表不同,則返回0;如果它們相同,則返回1。
int compare(struct Node *list1, struct Node *list2)
{
struct Node *node1 = list1;
struct Node *node2 = list2;
while ((node1) || (node2))
if (node1->data != node2->data)
return 0;
else {
node1 = node1->next;
node2 = node2->next;
}
if ((node1 == NULL) && (node2 == NULL))
return 1;
else
return 0;
}
嘿,夥計能你提供完整的代碼。我無法單獨測試這種方法。 – harshitgupta 2014-10-30 21:01:39
while條件應該使用'&&'而不是'||'。順便說一句,你過度使用括號! – ooga 2014-10-30 21:01:41
@ooga也許,爲第一個while循環。 – 2014-10-30 21:05:23