задачи1: Удаление в узле в списке> 3C++ Удаление узла в очереди
Описание:
Удаление шестого узла в списке семи, приводит к печати только первой и последний узел.
Доступные Узел Указатели: * next_ * prev_ * data_
Функция удаления указанного узла находится в LinkedList.cpp Имя: DeleteNode.
Функция, которая проходит по списку для печати узлов в main.cpp Название: PrintAllNodes
Возможное решение:
Будучи в состоянии получить доступ к токовым> prev_ в основной при перемещении для печати узлов ,
Код:
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 в main.cpp:
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