如果链接列表中不存在字符串userId,我将尝试插入该字符串。这是我的代码:
Node *insertLL(Node **ppHead, User user){
Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;
int pHead;
Fillup fillup; //file with user information
//searches for the user
pFind= searchLL(*ppHead, fillup.szUserId, &pPrecedes);
if(pFind == NULL)
{
return allocateNode(user);
if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;
}
return pNew;
}
发布的代码出现的问题肯定对没有帮助
if(pFind == NULL)
{
return allocateNode(user); <<< == nothing will be executed after this line
if (strcmp(pPrecedes->user.szUserId, pFind)!= 0) //pFind seems wrong
pNew->pNext = pPrecedes->pNext;
else
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew; <<<<< == should this line be dependant on the else above. The indentation says yes but there are no braces
}
是的,我知道,我会被投票否决,因为这"不是一个答案"——不管怎样,我会接受打击,看看我是否能帮助
我已经找到了正确的代码!!!我本应该提到这一点,但我有另一个函数searchLL,它已经比较并搜索了用户,所以strcmp是不必要的。我不得不做两个新的if语句。其中,如果字符串已经存在,则只返回该字符串。另一种情况是,字符串必须插入到没有前一个字符串的开头。我还必须为我的新琴弦腾出一些空间。节点*insertLL(节点**ppHead,用户-用户({
Node *pPrecedes;
Node *pNew;
Node *pNext;
Node *pFind;
//searches the user //searchLL is a differen't function
pFind= searchLL(*ppHead, user.szUserId, &pPrecedes);
if (pFind!= NULL) //if pFind already exists
return pFind;
pNew=allocateNode(user);//get space for pNew
if(pFind == NULL) //if pFind doesn't exist insert pFind
{
//insert at the beginning
if(pPrecedes == NULL) //if there is no pPrecedes
{
pNew->pNext = *ppHead; //pNew's next will be the head
*ppHead = pNew; //pNew is now the new head
}
//insert in the middle
else
{
pNew->pNext = pPrecedes->pNext;
pPrecedes->pNext = pNew;
}
}
return pNew;
}