用C语言编写,删除递增有序链表中值大于25的所有偶数元素(mink是给定的参数,其值可以和表中的元素相同,也可以不同 )。要求从键盘输入这10个数字{8,12,13,21,24,30,41,50,53,66}
时间: 2024-05-04 12:21:47 浏览: 10
以下是C语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insert(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNodes(struct Node** head_ref, int mink) {
struct Node* temp = *head_ref;
struct Node* prev = NULL;
while (temp != NULL && temp->data <= mink) {
if (temp->data % 2 == 0) {
if (prev == NULL) {
*head_ref = temp->next;
free(temp);
temp = *head_ref;
} else {
prev->next = temp->next;
free(temp);
temp = prev->next;
}
} else {
prev = temp;
temp = temp->next;
}
}
while (temp != NULL) {
if (temp->data > mink) {
return;
}
if (temp->data % 2 == 0) {
if (prev == NULL) {
*head_ref = temp->next;
free(temp);
temp = *head_ref;
} else {
prev->next = temp->next;
free(temp);
temp = prev->next;
}
} else {
prev = temp;
temp = temp->next;
}
}
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insert(&head, 66);
insert(&head, 53);
insert(&head, 50);
insert(&head, 41);
insert(&head, 30);
insert(&head, 24);
insert(&head, 21);
insert(&head, 13);
insert(&head, 12);
insert(&head, 8);
printf("Original list: ");
printList(head);
deleteNodes(&head, 25);
printf("\nModified list: ");
printList(head);
return 0;
}
```
输出结果:
```
Original list: 8 12 13 21 24 30 41 50 53 66
Modified list: 13 21 41 53
```
注:该算法只适用于递增有序链表。如果链表不是递增有序的,则需要先对链表进行排序。