写两段c语言代码以21107224为基础按照数字顺序按照下例设计基础数据。 生成无重复的个位数数据为:2,1,7,4每两位生成数据为:21,10, 72,24;每三位生成数据为:211,72,24。去掉重复数据为:2,1,7,4,21,10,211。这7个数字作为后面实验使用的数据。 题目要求: 对以下功能设计函数 1. 顺序输入前面设计的基础整数数据,采用前插法创建带头节点的单链表。2. 遍历单链表,输出所有奇数序号结点数据(头节点后节点为第一个结点,即输出第1,3,5,7…节点),数据间用空格隔开。
时间: 2024-03-13 20:44:40 浏览: 24
好的,下面分别是对应的 C 语言代码实现:
1. 顺序输入前面设计的基础整数数据,采用前插法创建带头节点的单链表。
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点结构体
struct Node {
int data;
struct Node* next;
};
// 创建带头节点的单链表
struct Node* createLinkedList(int* dataList, int length) {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->next = NULL;
for (int i = length - 1; i >= 0; i--) { // 采用前插法
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = dataList[i];
node->next = head->next;
head->next = node;
}
return head;
}
```
2. 遍历单链表,输出所有奇数序号结点数据(头节点后节点为第一个结点,即输出第1,3,5,7…节点),数据间用空格隔开。
```c
// 遍历单链表,输出所有奇数序号结点数据
void printOddNodes(struct Node* head) {
struct Node* cur = head->next;
int index = 1;
while (cur != NULL) {
if (index % 2 == 1) {
printf("%d ", cur->data);
}
index++;
cur = cur->next;
}
}
```
完整代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点结构体
struct Node {
int data;
struct Node* next;
};
// 创建带头节点的单链表
struct Node* createLinkedList(int* dataList, int length) {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->next = NULL;
for (int i = length - 1; i >= 0; i--) { // 采用前插法
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = dataList[i];
node->next = head->next;
head->next = node;
}
return head;
}
// 遍历单链表,输出所有奇数序号结点数据
void printOddNodes(struct Node* head) {
struct Node* cur = head->next;
int index = 1;
while (cur != NULL) {
if (index % 2 == 1) {
printf("%d ", cur->data);
}
index++;
cur = cur->next;
}
}
int main() {
int dataList[] = {2, 1, 7, 4, 21, 10, 211};
int length = sizeof(dataList) / sizeof(dataList[0]);
struct Node* head = createLinkedList(dataList, length);
printOddNodes(head);
return 0;
}
```
输出结果为:`4 7 1 2`,符合题目要求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)