数据结构-单链表的头插法和尾插法-

单链表的头插法和尾插法

1. 头插法(Head Insertion)
  • 原理:每次将新节点插入到链表的头部(即原头节点的前面)。
  • 特点 :
    • 插入顺序与最终链表顺序 相反。
    • 时间复杂度:O(1)(无需遍历)。
  • 示例 :
    • 插入顺序:1 → 2 → 3 → 链表最终顺序:3 → 2 → 1。
2. 尾插法(Tail Insertion)
  • 原理:每次将新节点插入到链表的尾部(即原尾节点的后面)。
  • 特点 :
    • 插入顺序与最终链表顺序 一致。
    • 时间复杂度:若维护尾指针则 O(1),否则需遍历到尾部 O(n)。
  • 示例 :
    • 插入顺序:1 → 2 → 3 → 链表最终顺序:1 → 2 → 3。

C语言实现

头插法代码
c 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 头插法函数
void insertHead(Node** head, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = *head;  // 新节点指向原头节点
    *head = newNode;        // 更新头指针
}

// 打印链表
void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d → ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    insertHead(&head, 1);
    insertHead(&head, 2);
    insertHead(&head, 3);
    printList(head);  // 输出:3 → 2 → 1 → NULL
    return 0;
}
尾插法代码
c 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 尾插法函数(需维护尾指针)
void insertTail(Node** head, Node** tail, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;

    if (*head == NULL) {  // 空链表时
        *head = newNode;
        *tail = newNode;
    } else {
        (*tail)->next = newNode;  // 原尾节点指向新节点
        *tail = newNode;          // 更新尾指针
    }
}

// 打印链表
void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d → ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    Node* tail = NULL;
    insertTail(&head, &tail, 1);
    insertTail(&head, &tail, 2);
    insertTail(&head, &tail, 3);
    printList(head);  // 输出:1 → 2 → 3 → NULL
    return 0;
}

关键点总结

  1. 头插法:直接操作头指针,无需遍历。
  2. 尾插法:需维护尾指针(或每次遍历到尾部),效率更高。
  3. 注意事项 :
    • 插入前需分配内存(malloc),删除时需释放内存(free)。
    • 处理空链表的边界条件(如首次插入时头/尾指针的初始化)。

总结

看上去的区别不太大

只是输出是相反的。和常识一致,是尾插法,先插进去的,位子靠前。

头插法;先处理s 后处理r

尾插法 先处理 r 后处理是;

相关推荐
汉克老师3 小时前
CSP-S 2026 初赛试题解析(第一部分:选择题(8-15))精讲
c++·csp-s·小学生·学c++编程
_wyt0013 小时前
洛谷 P10112 [GESP202312 八级] 奖品分配 题解
c++·数学·gesp·八级
朝朝辞暮i4 小时前
C++ 第 33 课:事件循环——spin() 到底在干什么
开发语言·c++·算法
m4Rk_4 小时前
【论文阅读】Agent 记忆机制(96):M3-Agent——让 Agent 从视听经历中持续形成情景与语义长期记忆
论文阅读·人工智能·学习·开源·github
殷色玫瑰4 小时前
AVL树:从平衡因子到四种旋转的完整实现
c语言·数据结构·c++·算法·visualstudio
进化中的刀盾兽5 小时前
从零实现 C++ AI 大模型接入 SDK(九):LLMManager 统一模型管理与请求路由
c++
会周易的程序员5 小时前
STVM OSAL 层架构设计文档
c++·物联网·嵌入式·虚拟机·iot·软plc·iec61131
.YM.Z5 小时前
C++——【红黑树】原理详解:定义、性质、插入变色与旋转实现
开发语言·c++
y1su5 小时前
Leetcode 二分模板
java·数据结构·算法·leetcode·排序算法
bksczm5 小时前
数据结构的大纲概括
数据结构