1. 下面是使用C语言实现单链表的各种操作的示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct LinkedList { struct Node* head; }; void initLinkedList(struct LinkedList* list) { list->head = NULL; } void destroyLinkedList(struct LinkedList* list) { struct Node* current = list->head; while (current != NULL) { struct Node* next = current->next; free(current); current = next; } } void insert(struct LinkedList* list, int position, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (position == 0) { newNode->next = list->head; list->head = newNode; } else { struct Node* current = list->head; for (int i = ; i < position - 1; i++) { current = current->next; } newNode->next = current->next; current->next = newNode; } } void remove(struct LinkedList* list, int position) { if (list->head == NULL) { return; } struct Node* target; if (position == 0) { target = list->head; list->head = target->next; } else { struct Node* current = list->head; for (int i = 0; i < position - 1; i++) { current = current->next; } target = current->next; current->next = target->next; } free(target); } int find(struct LinkedList* list, int data) { struct Node* current = list->head; int position = ; while (current != NULL) { if (current->data == data) { return position; } current = current->next; position++; } return -1; // Not found } int length(struct LinkedList* list) { struct Node* current = list->head; int length = 0; while (current != NULL) { length++; current = current->next; } return length; } int isEmpty(struct LinkedList* list) { return (list->head == NULL); } int get(struct LinkedList* list, int position) { struct Node* current = list->head; for (int i = 0; i < position; i++) { current = current->next; } return current->data; } void reverse(struct LinkedList* list) { struct Node* prev = NULL; struct Node* current = list->head; struct Node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } list->head = prev; } void printList(struct LinkedList* list) { struct Node* current = list->head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { struct LinkedList list; initLinkedList(&list); // 测试代码 insert(&list, 0, 25); insert(&list, 1, 21); insert(&list, 2, 46); insert(&list, 3, 90); insert(&list, 4, 12); insert(&list, 5, 98); insert(&list, 5, 2); printf("Length of the list: %d\n", length(&list)); printList(&list); insert(&list, 2, 43); printf("After inserting 43 at position 2: "); printList(&list); remove(&list, 3); printf("After removing element at position 3: "); printList(&list); reverse(&list); printf("Reversed list: "); printList(&list); destroyLinkedList(&list); return 0; } ``` 2. 按照要求进行单链表的测试: ```c int main() { struct LinkedList list; initLinkedList(&list); // 通过键盘输入数据建立一个单链表,并求表长并输出该线性表 int data; printf("Please enter elements (-1 to stop): "); while (scanf("%d", &data) && data != -1) { insert(&list, length(&list), data); } printf("Length of the list: %d\n", length(&list)); printList(&list); // 依次进行数据的插入、删除和查找,并在插入或删除数据后,再求表长并输出线性表 int position; printf("Insert element at which position: "); scanf("%d", &position); insert(&list, position - 1, 43); printf("After inserting 43 at position %d: ", position); printList(&list); printf("Length of the list: %d\n", length(&list)); printf("Remove element at which position: "); scanf("%d", &position); remove(&list, position - 1); printf("After removing element at position %d: ", position); printList(&list); printf("Length of the list: %d\n", length(&list)); printf("Find element: "); scanf("%d", &data); int foundPosition = find(&list, data); if (foundPosition != -1) { printf("%d found at position %d\n", data, foundPosition + 1); } else { printf("%d not found\n", data); } // 将最终的单链表整个逆置,并输出线性表 reverse(&list); printf("Reversed list: "); printList(&list); destroyLinkedList(&list); return 0; } ``` 这是一个完整的单链表实现,可以按照要求进行测试并进行相应操作。 [2024-03-28 16:24:20 | AI写代码神器 | 4796点数解答]