struct doubleList
{
int val;
doubleList * left;
doubleList * right;
doubleList(int num) : val(num), left(nullptr), right(nullptr) {}
};
bool insertNode(doubleList * p, int num)
{
doubleList * node = new doubleList(num);
doubleList * next = p->right;
p->right = node;
node->left = p;
node->right = next;
if (next != nullptr)
{
next->left = node;
}
return true;
}
bool deleteList(doubleList * head, doubleList * p)
{
if (p->right != nullptr)
{
doubleList * pLeft = p->left;
pLeft->right = p->right;
p->right->left = pLeft;
p->right = nullptr;
p->left = nullptr;
delete p;
p = nullptr;
}
else if (head == p)
{
head = head->right;
head->left = nullptr;
delete p;
p = nullptr;
}
else
{
p->left->right = nullptr;
p->left = nullptr;
delete p;
p = nullptr;
}
return true;
}
|