日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 建立单链表 单链表的插入_单链列表插入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

建立單鏈表 單鏈表的插入

All possible cases:

所有可能的情況:

  • Inserting at beginning

    開始插入

  • Inserting at the ending

    在末尾插入

  • 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.

    在這種情況下,要在當前頭節點之前插入一個新節點,即每次將插入的節點更新為頭節點時。 可以使用以下步驟完成這種插入。

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

    更新新節點(要插入的節點)的下一個指針,使其指向當前節點。

  • 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。 這些步驟是:

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

    將新節點的下一個指針設置為NULL。

  • 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:

    可以使用以下步驟處理這種情況:

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

    將當前指針移到要插入節點的位置。

  • Store current next pointer address to tmp_node next.

    將當前的下一個指針地址存儲到next tmp_node 。

  • 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 listtmp_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 listcurr->next = tmp_node;return;}// Inserting node in the list at given positiontmp_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 1num_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 1num_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 1num_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 55Insert 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

    建立單鏈表 單鏈表的插入

    總結

    以上是生活随笔為你收集整理的建立单链表 单链表的插入_单链列表插入的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。