写一段c语言单链表节点释放程序
时间: 2024-05-10 18:21:39 浏览: 10
以下是一段C语言单链表节点释放程序的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
// 定义单链表结构体
typedef struct ListNode {
int data;
struct ListNode* next;
} ListNode;
// 定义节点释放函数
void freeListNode(ListNode* node) {
if (node == NULL) {
return;
}
free(node);
}
int main() {
// 创建一个节点
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = 1;
node->next = NULL;
// 释放节点
freeListNode(node);
return 0;
}
```
在上面的代码中,我们先定义了一个单链表结构体 `ListNode`,其中包含了一个整型的 `data` 和一个指向下一个节点的指针 `next`。然后,我们定义了一个 `freeListNode` 函数,用于释放单链表节点的内存。在 `main` 函数中,我们创建了一个节点,并调用 `freeListNode` 函数来释放该节点的内存。
需要注意的是,在释放节点的内存时,我们需要判断该节点是否为 `NULL`,以避免出现空指针异常。
相关问题
写一段c语言单链表节点逐个释放程序
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void freeList(struct Node* head) {
struct Node* tmp;
while (head != NULL) {
tmp = head;
head = head->next;
free(tmp);
}
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// Allocate 3 nodes in the heap
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
/* Three blocks have been allocated dynamically.
We have pointers to these three blocks as head,
second and third
head second third
| | |
| | |
+---+------+ +----+-----+ +----+-----+
| # | data | | # | data | | # | data |
+---+------+ +----+-----+ +----+-----+
*/
head->data = 1; // assign data in first node
head->next = second; // Link first node with second
second->data = 2; // assign data to second node
second->next = third;
third->data = 3; // assign data to third node
third->next = NULL;
freeList(head); // Release memory allocated by nodes
return 0;
}
用C语言写一段程序用于单链表的插入
下面是用C语言写的单链表插入程序:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_list();
void insert_node(struct node *head, int pos, int value);
void print_list(struct node *head);
int main() {
struct node *head = create_list(); // 创建单链表
print_list(head); // 输出单链表的初始状态
insert_node(head, 2, 100); // 在第2个位置插入值为100的节点
print_list(head); // 输出插入节点后的单链表
return 0;
}
// 创建单链表
struct node *create_list() {
struct node *head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
struct node *tail = head;
int n, value;
printf("请输入单链表的长度:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个节点的值:", i+1);
scanf("%d", &value);
struct node *p = (struct node*)malloc(sizeof(struct node));
p->data = value;
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
// 在单链表的pos位置插入值为value的节点
void insert_node(struct node *head, int pos, int value) {
int i = 0;
struct node *p = head;
while (p != NULL && i < pos-1) {
p = p->next;
i++;
}
if (p == NULL || i > pos-1) {
printf("插入位置不合法!\n");
return;
}
struct node *q = (struct node*)malloc(sizeof(struct node));
q->data = value;
q->next = p->next;
p->next = q;
}
// 输出单链表
void print_list(struct node *head) {
struct node *p = head->next;
printf("单链表的元素为:");
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
```
程序的运行结果如下:
```
请输入单链表的长度:3
请输入第1个节点的值:1
请输入第2个节点的值:2
请输入第3个节点的值:3
单链表的元素为:1 2 3
单链表的元素为:1 2 100 3
```
在上面的程序中,我们定义了一个结构体`node`,用于表示单链表的节点。每个节点包含两个成员变量:`data`表示节点的值,`next`表示指向下一个节点的指针。然后,我们定义了三个函数:
- `create_list()`:用于创建单链表,先创建一个头结点,然后输入单链表的长度和每个节点的值,通过尾插法构建单链表。
- `insert_node(struct node *head, int pos, int value)`:用于在单链表的第`pos`个位置插入值为`value`的节点。首先遍历单链表,找到要插入位置的前一个节点,然后创建一个新节点,将它插入到链表中。
- `print_list(struct node *head)`:用于输出单链表的元素。
在`main`函数中,我们首先创建一个单链表,然后输出它的初始状态。接着,我们在第2个位置插入值为100的节点,再输出插入节点后的单链表。