gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
阻塞的英文
- User
阻塞的英文
- Assistant
Blockade
- User
用C语言写一个链表数据结构
- Assistant
以下是一个基础的单向链表数据结构的C语言代码实现:
c#include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct Node { int data; // 节点中存储的数据 struct Node *next; // 指向下一个节点的指针 }; // 创建并返回一个初始节点指针 struct Node* createList() { struct Node *head = malloc(sizeof(struct Node)); head->data = 0; // 初始节点的数据可随意 head->next = NULL; // 初始节点的指针置空 return head; } // 插入节点 void insertNode(struct Node *head, int data) { struct Node *newNode = malloc(sizeof(struct Node)); newNode->data = data; newNode->next = head->next; head->next = newNode; } // 打印链表内容 void printList(struct Node *head) { struct Node *p = head->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } } // 测试函数 int main() { struct Node *head = createList(); insertNode(head, 3); insertNode(head, 5); insertNode(head, 7); printList(head); return 0; }