-
Lesson 1 - 最简单的C程序
-
Lesson 2 - 打印输出
-
Lesson 3 - 循环打印
-
Lesson 4 - 判断奇偶
-
Lesson 5 - 从1加到100求和
-
Lesson 6 - 乘法表
-
Lesson 7 - 求100以内的最大素数
-
Lesson 8 - 1到100有多少个9
-
Lesson 9 - 整型转字符串
-
Lesson 10 - 约瑟夫环
-
Lesson 11 - 求两个坐标点之间的距离
-
Lesson 12 - 判断机器存储是否小尾端
-
Lesson 13 - 对不起,你的车今天限行
-
Lesson 14 - 判断地图上某点是否有出路
-
Lesson 15 - 统计一个数二进制表示中1的个数
-
Lesson 16 - 字符串拷贝
-
Lesson 17 - 统计单词个数
-
Lesson 18 - 实现 printf
-
Lesson 19 - 命令解释器
-
Lesson 20 - 预处理器实现
-
Lesson 21 - 词法分析器实现
-
Lesson 22 - 猜数游戏
-
Lesson 23 - 五子棋
-
Lesson 24 - 超链接分析器
-
Lesson 25 - cp命令实现
-
Lesson 26 - ELF文件头分析器实现
-
Lesson 27 - 简单流处理器实现和正则表达式
-
Lesson 28 - 数学计算器实现
-
Lesson 29 - 数学计算器实现more命令实现
-
Lesson 30 - sort命令实现
-
Lesson 31 - ls -l命令实现
-
Lesson 32 - Bash项目
-
Lesson 33 - 动态数组实现
-
Lesson 34 - 约瑟夫环问题
-
Lesson 35 - 表达式求值问题
-
Lesson 36 - 广度优先解决迷宫问题
-
Lesson 37 - 词频统计器
-
Lesson 38 - 堆排序问题
-
Lesson 39 - 构造符号表
-
Lesson 40 - MyDictionary项目
-
Lesson 41 - BSearch 实现
-
Lesson 42 - QSort 实现
-
Lesson 43 - 深度优先解决迷宫问题
-
Lesson 44 - KMP 算法实现
-
Lesson 45 - 最长公共子序列(LCS)问题
-
Lesson 46 - Dijkstra 算法
-
Lesson 47 - Huffman Coding 算法
-
Lesson 48 - 地图导航项目
Lesson 34 - 约瑟夫环问题
课程任务
通过循环链表 circular list 这种特殊的数据结构(简称 clist),实现 Lesson 10 要求的约瑟夫环(Josephus Ring)问题。
重要知识点
- 循环链表的生成,插入,删除等操作
- cursor 游标的使用
数据结构和接口设计
struct node
{
void *data;
struct node * next;
};
typedef struct node * link;
link make_node(void *data);
int *make_data(int data);
void print_int_data(void *data);
link clist_new(void);
void clist_print(link cur, void (*pf)(void *));
link clist_insert_after(link cur, link item);
link clist_delete(link cur, link item);
int clist_length(link cur);
copy
主程序流程
#include <stdio.h>
#include "clist.h"
int main(void)
{
link cursor = NULL;
int i = 0;
clist_print(cursor, print_int_data);
for (i = 0; i < 100; i++)
{
int *p = make_data(i+1);
link item = make_node(p);
cursor = clist_insert_after(cursor, item);
}
cursor = cursor->next;
clist_print(cursor, print_int_data);
printf("ring list length = %d\n", clist_length(cursor));
int step = 0;
while (cursor != NULL)
{
print_int_data(cursor->data);
step++;
if (step == 3)
{
printf("-> %d out\n", *(int *)(cursor->data));
cursor = clist_delete(cursor, cursor);
printf("length = %d\n", clist_length(cursor));
step = 0;
}
else
cursor = cursor->next;
//getchar();
//sleep(1);
}
return 0;
}
copy