1
問題1的節點:在列表中的一個節點刪除> 3C++刪除在隊列
描述:第六節點的在七個列表
缺失,導致只有第一 打印和最後一個節點。
可用節點指針: * next_,* prev_,* data_中
功能刪除指定的節點是在LinkedList.cpp 名稱:DeleteNode。
功能,通過該列表穿越打印節點是在main.cpp中 名稱:PrintAllNodes
可能的解決方案:
穿越打印的節點時,能夠訪問電流 - > prev_主。
代碼:在main.cpp中
void LinkedList::DeleteNode(Node* node)
{
Node *Current = first_; // I want this to be my only Node Ptr Varaible Declaration.
if (NULL == first_)
std::cout << "Cannot delete from an empty list: \n";
//TRAVERSING WAS/IS A BAD IDEA.............................
while (Current != NULL)
{
if (Current->data_ == node->data_)
{
//If Current isn't the head of the list, set prev to next
if (Current != first_)
{
Current->prev_ = first_; //statement that follows crashes if this is not assigned.
Current->prev_->next_ = Current->next_;
}
else
{
first_ = Current->next_;
if (first_ != NULL)
first_->prev_ = NULL;
}
//If Current isn't the tail of the list, set next to prev
if (Current->next_ != NULL)
Current->next_ = Current->prev_;
else if (Current->prev_ != NULL)
Current->prev_->next_ = NULL;
listLen_--;
delete Current;
Current = NULL;
}
else
{
Current->prev_ = Current;
Current = Current->next_;
}
}
return;
}
代碼PrintAllNodes:
void PrintAllNodes(LinkedList *LinkedObject, long length = 0)
{
const char *Names = NULL;
length = LinkedObject->GetListLength();
Node *GetNode = LinkedObject->GetFirstNode();
for (signed short x = 0; x < length; x++)
{
Names = static_cast< NameObject* >(GetNode->data_)->GetName();
cout << Names << endl;
GetNode = GetNode->next_; // traversing
}
return;
}
有問題?如果你真的可以解釋作業問題並指出什麼似乎是問題而不是複製/粘貼並等待其他人解決,那麼你可以爲你的問題增加價值。 – stefanB 2009-05-28 07:15:54