活用内核链表解决约瑟夫斯问题
約瑟夫斯問題(有時(shí)也稱為約瑟夫斯置換),是一個(gè)出現(xiàn)在計(jì)算機(jī)科學(xué)和數(shù)學(xué)中的問題。在計(jì)算機(jī)編程的算法中,類似問題又稱為“約瑟夫環(huán)”,也有的地方叫做“丟手絹”。
問題是這樣的:
有編號從1到n的n個(gè)人圍坐成一圈。從編號為1的人開始報(bào)數(shù),報(bào)到m的人出局,下一位再從 1 開始報(bào)數(shù),報(bào)到 m 的人出局,……如此持續(xù),直到剩下一人為止,假設(shè)此人的原始編號是x。給定 n和 m,求出x。
關(guān)于這個(gè)問題,我已經(jīng)寫了一篇博客。
http://blog.csdn.net/longintchar/article/details/75150621
因?yàn)樽罱诳磧?nèi)核鏈表,想把內(nèi)核鏈表用起來,我又想到了約瑟夫環(huán)。
假設(shè)n=8, m=3,那么淘汰順序應(yīng)該是3 、6、 1、 5 、2 、8 、4,最后剩下7號。
代碼如下。
#include <stdio.h> #include "list.h"#define N 8 //N為總?cè)藬?shù) #define M 3 //從1開始報(bào)數(shù),報(bào)M的人出局struct person_info {int data; //記錄編號struct list_head list; //內(nèi)核鏈表 };struct person_info person[N] = {0}; //N個(gè)人int main(void) {LIST_HEAD(head); //定義并且初始化頭結(jié)點(diǎn)int i; //雙向循環(huán)鏈表的尾插for (i = 0; i < N; ++i) {person[i].data = i+1; //編號從1開始,所以+1list_add_tail(&person[i].list, &head);} struct person_info *p_person = NULL;struct list_head *cur = NULL;struct list_head *tmp = NULL;int count = 0; //用來計(jì)數(shù)//安全遍歷節(jié)點(diǎn),報(bào)數(shù)到M時(shí)刪除結(jié)點(diǎn),直到只剩一個(gè)結(jié)點(diǎn)//循環(huán)停止條件是剩下一個(gè)人while( (&head)->next->next != (&head) ){list_for_each_safe(cur, tmp, &head){ ++count;//模擬計(jì)數(shù)if (M == count) { //小指針轉(zhuǎn)大指針p_person = container_of(cur, struct person_info, list);printf("del:%d\n", p_person->data);list_del(cur); //刪除結(jié)點(diǎn)count = 0; //重新計(jì)數(shù)}} }//打印最后一個(gè)人的編號p_person = container_of((&head)->next, struct person_info, list);printf("winner:%d\n", p_person->data);return 0; }程序運(yùn)行結(jié)果是:
del:3
del:6
del:1
del:5
del:2
del:8
del:4
winner:7
需要說明的是,以上代碼包含的頭文件"list.h"去哪里找呢?當(dāng)然是內(nèi)核源碼了。
方便起見,我把相關(guān)代碼扒拉出來,略微修改,組成了一個(gè)小而美的"list.h"。有了這個(gè)頭文件,即使到了單片機(jī)上,內(nèi)核鏈表也是可以用的。
#ifndef _LIST_H #define _LIST_H /*/usr/src/linux-headers-4.8.0-36-generic/include/linux/stddef.h *///求偏移量 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)/*/usr/src/linux-headers-4.8.0-36-generic/include/linux/kernel.h *//*** container_of - cast a member of a structure out to the containing structure* @ptr: the pointer to the member.* @type: the type of the container struct this is embedded in.* @member: the name of the member within the struct.**///小指針轉(zhuǎn)大指針 #define container_of(ptr, type, member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)( (char *)__mptr - offsetof(type,member) );})/*/usr/src/linux-headers-4.8.0-36-generic/include/linux/types.h */ struct list_head {struct list_head *next, *prev; };/*/usr/src/linux-headers-4.8.0-36-generic/include/linux/list.h */#define LIST_HEAD_INIT(name) { &(name), &(name) }//以下這個(gè)宏用來定義并且初始化頭結(jié)點(diǎn) #define LIST_HEAD(name) \struct list_head name = LIST_HEAD_INIT(name)/* kernel 3.14 */ static inline void __list_add(struct list_head *new,struct list_head *prev,struct list_head *next) {next->prev = new;new->next = next;new->prev = prev;prev->next = new; //kernel 4.8中 這句話是 WRITE_ONCE(prev->next, new); } /*** list_add - add a new entry* @new: new entry to be added* @head: list head to add it after** Insert a new entry after the specified head.* This is good for implementing stacks.*/ static inline void list_add(struct list_head *new, struct list_head *head) {__list_add(new, head, head->next); //頭插 }/*** list_add_tail - add a new entry* @new: new entry to be added* @head: list head to add it before** Insert a new entry before the specified head.* This is useful for implementing queues.*/ static inline void list_add_tail(struct list_head *new, struct list_head *head) {__list_add(new, head->prev, head); //尾插 }/** Delete a list entry by making the prev/next entries* point to each other.** This is only for internal list manipulation where we know* the prev/next entries already!*/ static inline void __list_del(struct list_head * prev, struct list_head * next) {next->prev = prev;prev->next = next; //WRITE_ONCE(prev->next, next); }static inline void list_del(struct list_head *entry) {__list_del(entry->prev, entry->next);//entry->next = LIST_POISON1;//entry->prev = LIST_POISON2; }/*** list_empty - tests whether a list is empty* @head: the list to test.*/ static inline int list_empty(const struct list_head *head) {return head->next == head;//return READ_ONCE(head->next) == head; } /*** list_for_each - iterate over a list* @pos: the &struct list_head to use as a loop cursor.* @head: the head for your list.*/ #define list_for_each(pos, head) \for (pos = (head)->next; pos != (head); pos = pos->next)/*** list_for_each_safe - iterate over a list safe against removal of list entry* @pos: the &struct list_head to use as a loop cursor.* @n: another &struct list_head to use as temporary storage* @head: the head for your list.*/ #define list_for_each_safe(pos, n, head) \for (pos = (head)->next, n = pos->next; pos != (head); \pos = n, n = pos->next)/*** list_entry - get the struct for this entry* @ptr: the &struct list_head pointer.* @type: the type of the struct this is embedded in.* @member: the name of the list_head within the struct.*/ #define list_entry(ptr, type, member) \container_of(ptr, type, member)/*** list_first_entry - get the first element from a list* @ptr: the list head to take the element from.* @type: the type of the struct this is embedded in.* @member: the name of the list_head within the struct.** Note, that list is expected to be not empty.*/ #define list_first_entry(ptr, type, member) \list_entry((ptr)->next, type, member)/*** list_next_entry - get the next element in list* @pos: the type * to cursor* @member: the name of the list_head within the struct.*/ #define list_next_entry(pos, member) \list_entry((pos)->member.next, typeof(*(pos)), member)/*** list_for_each_entry - iterate over list of given type* @pos: the type * to use as a loop cursor.* @head: the head for your list.* @member: the name of the list_head within the struct.*/ #define list_for_each_entry(pos, head, member) \for (pos = list_first_entry(head, typeof(*pos), member); \&pos->member != (head); \pos = list_next_entry(pos, member)) /*** list_for_each_entry_safe - iterate over list of given type safe against removal of list entry* @pos: the type * to use as a loop cursor.* @n: another type * to use as temporary storage* @head: the head for your list.* @member: the name of the list_head within the struct.*/ #define list_for_each_entry_safe(pos, n, head, member) \for (pos = list_first_entry(head, typeof(*pos), member), \n = list_next_entry(pos, member); \&pos->member != (head); \pos = n, n = list_next_entry(n, member))/*** list_for_each_entry_from - iterate over list of given type from the current point* @pos: the type * to use as a loop cursor.* @head: the head for your list.* @member: the name of the list_head within the struct.** Iterate over list of given type, continuing from current position.*///從pos指向的結(jié)構(gòu)體開始遍歷 #define list_for_each_entry_from(pos, head, member) \for (; &pos->member != (head); \pos = list_next_entry(pos, member)) /*** list_for_each_entry_safe_from - iterate over list from current point safe against removal* @pos: the type * to use as a loop cursor.* @n: another type * to use as temporary storage* @head: the head for your list.* @member: the name of the list_head within the struct.** Iterate over list of given type from current point, safe against* removal of list entry.*/ #define list_for_each_entry_safe_from(pos, n, head, member) \for (n = list_next_entry(pos, member); \&pos->member != (head); \pos = n, n = list_next_entry(n, member)) /*** list_for_each_entry_continue - continue iteration over list of given type* @pos: the type * to use as a loop cursor.* @head: the head for your list.* @member: the name of the list_head within the struct.** Continue to iterate over list of given type, continuing after* the current position.*///從pos的下一個(gè)開始遍歷 #define list_for_each_entry_continue(pos, head, member) \for (pos = list_next_entry(pos, member); \&pos->member != (head); \pos = list_next_entry(pos, member))/*** list_for_each_entry_safe_continue - continue list iteration safe against removal* @pos: the type * to use as a loop cursor.* @n: another type * to use as temporary storage* @head: the head for your list.* @member: the name of the list_head within the struct.** Iterate over list of given type, continuing after current point,* safe against removal of list entry.*/ #define list_for_each_entry_safe_continue(pos, n, head, member) \for (pos = list_next_entry(pos, member), \n = list_next_entry(pos, member); \&pos->member != (head); \pos = n, n = list_next_entry(n, member))#endif總結(jié)
以上是生活随笔為你收集整理的活用内核链表解决约瑟夫斯问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度搜索关键字抓取_百度搜索引擎收录的抓
- 下一篇: 自定义依赖注解无效_关于Apt注解实践与