C语言基础-单链表

📅 2026/7/15 6:37:21 👁️ 阅读次数 📝 编程学习
C语言基础-单链表

#// 动态创建一个链表:动态申请内存+模块化设计#

## 什么是链表?


//1.创建链表(创建一个表头,代表整个链表)

结构体指针-----通过(动态内存申请)转变成-----》结构体变量

struct Node* createList(){ struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); // headNode变成结构体变量 // 变量使用前要初始化 // headNode->data = 1; headNode->next = NULL; return headNode; };

//2.创建节点

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

//3.从头插入节点(从尾部插入、指定位置插入)

// 插入节点(插入函数):参数是:插入那个链表,插入的数据是多少 void insertNodeByHead(struct Node* headNode, int data){ struct Node* newCode = createNode(data); newCode->next = headNode->next; // 为啥顺序反了就不行?? headNode->next = newCode; }


//4.删除节点(指定结点删除)


//5.打印遍历节点(测试用)

// 打印遍历节点 void printList(struct Node* headNode) { struct Node* pMove = headNode->next; while(pMove){ printf("%d \t",pMove->data); pMove = pMove->next; } printf("链表节点遍历完成\n"); }