双向链表遍历和移除
//单向链表和双向链表
template<class T>
struct FNode
{
FNode()
:Next(NULL)
,Previous(NULL)
{
}
FNode* Next;
FNode* Previous;
void* Data;
};
int main()
{
FNode<string> Node;
Node.Data = "Hello1";
{
Node.Next = new FNode<string>();
Node.Next->Data = "Hello2";
Node.Next->Previous = &Node;
Node.Next->Next = new FNode<string>();
Node.Next->Next->Data = "Hello3";
Node.Next->Next->Previous = Node.Next;
Node.Next->Next->Next = new FNode<string>();
Node.Next->Next->Next->Data = "Hello4";
Node.Next->Next->Next->Previous = Node.Next->Next;
}
auto PrintfNode = [&]()
{
FNode<string>* NodePtr = &Node;
while (NodePtr)
{
cout << NodePtr->Data << endl;
NodePtr = NodePtr->Next;
}
};
auto RemoveNode = [&](const string& RemoveString)
{
FNode<string>* NodePtr = &Node;
while (NodePtr)
{
if (NodePtr->Data == RemoveString)
{
NodePtr->Previous->Next = NodePtr->Next;
NodePtr->Next->Previous = NodePtr->Previous;
delete NodePtr;
NodePtr = NULL;
}
if (NodePtr)
{
NodePtr = NodePtr->Next;
}
}
};
RemoveNode("Hello3");
PrintfNode();
}