RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
c语言单链表插入节点函数,c语言实现单链表的基本操作

你好,请问一下怎么用C语言编写单链表插入节点程序,万分感谢!

#include "sll_node.h"

创新互联是一家专业提供应县企业网站建设,专注与成都网站设计、成都网站建设、外贸网站建设H5响应式网站、小程序制作等业务。10年已为应县众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

#include stdlib.h

#define FALSE 0

#define TRUE 1

// insertNode2:把newValue的值插入到递增排序的链表中,正确返回TRUE,错误返回FALSE

// nextp是指向当前节点的指针,最初是头指针

int insertNode2(Node **nextp, int newValue)

{

Node *newNode; // 新节点指针

Node *current; // 当前节点指针

current = *nextp; // 最初当前节点为nextp指针指向的节点

// 查找新插入节点的位置

while (current != NULL current-value newValue)

{

nextp = ¤t-next;

current = current-next;

}

// 为新节点分配内存

newNode = (Node *)malloc(sizeof(Node));

if (newNode == NULL)

return FALSE;

newNode-value = 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));

head-next = NULL;

for (int i = 0; i 5; 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语言单链表怎么插入节点

#include "sll_node.h"  

#include stdlib.h  

#define FALSE   0  

#define TRUE    1  

// insertNode2:把newValue的值插入到递增排序的链表中,正确返回TRUE,错误返回FALSE  

// nextp是指向当前节点的指针,最初是头指针  

int insertNode2(Node **nextp, int newValue)  

{  

Node *newNode; // 新节点指针  

Node *current; // 当前节点指针  

current = *nextp; // 最初当前节点为nextp指针指向的节点  

// 查找新插入节点的位置  

while (current != NULL  current-value  newValue)  

{  

nextp = ¤t-next;  

current = current-next;  

}  

// 为新节点分配内存  

newNode = (Node *)malloc(sizeof(Node));  

if (newNode == NULL)  

return FALSE;  

newNode-value = 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));  

head-next = NULL;  

for (int i = 0; i  5; 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语言链表插入一个新节点的函数问题

首先,主函数中,“请输入插入的数据”那里scanf应该是b,这是引发崩溃的原因。

其次,insert函数的目的应该是想插入数据后仍是有序链表。但你的insert函数逻辑太乱,有些不必要的判断,我修正了你的代码,贴给你看看。(虽然你insert是想保证有序,但你在创建的时候没有保证有序,所以最终结果不一定是有序。例如,创建 1,5,2,插入3,最后输出的是 1,3,5,2)

代码修改:

scanf("%d", b);

重写了insert函数,简化逻辑;

动态分配的内存记得释放,增加freeNode释放空间

#include stdio.h

#include stdlib.h

struct link

{

int data;

struct link *next;

};

struct link *add(struct link *head);//创建链表 

void display(struct link *head);//输出数据 

struct link *insert(struct link *head, int b); //插入新节点 

void freeNode(struct link *); //释放空间

int main()

{

char c;

struct link *head = NULL;

printf("要创建一个链表吗?");

scanf(" %c", c);

while (c == 'y' || c == 'Y')

{

head = add(head);

printf("要继续创建节点吗?");

scanf(" %c", c);

}

display(head);

int b;

printf("输入插入的数据");

scanf("%d", b);

head = insert(head, b);

display(head);

freeNode(head);

}

struct link *add(struct link *head)

{

int data;

struct link *p = (struct link*)malloc(sizeof(struct link));

if (head == NULL)

{

head = p;

}

else

{

struct link *pr = head;//一个临时指针pr先保存下head的地址 

while (pr-next != NULL)

{

pr = pr-next;

}

pr-next = p;

}

printf("输入数据");

scanf("%d", p-data);

p-next = NULL;

return head;

}

void display(struct link *head)

{

struct link *pr = head;

while (pr != NULL)

{

printf("%d\n", pr-data);

pr = pr-next;

}

}

struct link *insert(struct link *head, int b)

{

struct link *ptr = head, *prev = head;

struct link *newNode = (struct link *)malloc(sizeof(struct link));

newNode-data = b;

while (ptr  b  ptr-data) {

prev = ptr;

ptr = ptr-next;

}

newNode-next = ptr;

if (ptr == head) head = newNode;

else prev-next = newNode;

return head;

}

void freeNode(struct link *node) {

if (!node) return;

freeNode(node-next);

free(node);

}


文章题目:c语言单链表插入节点函数,c语言实现单链表的基本操作
链接分享:http://cqwzjz.cn/article/dsshdoi.html