练习 #1
Coddy 链表:数据结构系列第 5 课 课程的第 10 课,共 14 课。
挑战
简单编写一个函数 reverseList,接收一个整数数组 arr,并返回反转后的数组。
使用链表解决这个问题!将每个值添加到列表的开头,然后依次读取这些值。
你必须使用 LinkedList 类(由 linkedlist.<ext> 和 node.<ext> 提供),不要使用语言内置功能,例如数组自带的反转方法、切片或标准库中的列表操作来计算结果。
亲手试试
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"
int main() {
char line[8192];
if (!fgets(line, sizeof(line), stdin)) line[0] = '\0';
int arr[4096];
int len = 0;
char* tok = strtok(line, " \t\r\n");
while (tok) { arr[len++] = atoi(tok); tok = strtok(NULL, " \t\r\n"); }
int rs = 0;
int* r = reverseList(arr, len, &rs);
for (int idx = 0; idx < rs; idx++) {
if (idx > 0) printf(" ");
printf("%d", r[idx]);
}
printf("\n");
return 0;
}
链表:数据结构系列第 5 课中的所有课
自己练习: C 在线编译器