7-1 单链表的创建,遍历与销毁
时间: 2024-05-22 22:15:09 浏览: 12
好的,这是一个关于单链表的问题,我可以为您解答。
单链表的创建可以通过动态内存分配来实现,具体步骤如下:
1. 定义一个结构体表示链表的每个节点,包括数据和指向下一个节点的指针。
2. 通过 malloc 函数分配一个节点的内存,返回指向该节点的指针。
3. 将数据存储在该节点中,并将节点的指针指向下一个节点的地址(初始状态设为 NULL)。
4. 重复以上步骤,不断创建新节点并将当前节点的指针指向下一个节点,直到链表的最后一个节点。
遍历单链表可以通过遍历指针不断获取节点数据的方式来实现。具体步骤如下:
1. 定义一个指向链表头节点的指针。
2. 使用 while 循环遍历链表,每次获取当前节点的数据打印出来,然后将指针指向下一个节点。
3. 当指针为 NULL 时,表示已经到了链表的结尾,遍历结束。
销毁单链表可以通过释放动态分配的内存来实现,具体步骤如下:
1. 定义一个指向链表头节点的指针。
2. 使用 while 循环遍历链表,每次记录当前节点的指针并释放节点内存,然后将指针指向下一个节点。
3. 当指针为 NULL 时,表示已经到了链表的结尾,销毁结束。
希望上述解答能帮到您,如果有其他问题欢迎随时询问。
相关问题
7-1 单链表的创建,遍历与销毁 代码
单链表的创建、遍历和销毁的代码如下:
//定义链表节点结构体
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
//创建链表
ListNode* createList(int* nums, int numsSize) {
ListNode* head = NULL;
ListNode* tail = NULL;
for (int i = 0; i < numsSize; i++) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->val = nums[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
//遍历链表
void traverseList(ListNode* head) {
ListNode* p = head;
while (p != NULL) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
//销毁链表
void destroyList(ListNode* head) {
ListNode* p = head;
while (p != NULL) {
ListNode* q = p->next;
free(p);
p = q;
}
}
注意:以上代码仅供参考,具体实现可能需要根据具体情况进行修改。
单链表的创建销毁增删改查与调用代码
以下是单链表的创建、销毁、增加、删除和查找的示例代码,以及对这些函数的调用示例:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点结构定义
typedef struct Node {
int data; // 数据域
struct Node *next; // 指向下一个节点的指针
} Node;
// 创建一个新节点
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("内存分配失败!\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 销毁整个链表
void destroyList(Node *head) {
Node *current = head;
while (current != NULL) {
Node *temp = current;
current = current->next;
free(temp);
}
}
// 在链表末尾插入一个节点
void insertAtEnd(Node **head, int data) {
Node *newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 删除指定值的节点
void deleteNode(Node **head, int data) {
if (*head == NULL) {
printf("链表为空!\n");
return;
}
Node *current = *head;
Node *prev = NULL;
// 如果头节点就是要删除的节点
if (current->data == data) {
*head = current->next;
free(current);
return;
}
// 遍历链表查找要删除的节点
while (current != NULL && current->data != data) {
prev = current;
current = current->next;
}
// 找到要删除的节点,进行删除操作
if (current != NULL) {
prev->next = current->next;
free(current);
} else {
printf("未找到要删除的节点!\n");
}
}
// 修改指定值的节点
void modifyNode(Node *head, int data, int newData) {
Node *current = head;
while (current != NULL) {
if (current->data == data) {
current->data = newData;
return;
}
current = current->next;
}
printf("未找到要修改的节点!\n");
}
// 查找指定值的节点
Node *searchNode(Node *head, int data) {
Node *current = head;
while (current != NULL) {
if (current->data == data) {
return current;
}
current = current->next;
}
return NULL;
}
// 打印链表中的所有节点
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node *head = NULL; // 头节点指针
// 插入节点
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
// 打印链表
printf("链表内容:");
printList(head);
// 查找节点
Node *searchResult = searchNode(head, 2);
if (searchResult != NULL) {
printf("找到节点:%d\n", searchResult->data);
} else {
printf("未找到节点!\n");
}
// 修改节点
modifyNode(head, 2, 4);
// 打印链表
printf("修改节点后的链表内容:");
printList(head);
// 删除节点
deleteNode(&head, 2);
// 打印链表
printf("删除节点后的链表内容:");
printList(head);
// 销毁链表
destroyList(head);
return 0;
}
```
这段代码包含了创建、销毁、增加、删除和查找单链表的函数,以及对这些函数的调用示例。通过调用这些函数,您可以创建一个单链表,并进行相应的操作。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)