我正在研究一个基本上已经工作的链接列表,但我需要扩展我目前拥有的内容,但我遇到了问题。
我有一个存储来自电话的出站呼叫腿的结构。我将这些调用存储在一个链表中,该链表定义如下:
typedef struct CallLogSearchOutboundStruct
{
char * target;
float duration;
char * cleardownCause;
BOOL allowOverwrite;
struct CallLogSearchOutboundStruct * nextLeg;
} callLogSearchOutboundStruct;
我有基本的代码工作,我可以使用下面的代码成功地将新的出站呼叫添加到链接列表的末尾:
void insertOutboundLegToList(callLogSearchOutboundStruct * outboundLeg, char * target, float duration, int cleardown, BOOL overwriteFirstOutboundLegs)
{
f (outboundLeg->target == NULL)
{
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
outboundLeg->allowOverwrite = FALSE;
outboundLeg->nextLeg = NULL;
}
else
{
if (overwriteFirstOutboundLegs == FALSE)
{
while (outboundLeg->nextLeg != NULL)
{
outboundLeg = outboundLeg->nextLeg;
}
}
if (outboundLeg->nextLeg == NULL)
{
outboundLeg->nextLeg = (callLogSearchOutboundStruct*)malloc(sizeof(callLogSearchOutboundStruct));
outboundLeg = outboundLeg->nextLeg;
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
outboundLeg->nextLeg = NULL;
}
else
{
outboundLeg->target = NULL;
outboundLeg->duration = 0;
outboundLeg->cleardownCause = NULL;
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
}
}
}
此代码正在工作,但是我需要对其进行修改,以便如果设置了 allowOverwrite 标志,它将首先在链接列表中的第一个出站分支,覆盖它并将第一条分支的覆盖设置为 false,但所有其他分支在列表设置为允许覆盖。
因此,当需要插入新的出站呼叫时,如果覆盖第一支路设置为 false,则程序将需要循环遍历每个出站支路并检查该支路的允许覆盖是否设置为真,如果是,则覆盖那条腿,然后将覆盖标志设置为假,然后再次在下一个出站腿上,继续循环,直到它看到允许覆盖为真,覆盖并设置为假,这应该一直持续到下一个腿为空,然后它只是插入出站像往常一样将腿放在末端。
我认为我的基本逻辑是正确的,但是,当我从循环中中断时,我似乎继续对第一条腿进行 NULL 化,所以我最终没有出站腿。
下面是我如何修改代码以尝试实现我所需要的。
if (overwriteFirstOutboundLegs == TRUE)
{
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
outboundLeg->allowOverwrite = FALSE;
//Loop through existing outbound legs and set overwrite flag to TRUE
while (outboundLeg->nextLeg != NULL)
{
outboundLeg = outboundLeg->nextLeg;
outboundLeg->allowOverwrite = TRUE;
}
outboundLeg->nextLeg = NULL;
}
else
{
if (outboundLeg->target == NULL)
{
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
outboundLeg->allowOverwrite = FALSE;
outboundLeg->nextLeg = NULL;
}
else
{
if (outboundLeg->nextLeg == NULL)
{
outboundLeg->nextLeg = (callLogSearchOutboundStruct*)malloc(sizeof(callLogSearchOutboundStruct));
outboundLeg = outboundLeg->nextLeg;
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
outboundLeg->allowOverwrite = FALSE;
outboundLeg->nextLeg = NULL;
}
else
{
while (outboundLeg->nextLeg != NULL)
{
outboundLeg = outboundLeg->nextLeg;
if (outboundLeg->allowOverwrite == TRUE)
{
break;
}
}
outboundLeg->target = strdup(target);
outboundLeg->duration = duration;
outboundLeg->cleardownCause = strdup(setCallResult(cleardown));
outboundLeg->allowOverwrite = FALSE;
outboundLeg->nextLeg = NULL;
}
}
}
我正在使用以下代码调用该函数:
insertOutboundLegToList(outboundCallLegStartPtr, targetBuffer, durationBuffer, atoi(rowReport[cleardownColIndex]), overwriteFirstOutboundLegs);
下面还附有一张图表,显示了我插入新腿所需的流程。
感谢您的任何帮助,您可以提供。