我有以下两个(单链接列表)两个不同的结构:
struct Main {
int data1;
std::string some_data;
Main *next;
}
struct Side {
int data2;
int data3;
Side *next;
}
以下两个功能仅从特定链接列表Main
或Side
中删除第一个节点:
void DeleteFirstMain(Main *&pHeadMain)
{
Main *MainToBeDeleted;
if (pHeadMain == nullptr)
std::cout << "List is already empty.";
else
{
MainToBeDeleted = pHeadMain;
pHeadMain = pHeadMain->next;
/* Clears the memory occupied by first node*/
delete MainToBeDeleted;
}
}
void DeleteFirstSide(Side *&pHeadSide)
{
Side *SideToBeDeleted;
if (pHeadSide == nullptr)
std::cout << "List is already empty.";
else
{
SideToBeDeleted = pHeadSide;
pHeadSide = pHeadSide->next;
/* Clears the memory occupied by first node*/
delete SideToBeDeleted;
}
}
一切正常。
但是,我认为这两个功能可以合并为一个,从而使代码更整洁-两个功能本质上是同一件事,但是结构不同。这是非常基础的东西,但是在C ++中,我不知道如何正确地将“模板”结构作为参数传递给函数。
一如既往,我们非常感谢您的帮助。谢谢。