本系列文章简要介绍链表的相关知识。

本文是系列文章的第二篇,将介绍在单向链表中插入节点的方法,并给出代码示例。

1 概述

在单向链表中插入节点,是要将待插入节点插入到链表的“合适”位置中。所以,这里需要一个前提条件:单向链表中的各节点是按其某个成员(如学生的成绩)的值有序排列(如由小打到)的。只有具备了这个前提条件,才能进行针对单向链表的节点插入操作。

为了将待插入节点插入到链表的“合适”位置中,需要进行两个步骤:

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

下面通过伪代码的形式介绍实现上述两个步骤的算法。

算法:InsertLinkedlist(list,new)
目的:在单向有序链表中插入节点
前提:链表和要插入的节点数据
后续:无
返回:新链表
{if (list == null)           // insert into empty list{list <- new(*new).link <- null}else{while (((*new).data > (*cur).data) && ((*cur).link != null))    // find location where the new node insert into{pre <- curcur <- (*cur).link}if ((*new).data <= (*cur).data){if (list == cur)                // insertion at the beginning{list <- new(*new).link <- cur}else                            // insertion in the middle{(*pre).link <- new(*new).link <- cur}}if ((*cur).link == null)            // insertion at the end{(*cur).link <- new(*new).link <- null}}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 */{head = new;(*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 */{head = new;(*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;}return head;
}/*
*  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;
}

上述代码的编译及运行结果如下:

为了验证在链表头、链表尾插入的情况,可以修改结构体成员“stu_new.stu_score”的值,使节点满足在链表头、链表尾插入的条件。代码修改后的编译及运行情况如下:

说明:

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

链表简介(二)——在单向链表中插入节点相关推荐

  1. 双链表中插入节点(C语言实现)

    文章目录 1. 相关背景介绍 1.1 双链表概念 1.2 双链表的优势与劣势 1.3 双链表插入节点的位置 2. 不同位置插入数据 2.1 在DLL的前端添加节点 2.2 在给定节点之前添加节点 2. ...

  2. 数据结构链表之单向链表:Python3 实现单向链表——1

    Python3 实现单向链表 链表定义与简介 定义:链表与顺序表(Python中列表)性质相反,链表是物理单元上非顺序的.非连续的,在逻辑顺序上其数据元素是通过指针实现的,组成链表的每一个元素也可以叫 ...

  3. 拿捏链表(二)—— 反转链表

    文章目录 题目描述 思路一 代码实现 思路二 代码实现 题目描述 反转一个单链表.(题目来源) 思路一 其实,反转一个单向链表,我们可以看成是将链表中的每个结点的指向反向(即从后一个结点指向前一个结点 ...

  4. 链表问题8——将单向链表按某值划分成左边小、中间相等、右边大的形式(进阶)

    题目 给定一个单向链表头节点head,和一个整数pivot. 实现一个调整链表的函数,将链表调整为左部分小于pivot,中间等于,右边大于pivot的.调整后的节点顺序要保持与原链表中节点的先后次序一 ...

  5. 链表问题8——将单向链表按某值划分成左边小、中间相等、右边大的形式(初阶)

    题目 给定一个单向链表头节点head,和一个整数pivot. 实现一个调整链表的函数,将链表调整为左部分小于pivot,中间等于,右边大于pivot的.对调整后的节点顺序没有更多的要求 链表9-> ...

  6. [******] 链表问题:将单向链表按某值划分成左边小、中间相等、右边大的形式...

    问题描述 普通问题:给定一个单向链表的头节点head,节点的值类型是整数,再给定一个整数 pivot,实现一个调整链表的函数: 使得左半部分的值都是小于pivot的节点,中间部分都是等于pivot的节 ...

  7. 链表问题4——反转单向链表

    题目 实现反转单向链表的函数 要求 如果链表长度为N,时间复杂度要求为O(N),额外空间复杂度要求为O(1). 源码 public class Node{public int value;public ...

  8. 链表之反转部分单向链表

    package com.chenyu.zuo.linkedList;import com.chenyu.zuo.linkedList.RemoveByRatio.Node;/*** 题目:给定一个单向 ...

  9. 《程序员代码面试指南》第二章 链表问题 反转部分单向链表

    题目 给一个单向链表和开始和结束的位置,将这两位置区间链表进行反转 java代码 /*** @Description:反转部分单向链表* @Author: lizhouwei* @CreateDate ...

  10. 链表C语言实现--单向链表

    线性结构的链式存储也称为链表,相比于顺序表,链表能够解决顺序表中空间资源浪费问题以及空间不足的问题.链表的每一个结点包含数据域和指针域,而每一个结点在内存中的地址是不连续的,且能适应动态变化.在数据插 ...

最新文章

  1. 【编程题目】编程判断俩个链表是否相交 ☆
  2. JavaScript之词法作用域和动态作用域
  3. PHP Warning: date(): It is not safe to rely on the system's timezone settings
  4. 刚毕业的参加工作的黄金时期的核心策略:打好基础
  5. 跳一跳201803-1
  6. jet mysql连接字符串,关于jet db的连接字串,以及加密后的字串-数据库专栏,SQL Server...
  7. 强制关机对电脑的影响_电脑强制关机,对电脑有影响吗?你被伪科普骗了多久?...
  8. ionic 中文 API CSS and javascript link
  9. app访问java web_Java Web App体系结构
  10. Java排序算快速排序_Java排序算法 [快速排序]
  11. SWIFT4.0学习01 - 函数的命名、调用以及注意事项
  12. python 协程小程序(草稿有待完善)
  13. MongoDB-Getting Started with the C# Driver
  14. 普林斯顿微积分读本-[美]阿德里安·班纳著-修订版;杨爽, 赵晓婷, 高璞译
  15. HTML前端连接go语言后段,一次完整的浏览器请求响应过程-Go语言中文社区
  16. 投票服务器维护时间,【维护】4月1日官方维护公告(正式服)
  17. 如何运营一个微信公众号
  18. HTML实现图片切换
  19. 【轮播图】使用bootstrap轮播插件(Carousel)
  20. java jdom 类_JDOM常用类介绍及示例代码

热门文章

  1. 设计模式的征途—23.解释器(Interpreter)模式
  2. 无法访问移动磁盘显示磁盘未被格式化的文件寻回方案
  3. Volley 源码分析
  4. Oracle数据库to_date()和to_char()的相关
  5. mac上启用tftp服务器
  6. Android各版本代号、版本号、API/NDK级别、发布时间及市场份额
  7. PO、VO、BO、DTO、POJO、DAO之间的关系
  8. iPhone文件系统:创建、重命名以及删除文件
  9. 跨地域为同事广播幻灯片
  10. pku1088----滑雪(记忆性搜索)