-1
Функция removeNode() реализует циклический двойной список, имеющий узел-дозор. То, что я пытаюсь сделать, определяется в псевдокоде рядом с функцией. Мне просто сложно понять, как это сделать. ФункцияЦиклический двойной связанный список C++
#include "CDLList.h"
#include <iostream>
using namespace std;
ListNode *createList()
{
ListNode *sentinel = new ListNode();
sentinel->last = sentinel;
sentinel->next = sentinel;
return sentinel;
}
void destroyList(ListNode *&sentinel)
{
// Delete any item nodes
clearList(sentinel);
// Delete the sentinel node
delete sentinel;
sentinel = nullptr;
}
bool isEmpty(ListNode *sentinel)
{
return (sentinel == sentinel->next);
}
ListNode *findNode(ListNode *sentinel, string value)
{
ListNode *pCurrNode = sentinel->next;
while (pCurrNode != sentinel)
{
// Check if we found the node
if (pCurrNode->item == value)
{
return pCurrNode;
}
// Move to next node
pCurrNode = pCurrNode->next;
}
return nullptr;
}
void addItem(ListNode *sentinel, string value)
{
ListNode *newNode = new ListNode;
newNode->item = value;
newNode->last = sentinel->last;
newNode->next = sentinel;
sentinel->last->next = newNode;
sentinel->last = newNode;
}
void removeNode(ListNode *node) // Implement this function!
{
// Unlink node
// Delete node
}
RemoveNode() вызывается в пределах этих двух функций
void removeItem(ListNode *sentinel, string value)
{
ListNode *node = findNode(sentinel, value);
// If the item was not found, there's nothing to do (remove)
if (node == nullptr)
{
return;
}
removeNode(node);
}
void clearList(ListNode *sentinel)
{
while (!isEmpty(sentinel))
{
removeNode(sentinel->next);
}
}
Попробуйте сделать это на бумаге первый ... Нарисуйте несколько списков на бумаге, а затем посмотрите, как вы можете сделать, чтобы удалить различные узлы в этих списках. Когда вы думаете, что вы это выяснили на бумаге, попробуйте перевести это решение в код. Списки, которые я рекомендую вам «проверить», включают пустой список, список с единственным узлом, список, в котором вы удаляете первый узел, последний узел или внутренний узел. –
Все еще работает в пустой с фактической реализации этого. Какие-нибудь дополнительные предложения? – bradym55