链表简介（二）——在单向链表中插入节点

65 篇文章 254 订阅
22 篇文章 1 订阅
3 篇文章 0 订阅

1 概述

1. 找到链表中合适的位置；
2. 将节点插入到步骤1找到的位置中。

{
if (list == null)           // insert into empty list
{
list <- new
}
else
{
while (((*new).data > (*cur).data) && ((*cur).link != null))    // find location where the new node insert into
{
pre <- cur
}

if ((*new).data <= (*cur).data)
{
if (list == cur)                // insertion at the beginning
{
list <- new
}
else                            // insertion in the middle
{
}
}

if ((*cur).link == null)            // insertion at the end
{
}
}

return list
}

2 代码示例

#include <stdio.h>

#define STRUCT_LEN sizeof(struct student)

struct student
{
int stu_num;                /* student number */
float stu_score;            /* student score */
struct student* next;
};

int main()
{
/* declaration of func */
struct student * insert(struct student * head, struct student * new_node);
void print(struct student * list);

/* create a static linked list, which sorted by student score from small to large */
struct student * list;
struct student stu_1, stu_2, stu_3;
stu_1.stu_num = 1;
stu_1.stu_score = 88;
stu_2.stu_num = 2;
stu_2.stu_score = 66;
stu_3.stu_num = 3;
stu_3.stu_score = 100;
list = &stu_2;
stu_2.next = &stu_1;
stu_1.next = &stu_3;
stu_3.next = NULL;

/* print linked list before insertion */
printf("before insertion, content of linked list as followed(sorted by student score from small to large):\n");
print(list);

/* create new node that wants to insert into linked list */
struct student stu_new;
stu_new.stu_num = 5;
stu_new.stu_score = 83;

/* insert new node into linked list */
list = insert(list, &stu_new);

/* print linked list after insertion */
printf("after insertion, content of linked list as followed(sorted by student score from small to large):\n");
print(list);

return 0;
}

/*
** this is the insert linked list function.
*/
struct student * insert(struct student * head, struct student * new_node)
{
struct student * cur;
struct student * pre;
struct student * new;

cur = head;                 /* let cur point first node */
new = new_node;             /* let new point the node that wants to insert into linked list */

if (NULL == head)           /* insert into empty list */
{
(*new).next = NULL;
}

while (((*new).stu_score > (*cur).stu_score) && ((*cur).next != NULL))  /* find location where the new node insert into */
{
pre = cur;
cur = (*cur).next;
}

if ((*new).stu_score <= (*cur).stu_score)
{
if (head == cur)        /* insertion at the beginning */
{
(*new).next = cur;
}
else                    /* insertion in the middle */
{
(*pre).next = new;
(*new).next = cur;
}
}

if ((*cur).next == NULL)        /* insertion at the end */
{
(*cur).next = new;
(*new).next = NULL;
}

}

/*
*  this is the print linked list content function.
*/
void print(struct student * list)
{
struct student *walker;
walker = list;

printf("The linked list contents(student number and score) as followed:\n");
printf("[student number] [student score]\n");

while (walker != NULL)
{
printf("%d                %-f\n", (*walker).stu_num, (*walker).stu_score);
walker = (*walker).next;
}

return;
}

• 为了简化代码内容，上述代码示例使用的是静态链表，即链表的所有节点都是在程序中定义的，节点的存储空间不是临时开辟的；
• 上述代码示例中，链表是按照学生的分数由小到大排序的。

• 0
点赞
• 10
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
06-01
10-19
07-29 1417
10-21 3007
12-30 1541
03-25 3235
05-23 714
09-04
08-21 31万+
08-21 30万+

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

liitdar

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。