如何在c语言的链表中插入字符串?
如果链接列表中不存在字符串userId,我将尝试插入该字符串。这是我的代码:如何在c语言的链表中插入字符串?,c,string,pointers,linked-list,nodes,C,String,Pointers,Linked List,Nodes,如果链接列表中不存在字符串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 p
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
}
if(pFind==NULL)
{
返回allocateNode(用户);pNext=ppreceedes->pNext;
其他的
pNew->pNext=ppreceides->pNext;
pPrecedes->pNext=pNew;我已经找到了正确的代码!!!我应该提到这一点,但我有另一个函数searchLL,它已经比较并搜索了用户,所以strcmp是不必要的。我必须创建两个新的if语句。一个是如果字符串已经存在,它只返回该字符串。另一个是在字符串h的情况下至于在没有前面字符串的开头插入,我还必须为我的新字符串打开一些空间。
节点*insertLL(节点**ppHead,用户)
{
}在return语句之后添加代码没有多大意义。它不会被执行。问题是?欢迎来到stackoverflow。请看下面的,然后读这个:。和这个:。然后你的问题就不可能回答了。我们现在只能说:“你的代码中可能有一个bug”1.我很高兴你抽出时间回复我!
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;