请为我详细讲解c语言中的链表,要求结合代码,包括链表的增删改查
时间: 2024-03-15 09:47:16 浏览: 91
好的,下面是C语言中链表的详细讲解和代码示例。
链表是一种常见的数据结构,它使用指针将一组节点连接在一起,每个节点包含一个数据元素和一个指向下一节点的指针。链表的优点是可以动态地添加或删除元素,而无需移动其他元素。
我们先定义一个链表节点的结构体,包含一个整型数据元素和一个指向下一节点的指针。
```c
struct ListNode {
int val;
struct ListNode *next;
};
```
接下来,我们可以定义一些常见的链表操作函数,如下:
1. 创建一个新节点
```c
struct ListNode *createNode(int val) {
struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
```
2. 在链表尾部插入一个节点
```c
void insertAtTail(struct ListNode **head, int val) {
struct ListNode *newNode = createNode(val);
if (*head == NULL) {
*head = newNode;
return;
}
struct ListNode *tail = *head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newNode;
}
```
3. 在链表头部插入一个节点
```c
void insertAtHead(struct ListNode **head, int val) {
struct ListNode *newNode = createNode(val);
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
*head = newNode;
}
```
4. 在指定位置插入一个节点
```c
void insertAtIndex(struct ListNode **head, int val, int index) {
if (index < 0) {
return;
}
if (index == 0) {
insertAtHead(head, val);
return;
}
struct ListNode *newNode = createNode(val);
struct ListNode *prev = *head;
for (int i = 0; i < index - 1 && prev != NULL; i++) {
prev = prev->next;
}
if (prev == NULL) {
return;
}
newNode->next = prev->next;
prev->next = newNode;
}
```
5. 删除链表中的一个节点
```c
void deleteNode(struct ListNode **head, int val) {
if (*head == NULL) {
return;
}
if ((*head)->val == val) {
struct ListNode *temp = *head;
*head = (*head)->next;
free(temp);
return;
}
struct ListNode *prev = *head;
while (prev->next != NULL && prev->next->val != val) {
prev = prev->next;
}
if (prev->next == NULL) {
return;
}
struct ListNode *temp = prev->next;
prev->next = temp->next;
free(temp);
}
```
6. 查找链表中的一个节点
```c
struct ListNode *searchNode(struct ListNode *head, int val) {
while (head != NULL) {
if (head->val == val) {
return head;
}
head = head->next;
}
return NULL;
}
```
下面是一个完整的示例程序,演示了如何使用上述函数创建、插入、删除和查找链表节点。
```c
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *createNode(int val) {
struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
void insertAtTail(struct ListNode **head, int val) {
struct ListNode *newNode = createNode(val);
if (*head == NULL) {
*head = newNode;
return;
}
struct ListNode *tail = *head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newNode;
}
void insertAtHead(struct ListNode **head, int val) {
struct ListNode *newNode = createNode(val);
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
*head = newNode;
}
void insertAtIndex(struct ListNode **head, int val, int index) {
if (index < 0) {
return;
}
if (index == 0) {
insertAtHead(head, val);
return;
}
struct ListNode *newNode = createNode(val);
struct ListNode *prev = *head;
for (int i = 0; i < index - 1 && prev != NULL; i++) {
prev = prev->next;
}
if (prev == NULL) {
return;
}
newNode->next = prev->next;
prev->next = newNode;
}
void deleteNode(struct ListNode **head, int val) {
if (*head == NULL) {
return;
}
if ((*head)->val == val) {
struct ListNode *temp = *head;
*head = (*head)->next;
free(temp);
return;
}
struct ListNode *prev = *head;
while (prev->next != NULL && prev->next->val != val) {
prev = prev->next;
}
if (prev->next == NULL) {
return;
}
struct ListNode *temp = prev->next;
prev->next = temp->next;
free(temp);
}
struct ListNode *searchNode(struct ListNode *head, int val) {
while (head != NULL) {
if (head->val == val) {
return head;
}
head = head->next;
}
return NULL;
}
void printList(struct ListNode *head) {
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
printf("\n");
}
int main() {
struct ListNode *head = NULL;
insertAtTail(&head, 1);
insertAtTail(&head, 2);
insertAtTail(&head, 3);
insertAtHead(&head, 0);
insertAtIndex(&head, 4, 4);
printList(head);
deleteNode(&head, 3);
printList(head);
struct ListNode *node = searchNode(head, 2);
if (node != NULL) {
printf("Found node with value %d\n", node->val);
} else {
printf("Node not found\n");
}
return 0;
}
```
希望这篇文章对你有所帮助!
阅读全文