C语言链表的动态创建,删除,修改的代码
时间: 2023-04-06 08:02:48 浏览: 70
我可以回答这个问题。以下是C语言链表的动态创建、删除和修改的代码示例:
动态创建链表:
```
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void addNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
```
动态删除链表:
```
void deleteNode(int data) {
struct Node* current = head;
struct Node* previous = NULL;
while (current != NULL && current->data != data) {
previous = current;
current = current->next;
}
if (current == NULL) {
return;
}
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
```
动态修改链表:
```
void modifyNode(int oldData, int newData) {
struct Node* current = head;
while (current != NULL && current->data != oldData) {
current = current->next;