zhange 发表于 2024-8-7 16:01:31

第一次接触链表 裂开了

本来以为学会c了没想到还差得远呀{:5_271:}


补下知识吧!!!


想学习rt—thread    买了一本书开始直接讲原理链表汇编语言都上了咋办呀    都不会


各位学过的大佬 能给点指点意见不

zhange 发表于 2024-8-7 16:02:01

#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));
    if (newNode == NULL) {
      printf("内存分配失败!\n");
      exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

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

// 插入节点到链表头部
void insertAtHead(struct Node** head_ref, int new_data) {
    struct Node* newNode = createNode(new_data);
    newNode->next = (*head_ref);
    (*head_ref) = newNode;
}

// 插入节点到链表尾部
void insertAtTail(struct Node** head_ref, int new_data) {
    struct Node* newNode = createNode(new_data);
    struct Node* last = *head_ref;

    while (last->next!= NULL) {
      last = last->next;
    }

    last->next = newNode;
    newNode->next = NULL;
}

// 插入节点到指定位置
void insertAtPosition(struct Node** head_ref, int new_data, int position) {
    struct Node* newNode = createNode(new_data);
    struct Node* current = *head_ref;
    struct Node* prev = NULL;

    int i = 1;
    while (i < position - 1 && current!= NULL) {
      prev = current;
      current = current->next;
      i++;
    }

    if (current == NULL) {
      printf("指定位置超出链表范围!\n");
      return;
    }

    newNode->next = current;
    prev->next = newNode;
}

// 删除链表节点
void deleteNode(struct Node* node) {
    if (node == NULL) {
      printf("要删除的节点为空!\n");
      return;
    }

    struct Node* temp = node->next;
    free(node);
    node = temp;
}

// 释放链表内存
void freeList(struct Node* head) {
    while (head!= NULL) {
      struct Node* temp = head;
      head = head->next;
      free(temp);
    }
}

int main() {
    // 创建链表
    struct Node* head = NULL;

    // 插入节点到链表头部
    insertAtHead(&head, 7);
    insertAtHead(&head, 13);
    insertAtHead(&head, 18);

    // 打印链表
    printf("创建链表后:\n");
    printList(head);

    // 插入节点到链表尾部
    insertAtTail(&head, 56);
    insertAtTail(&head, 31);

    // 打印链表
    printf("插入节点到链表尾部后:\n");
    printList(head);

    // 插入节点到指定位置
    insertAtPosition(&head, 22, 3);

    // 打印链表
    printf("插入节点到指定位置后:\n");
    printList(head);

    // 删除链表节点
    deleteNode(head->next->next);

    // 打印链表
    printf("删除链表节点后:\n");
    printList(head);

    // 释放链表内存
    freeList(head);

    return 0;
}

zhange 发表于 2024-8-7 16:07:07

malloc是 C 和 C++ 语言中的标准库函数,用于动态分配内存。


这个函数学过以后用单片机就没用到过   都忘了{:4_168:}{:4_168:}

DebugLab 发表于 2024-8-7 16:57:31

链表可以看看浙大的慕课

_奶咖君_ 发表于 2024-8-7 17:24:18

C语言学完了 找一本计算机系的教材 数据结构 最好找C语言版的 看看

嵌入式之路 发表于 2024-8-7 22:26:54

哪里给你讲链表了。 只不过对象管理器的管理对应的对象的时候是按照链表排列的。 这不是很简单吗? 比如一个Timer对象,在链表中管理是按照counter的值进行排序的

xxxevery 发表于 2024-8-7 23:31:31

多到B站去看数据结构的视频吧

zhange 发表于 2024-8-8 10:56:15

_奶咖君_ 发表于 2024-8-7 17:24
C语言学完了 找一本计算机系的教材 数据结构 最好找C语言版的 看看

多谢指点得买本书看看了




哪个好一点

TechQI 发表于 2024-8-8 12:18:24

看过《人体蜈蚣》这部电影不?这就是链表最直观的体现了,后一个的头连接前一个的屁股

未元星系 发表于 2024-8-8 14:22:42

我最近也在学数据结构,真的有种知识躲着我脑子的感觉{:4_177:}
页: [1] 2
查看完整版本: 第一次接触链表 裂开了