c语言链表插入节点函数 c语言链表节点是什么( 三 )


Node *current; // 当前节点指针
current = *nextp; // 最初当前节点为nextp指针指向的节点
// 查找新插入节点的位置
while (current != NULLcurrent-valuenewValue)
{
nextp = ¤t-next;
current = current-next;
}
// 为新节点分配内存
newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL)
return FALSE;
newNode-value = https://www.04ip.com/post/newValue;
// 统一了插入的步骤 。即:每次插入,都是前一个指针指向新节点 , 新节点指向下一个节点
*nextp = newNode;
newNode-next = current;
return TRUE;
}
main函数
[cpp] view plain copy
#include stdio.h
#include stdlib.h
#include time.h
#include "sll_node.h"
int insertNode(Node **rootp, int newValue);
int insertNode2(Node **nextp, int newValue);
int main()
{
srand(time(0));
Node *head = (Node *)malloc(sizeof(Node));
【c语言链表插入节点函数 c语言链表节点是什么】head-next = NULL;
for (int i = 0; i5; i++)
{
int temp = rand() % 50;
printf("%d\n", temp);
//insertNode(head,temp);
insertNode2(head,temp);
}
Node *p = head-next;
while (p != NULL)
{
printf("%d\n", p-value);
p = p-next;
}
getchar();
getchar();
return 0;
}
关于c语言链表插入节点函数和c语言链表节点是什么的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读