建立单链表 单链表的插入

All possible cases:

所有可能的情况:

  1. Inserting at beginning

    开始插入

  2. Inserting at the ending

    在末尾插入

  3. Inserting at given position

    在给定位置插入

Algorithms:

算法:

1)开始插入 (1) Inserting at the beginning)

In this case, a new node is to be inserted before the current head node, i.e. , every time the node that got inserted is being updated to the head node. Such insertion can be done using following steps.

在这种情况下,要在当前头节点之前插入一个新节点,即每次将插入的节点更新为头节点时。 可以使用以下步骤完成这种插入。

  1. Update next pointer of the new node (node to be inserted) to point to the current node.

    更新新节点(要插入的节点)的下一个指针,使其指向当前节点。

  2. Update new node as head node.

    节点更新为头节点。

2)在结尾处插入 (2) Inserting at the ending)

In such case the new node is going to be the last node, i.e. , the next pointer of the new node is going to be NULL. The steps are:

在这种情况下,新节点将成为最后一个节点,即新节点的下一个指针将为NULL。 这些步骤是:

  1. Set the next pointer of the new node to be NULL.

    将新节点的下一个指针设置为NULL。

  2. Last node of the existing node is linked with the new node, i.e. , the last node's(existing) next pointer points to the new node.

    现有节点的最后一个节点与节点链接,即,最后一个节点的(现有) 下一个指针指向新节点。

3)插入指定位置 (3) Inserting at given position)

Such case can be handles using following steps:

可以使用以下步骤处理这种情况:

  1. Move the current pointer upto the position where node to be inserted.

    将当前指针移到要插入节点的位置。

  2. Store current next pointer address to tmp_node next.

    将当前的下一个指针地址存储到next tmp_node 。

  3. Store tmp_node address to current next.

    将tmp_node地址存储到当前的下一个地址。

    See the below given program...

    请参阅以下给定的程序...

Insertion is done.

插入完成。

在链接列表中插入新节点的C实现 (C implementation of inserting a new node to a link list)

//
//  main.c
//  linkedlist_insert_element_code
//
//  Created by Anshuman Singh on 22/06/19.
//  Copyright © 2019 Anshuman Singh. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct node {int data;
struct node* next;
} node;
void insert_node(node** head, int val, int position);
void insert_node(node** head, int val, int position)
{struct node *curr = *head, *tmp_node = NULL;
int count = 1;
tmp_node = (node*)malloc(sizeof(node));
if (tmp_node == NULL) {printf("Memory allocation is failed:");
return;
}
tmp_node->data = val;
tmp_node->next = NULL;
if (*head == NULL) {// List is empty, assigning head pointer to tmp_node
*head = tmp_node;
return;
}
if (position == 1) {// Inserting node at the beginning of the list
tmp_node->next = *head;
*head = tmp_node;
return;
}
while (curr && count < position - 1) {curr = curr->next;
count++;
}
if (position > (count + 1)) {printf("\n position doesn't exists in the list ");
return;
}
if (count + 1 == position && curr->next == NULL) {// Inseting node at the end of the list
curr->next = tmp_node;
return;
}
// Inserting node in the list at given position
tmp_node->next = curr->next;
curr->next = tmp_node;
}
void print_list(node* head)
{printf("\nList elements:\n");
while (head) {printf("%d ", head->data);
head = head->next;
}
printf("\n");
return;
}
int main()
{int num_nodes, value, index, position;
node* head = NULL;
printf("Enter the no. of nodes to create list: ");
scanf("%d", &num_nodes);
for (index = 1; index <= num_nodes; index++) {printf("Enter node data for position %d in the list:  ", index);
scanf("%d", &value);
insert_node(&head, value, index);
}
print_list(head);
printf("\nInsert the element at 1st position:  ");
scanf("%d", &value);
insert_node(&head, value, 1);
// We have inserted one more element, hence num_nodes get increased by 1
num_nodes += 1;
print_list(head);
printf("\nInsert the element at last position:  ");
scanf("%d", &value);
insert_node(&head, value, num_nodes + 1);
// We have inserted one more element, hence num_nodes will get increased by 1
num_nodes += 1;
print_list(head);
printf("\nInsert the element at any position in the list\n");
printf("Enter the position: ");
scanf("%d", &position);
printf("Enter the element value: ");
scanf("%d", &value);
insert_node(&head, value, position);
// We have inserted one more element, hence num_nodes will get increased by 1
num_nodes += 1;
print_list(head);
return 0;
}

Output

输出量

Enter the no. of nodes to create list: 5
Enter node data for position 1 in the list:  11
Enter node data for position 2 in the list:  22
Enter node data for position 3 in the list:  33
Enter node data for position 4 in the list:  44
Enter node data for position 5 in the list:  55
List elements:
11 22 33 44 55
Insert the element at 1st position:  10
List elements:
10 11 22 33 44 55
Insert the element at last position:  20
List elements:
10 11 22 33 44 55 20
Insert the element at any position in the list
Enter the position: 4
Enter the element value: 40
List elements:
10 11 22 40 33 44 55 20

翻译自: https://www.includehelp.com/data-structure-tutorial/single-linked-list-insertion.aspx

建立单链表 单链表的插入

建立单链表 单链表的插入_单链列表插入相关推荐

  1. 用c语言实现单链表的初始化,建表,查找,求长度,插入,删除等操作,【YTU+2430+C语言习题+链表建立+插入+删除+输(5)...

    的打印.判断链表是否为空.计算链表长度.插入节点.删除节点.删除整个链表.(2) 线性表adt顺序存储实现中的创建.查找.插入和删除等基本操作及相关算法,线性表adt链式存储实现中单链表.循环链表和双 ...

  2. python 单链表节点怎么快速定义_线性表链式存储结构之单链表

    线性表的链式存储结构的特点就是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以在内存中未被占用的任意位置.比起顺序存储结构每个元素只需要存储一个位置就可以了.现在链式存储结构中,除了要存储数 ...

  3. 在单链表写入一组数据代码_链表常见操作和15道常见面试题

    什么是单链表 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer),简单来说链表并不像数组那样 ...

  4. 单链表的创建、删除、反转、插入、排序操作

    单链表的创建.删除.反转.插入.排序操作 文章目录 单链表的创建.删除.反转.插入.排序操作 1.1 链表引言 1.2 单链表节点的数据结构 1.3 创建链表 1.4 打印整个链表 1.5 链表插入数 ...

  5. 数据结构上机-尾、头插法建立单链表-单链表遍历C语言完整代码实现

    点击此处跳转视频链接:数据结构上机-尾.头插法建立单链表-单链表遍历C语言完整代码实现

  6. java怎样建立头插法单链表,链表的创建,头插法创建单链表(带源码+解析)

    头插法创建单,即通过不断地将新创建的结点添加到链表的第一个数据结点之前,作为链表新的首个数据结点的方法,创建单链表. 根据链表是否有头结点,头插法插入结点的位置有所不同: 若链表存在头结点,头插法需将 ...

  7. 数据结构与算法笔记(三)—— 链表(单链表、循环链表、双向链表)

    一.前沿 1.1.为什么需要链表 顺序表的构建需要预先知道数据大小来申请连续的存储空间,而在进行扩充时又需要进行数据的搬迁,所以使用起来并不是很灵活. 链表结构可以充分利用计算机内存空间,实现灵活的内 ...

  8. 【编程2】单链表+单链表反转(LeetCode. 206)

    文章目录 一.链表 二.单链表 1.基本概念 (1)单链表 (2)头指针--必有元素 (3)头结点--非必需元素 (4)尾结点 2.查找操作 3.插入操作 4.删除操作 三.设计思想-- 时间 < ...

  9. 数据结构之链表--单链表

    Hello,大家好!好久不见了,之前一直在忙于一些琐事,最近半个月内会将数据结构的各种数据结构实现出来,一个挺有意思的东西. 这次我将要介绍的是链表.链表有单链表,单向循环链表,双向链表,双向循环链表 ...

最新文章

  1. CUDA入门(三) 初探线程与块
  2. linux centos7 开机自动登录
  3. 日志和告警数据挖掘经验谈
  4. python字典遍历的4种方法
  5. 【哈希和哈希表】Beads
  6. java utf-8 gbk_Java 字符转码之UTF-8转为GBK/GB2312
  7. 4.1-大秦立国-ip演变
  8. flink常见算子的一些操作
  9. java g1 gc ref proc_深入理解垃圾收集器的G1及日志分析
  10. if和switch以及for
  11. 大佬分享:程序员必知的干货
  12. 【Clickhouse】Clickhouse 物化视图 MATERIALIZED VIEW
  13. 教你如何做一次真正有价值的业务数据分析
  14. scala 偏函数与 map/collect
  15. 数值优化(二):信赖域方法与二维空间法
  16. bat命令快捷修改ip地址
  17. 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试...
  18. log日志中输出log所在类,方法和行数
  19. 裁员潮?忍不住偷出阿里P8大舅哥整理的2022年春招内部面试题
  20. 易语言模拟器中控源码 全新手游模拟器通用中控源码, 适用于各种游戏, 源码现成的只需要更换游戏就可以用哦

热门文章

  1. java中变量运算细节 (2)
  2. char 类型与lpcwstr_「lpctstr」char* 与 LPCTSTR 类型的互相转换 - seo实验室
  3. (2021) 25 [持久化] 文件系统实现:FAT和UNIX文件系统
  4. vps如何linux内核4.19,Linux kernel 4.19 RC1 发布,一个相当大的版本
  5. pyecharts 间距_高月双色球20108期:红球首尾间距参考29区段
  6. 倩女幽魂服务器维护时间,9月5日在线维护公告
  7. java方法重载实事例_零基础java入门教程函数重载function实例化格式案例
  8. violinplot如何看懂_一张图告诉你如何看懂个股大趋势
  9. Oracle GoldenGate复制过程
  10. Oracle GoldenGate简介