建立单链表 单链表的插入_单链列表插入

本文详细介绍了如何在单链表中进行插入操作,包括在开始、结束和指定位置插入新节点的步骤,并提供了C语言的实现。内容涵盖三种插入情况的算法,帮助理解链表数据结构的操作。
摘要由CSDN通过智能技术生成

建立单链表 单链表的插入

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

建立单链表 单链表的插入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值