题目:下面有结构node的定义,指针head中存放着node节点组成 的单链表的首节点地址,函数odd(head)获取head指向链表 中的序号(注意不是node结构的data字段,且序号从0开始编号) 为奇数的节点,并将这些节点按原有序组成新链表返回。 要求新链表中的节点采用动态内存分配的方式构建, 从而不破坏原链表。如:head指向的链表为:1->2->3->4->5->6, 则odd(head)得到的链表为:2->4->6。请完成函数odd的编写。
时间: 2024-04-13 15:29:47 浏览: 98
你可以使用以下代码来实现函数odd:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义节点结构
struct node {
int data;
struct node* next;
};
// 函数odd
struct node* odd(struct node* head) {
struct node* new_head = NULL; // 新链表的头节点
struct node* new_tail = NULL; // 新链表的尾节点
struct node* curr = head; // 当前节点
while (curr != NULL) {
if ((curr->data) % 2 == 0) { // 如果节点序号为偶数
if (new_head == NULL) { // 如果新链表为空,设置新链表的头节点和尾节点为当前节点
new_head = curr;
new_tail = curr;
} else { // 如果新链表不为空,将当前节点添加到新链表的尾部
new_tail->next = curr;
new_tail = new_tail->next;
}
}
curr = curr->next; // 遍历下一个节点
}
if (new_tail != NULL) {
new_tail->next = NULL; // 将新链表的尾节点的next指针置为NULL
}
return new_head;
}
// 创建链表
struct node* createLinkedList(int nums[], int n) {
struct node* head = NULL;
struct node* tail = NULL;
for (int i = 0; i < n; i++) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = nums[i];
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// 打印链表
void printLinkedList(struct node* head) {
struct node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
// 释放链表内存
void freeLinkedList(struct node* head) {
struct node* curr = head;
struct node* next = NULL;
while (curr != NULL) {
next = curr->next;
free(curr);
curr = next;
}
}
int main() {
int nums[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(nums) / sizeof(nums[0]);
struct node* head = createLinkedList(nums, n);
printf("原链表:");
printLinkedList(head);
struct node* new_head = odd(head);
printf("新链表:");
printLinkedList(new_head);
freeLinkedList(new_head);
return 0;
}
```
在上面给出的示例代码中,首先定义了节点结构`struct node`,包含数据域`data`和指向下一个节点的指针`next`。然后定义了函数`odd`,用于获取链表中序号为奇数的节点,并将这些节点按原有序组成新链表返回。
在函数`createLinkedList`中,根据传入的数组和长度创建了一个链表,并返回链表的头节点。
在函数`printLinkedList`中,遍历链表并打印每个节点的数据。
在函数`freeLinkedList`中,释放链表的内存空间。
在`main`函数中,首先创建了一个链表,并打印原链表。然后调用`odd`函数获取新链表,再打印新链表。最后释放新链表的内存空间。
输出结果为:
```
原链表:1 2 3 4 5 6
新链表:2 4 6
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)