struct Node { int* pData Node* pNext Node* pPrev } 写出插入节点和删除节点
时间: 2023-12-19 17:05:26 浏览: 84
插入节点:
```
void insertNode(Node* pHead, int data)
{
Node* newNode = new Node;
newNode->pData = new int(data);
newNode->pNext = nullptr;
newNode->pPrev = nullptr;
if(pHead == nullptr) //链表不存在,新建链表
{
pHead = newNode;
}
else
{
Node* pNode = pHead;
while(pNode->pNext != nullptr)
{
pNode = pNode->pNext;
}
pNode->pNext = newNode;
newNode->pPrev = pNode;
}
}
```
删除节点:
```
void deleteNode(Node* pHead, int data)
{
if(pHead == nullptr)
{
return;
}
Node* pNode = pHead;
while(pNode != nullptr && *(pNode->pData) != data)
{
pNode = pNode->pNext;
}
if(pNode == nullptr) //未找到节点
{
return;
}
//找到了节点,进行删除
if(pNode->pPrev != nullptr)
{
pNode->pPrev->pNext = pNode->pNext;
}
else
{
pHead = pNode->pNext;
}
if(pNode->pNext != nullptr)
{
pNode->pNext->pPrev = pNode->pPrev;
}
delete pNode->pData;
pNode->pData = nullptr;
delete pNode;
pNode = nullptr;
}
```
阅读全文