typedef struct NODE
{
int value;
NODE *plink;
}Node;
/*能够获取到根指针,并且修改根指针的指向,能够检查链表是否到底*/
int SingleListInsert(Node **rootp, int NewValue)
{
Node *Current=NULL;
Node *Previous=NULL;
Node *UnitNew;
Current = *rootp; //获取链表根指针
if (Current==NULL)
{
return -1;
}
while ((Current!=NULL)&&(Current->value<NewValue))
{
Previous = Current;
Current = Current->plink;
}
UnitNew = (Node *)malloc(sizeof(Node));
if (UnitNew==NULL)
{
return -1;
}
UnitNew->value = NewValue;
if (Previous==NULL) //传过来的数据要插入到第一个节点处
{
*rootp = UnitNew;
}
else //传过来的数据要插入到中间节点或者最后的节点处
{
Previous->plink = UnitNew;
}
UnitNew->plink = Current;
return 0;
}
单链表插入操作
最新推荐文章于 2023-12-02 10:41:28 发布