#include <stdio.h> struct entry { int value; struct entry *next; }; struct entry *g_list = NULL; struct entry *g_last = NULL; void init_list(int *vs, int len) { int i = 0; struct entry *head = (struct entry*)calloc(1, sizeof(struct entry)); struct entry *first = g_list = head; head->value = vs[0]; for (i = 1; i < len; i++) { struct entry *next = (struct entry*)calloc(1, sizeof(struct entry)); next->value = vs[i]; head->next = next; head = next; } head->next = first; g_last = head; } int go_to(struct entry *list, int T) { struct entry *pre = g_last; struct entry *curr = list; struct entry *next = list->next; int i = 0; for (i = 0; i < T-1; i ++) { pre = pre->next; curr = curr->next; next = next->next; } pre->next = next; g_list = next; g_last = pre; return curr->value; } int main (int argc, const char * argv[]) { int va[] = {1,2,3,4,5,6,7,8}; init_list(va, sizeof(va)/sizeof(int)); struct entry *thread = g_list; do { printf("out man:%d\n", go_to(g_list, 4)); }while (g_last != g_list); printf("last man:%d\n", go_to(g_list, 4)); return 0; }
#include <stdio.h> #include <stdlib.h> struct entry { int value; struct entry *next; }; struct entry* init_list(int *vs, int len) { int i = 0; struct entry *head = (struct entry*)calloc(1, sizeof(struct entry)); struct entry *first = head; head->value = vs[0]; for (i = 1; i < len; i++) { struct entry *next = (struct entry*)calloc(1, sizeof(struct entry)); next->value = vs[i]; head->next = next; head = next; } head->next = first; return head; } void go_to(struct entry *list, struct entry *pre_l, int T) { struct entry *pre = pre_l; struct entry *curr = list; struct entry *next = list->next; int i = 0; if (list->next == list) { printf("last:%d\n", list->value); return; } for (i = 0; i < T-1; i ++) { pre = pre->next; curr = curr->next; next = next->next; } pre->next = next; printf("ddddd:%d\n", curr->value); go_to(next, pre, T); //return curr->value; } int main (int argc, const char * argv[]) { int va[] = {1,2,3,4,5,6,7,8}; struct entry *ll = init_list(va, sizeof(va)/sizeof(int)); go_to(ll->next, ll, 4); return 0; }
struct entry { int value; struct entry *next, *prev; };
struct list_head *list_step(struct list_head* list, int step) { int i = 0; while(i++ < step) list = list->next; return list; }
有了上述接口,连同kernel自带的,我给出使用list_head实现的约瑟夫问题的代码:
#include <stdio.h> #include <stdlib.h> #include "list.h" struct entry { struct list_head list; int value; }; struct list_head *init_list(int *vs, int len) { int i = 0; struct list_head *head = NULL; for (i = 0; i < len; i++) { struct entry *next = (struct entry*)calloc(1, sizeof(struct entry)); INIT_LIST_HEAD(&next->list); next->value = vs[i]; if (i==0) head = &(next->list); else list_add_tail(&next->list, head); } return head; } void go_to(struct list_head *list, int T) { int i = 0; struct list_head *curr = NULL, *next = NULL; struct entry *e = NULL, *e2; if (list_empty(list)) { e = list_entry(list, struct entry, list); printf("last:%d\n", e->value); return; } curr = list_step(list, T-1); next = list_step(curr, 1); list_del(curr); e = list_entry(curr, struct entry, list); printf("curr:%d \n", e->value); go_to(next, T); } int main (int argc, const char * argv[]) { int va[] = {1,2,3,4,5,6,7,8}; struct list_head *head = init_list(va, sizeof(va)/sizeof(int)); go_to(head, 4); return 0; }
可见,这个代码很简单,除了我的函数以及变量命名不是很好之外,如果命名良好,只要看英语就可以知道整个解决思路了,没有任何让人看来是编程者专利的东西。