会员中心
网站首页 > 编程助手 > 特黄一级黄色高清大片 数据结构入门必看:C语言单链表基本操作详解

特黄一级黄色高清大片 数据结构入门必看:C语言单链表基本操作详解

在线计算网 · 发布于 2025-02-01 22:59:02 · 已经有10人使用

特黄一级黄色高清大片 数据结构入门必看:C语言单链表基本操作详解

引言

在编程的世界里,数据结构是构建复杂系统的基石。而单链表作为一种基础且重要的数据结构,掌握其基本操作对于提升编程技能至关重要。今天,我们就来深入探讨C语言中单链表的基本操作。

什么是单链表

单链表是一种线性数据结构,由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。其结构简单,操作灵活,广泛应用于各种场景。

单链表的结构

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

单链表的基本操作

1. 创建节点

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

2. 插入节点

头插法
void insertAtHead(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    newNode->next = *head;
    *head = newNode;
}
尾插法
void insertAtTail(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    struct Node* temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}

3. 删除节点

删除头节点
void deleteAtHead(struct Node** head) {
    if (*head == NULL) return;
    struct Node* temp = *head;
    *head = (*head)->next;
    free(temp);
}
删除尾节点
void deleteAtTail(struct Node** head) {
    if (*head == NULL) return;
    if ((*head)->next == NULL) {
        free(*head);
        *head = NULL;
        return;
    }
    struct Node* temp = *head;
    while (temp->next->next != NULL) {
        temp = temp->next;
    }
    free(temp->next);
    temp->next = NULL;
}

4. 查找节点

struct Node* search(struct Node* head, int key) {
    struct Node* temp = head;
    while (temp != NULL) {
        if (temp->data == key) return temp;
        temp = temp->next;
    }
    return NULL;
}

完整示例

#include <stdio.h>
#include <stdlib.h>

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

struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; }

void insertAtHead(struct Node** head, int data) { struct Node* newNode = createNode(data); newNode->next = *head; *head = newNode; }

void insertAtTail(struct Node** head, int data) { struct Node* newNode = createNode(data); if (*head == NULL) { head = newNode; return; } struct Node temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; }

void deleteAtHead(struct Node** head) { if (head == NULL) return; struct Node temp = *head; *head = (*head)->next; free(temp); }

void deleteAtTail(struct Node** head) { if (*head == NULL) return; if ((*head)->next == NULL) { free(*head); head = NULL; return; } struct Node temp = *head; while (temp->next->next != NULL) { temp = temp->next; } free(temp->next); temp->next = NULL; }

struct Node* search(struct Node* head, int key) { struct Node* temp = head; while (temp != NULL) { if (temp->data == key) return temp; temp = temp->next; } return NULL; }

int main() { struct Node* head = NULL; insertAtHead(&head, 10); insertAtTail(&head, 20); insertAtTail(&head, 30); deleteAtHead(&head); deleteAtTail(&head); struct Node* found = search(head, 20); if (found != NULL) { printf("Found: %d\n", found->data); } else { printf("Not Found\n"); } return 0; }

总结

通过本文的学习,相信你已经掌握了C语言中单链表的基本操作。无论是创建、插入、删除还是查找节点,这些操作都是构建复杂数据结构的基础。希望你在实际编程中能够灵活运用,不断提升自己的编程能力。

参考资料

  • 《数据结构与算法分析》

  • C语言标准库文档

微信扫码
X

更快、更全、更智能
微信扫码使用在线科学计算器

Copyright © 2022 www.tampocvet.com All Rights Reserved.
在线计算网版权所有严禁任何形式复制 粤ICP备20010675号 本网站由智启CMS强力驱动网站地图