以下是使用C语言创建单链表,并实现遍历和查找节点的代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory error "); return NULL; } newNode->data = data; newNode->next = NULL; return newNode; } Node* createList(int data[], int length) { Node *head, *temp; head = temp = createNode(data[0]); for (int i = 1; i < length; i++) { temp->next = createNode(data[i]); temp = temp->next; } return head; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf(" "); } Node* searchNode(Node* head, int x) { Node* temp = head; while (temp != NULL) { if (temp->data == x) return temp; temp = temp->next; } return NULL; } int main() { int data[] = {1, 2, 3, 4, 5}; Node* head = createList(data, 5); printList(head); Node* result = searchNode(head, 3); if (result != NULL) printf("Found node with data %d ", result->data); else printf("No node found with the given data "); return 0; } ``` 在这段代码中,我们首先定义了一个结构体`Node`来表示链表的节点。然后,我们实现了`createNode`函数来创建一个新的节点,`createList`函数来创建一个链表,`printList`函数来打印链表的所有元素,以及`searchNode`函数来查找具有特定数据的节点。 [2024-06-21 21:31:40 | AI写代码神器 | 528点数解答]