我試圖從雙向鏈表中刪除所有的偶數。雙鏈表刪除C++中的偶數
我已經考慮了案件:
- 空單
- 甚至在開始數,
- 什末數
- 偶數列表
- 中間偶數不在列表中
我的代碼如下不起作用p roperly:
void DoublyList::deleteEvens()
{
if (first == NULL) {
cerr << "Empty List" << endl;
}
else {
Node *current = first;
bool found = false;
while (current != NULL && (!found)) {
Node *tCurrent = current;
if (current->getNextLink() == NULL) {
if (current->getData() % 2 == 0) {
first = NULL;
last = NULL;
delete current;
current = NULL;
count = 0;
}
else {
cerr << "Not in the list" << endl;
found = true;
}
}
else if (current == first && current->getData() % 2 == 0) {
first = first -> getNextLink();
first->setPreviousLink(NULL);
current = current->getNextLink();
delete tCurrent;
tCurrent = NULL;
-- count;
}
else if (current == last && current->getData() % 2 == 0) {
last = last->getPreviousLink();
last->setNextLink(NULL);
delete current;
current = NULL;
-- count;
}
else if (current->getData() % 2 == 0) {
current->getPreviousLink()->setNextLink(current->getNextLink());
current->getNextLink()->setPreviousLink(current->getPreviousLink());
current = current->getNextLink();
delete tCurrent;
tCurrent = NULL;
}
else {
current = current->getNextLink();
}
}
}
}
_'My below will not work properly.'_ too fague question ... –
@πάνταῥεῖ我剛剛開始學習編程,我不知道如何具體,因爲有些代碼可以工作,有些時候不... – Julie
由於這是一個學校項目,你不應該得到代碼。但是,下面是如何解決它 - 將其分解成幾部分。首先編寫一個可以刪除雙向鏈表中的項目的函數。然後測試它。然後編寫一個函數來遍歷列表並查找所有偶數。測試它。然後結合這兩者。不要試圖將其作爲一個巨大的步驟。 –