帮我写个代码: 用C语言编程实现建立链表,并完成以下操作: 初始化链表。从键盘接受元素个数n以及n个整型数;通过键盘读取元素实现链表插入操作;通过键盘读取元素实现链表删除操作;通过键盘读取元素实现链表修改操作。
时间: 2023-08-04 14:11:42 浏览: 66
好的,以下是代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void initList(Node **head) {
*head = NULL;
}
void insert(Node **head, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node *curr = *head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newNode;
}
}
void delete(Node **head, int data) {
if (*head == NULL) {
return;
}
Node *curr = *head;
if (curr->data == data) {
*head = curr->next;
free(curr);
return;
}
while (curr->next != NULL) {
if (curr->next->data == data) {
Node *temp = curr->next;
curr->next = temp->next;
free(temp);
return;
}
curr = curr->next;
}
}
void modify(Node **head, int oldData, int newData) {
if (*head == NULL) {
return;
}
Node *curr = *head;
while (curr != NULL) {
if (curr->data == oldData) {
curr->data = newData;
return;
}
curr = curr->next;
}
}
void printList(Node *head) {
Node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
Node *head;
int n, i, data;
initList(&head);
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &data);
insert(&head, data);
}
printf("Initial list: ");
printList(head);
printf("Enter the element to delete: ");
scanf("%d", &data);
delete(&head, data);
printf("List after deletion: ");
printList(head);
printf("Enter the element to modify: ");
scanf("%d", &data);
int newData;
printf("Enter the new data: ");
scanf("%d", &newData);
modify(&head, data, newData);
printf("List after modification: ");
printList(head);
return 0;
}
```
在本代码中,我们定义了一个结构体 `Node` 来表示链表中的每个节点。我们使用 `initList` 函数来初始化链表,使用 `insert` 函数来插入元素,使用 `delete` 函数来删除元素,使用 `modify` 函数来修改元素,使用 `printList` 函数来打印链表。
在主函数中,我们先初始化链表,然后从键盘读取元素个数和元素,调用 `insert` 函数来插入元素,打印初始链表,再从键盘读取需要删除的元素,调用 `delete` 函数来删除元素,打印删除后的链表,然后从键盘读取需要修改的元素和新数据,调用 `modify` 函数来修改元素,打印修改后的链表。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](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)