c语言 头插法 带头结点的单链表 不使用基本操作函数
时间: 2024-05-12 14:19:51 浏览: 100
以下是使用头插法实现带头结点的单链表的示例代码,不使用基本操作函数:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义节点结构体
typedef struct Node {
int data;
struct Node *next;
} Node;
// 创建节点
Node *create_node(int data) {
Node *node = (Node *) malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
// 头插法插入节点
void insert_node(Node *head, int data) {
Node *node = create_node(data);
node->next = head->next;
head->next = node;
}
// 遍历链表
void traverse_list(Node *head) {
Node *node = head->next;
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
// 创建头结点
Node *head = create_node(0);
// 插入节点
insert_node(head, 1);
insert_node(head, 2);
insert_node(head, 3);
// 遍历链表
traverse_list(head);
return 0;
}
```
输出结果为:
```
3 2 1
```
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)