04hashtab-josephus

list.h

 

struct list_head {
    struct list_head *prev;
    struct list_head *next;
};

#define LIST_HEAD(head) struct list_head head = {&head, &head}

static inline void INIT_LIST_HEAD(struct list_head *node)
{
    node->prev = node;
    node->next = node;
}

static inline void __list_add(struct list_head *node,
        struct list_head *prev,
        struct list_head *next)
{
    node->prev = prev;
    node->next = next;
    prev->next = node;
    next->prev = node;
}

static inline void list_add(struct list_head *node,
        struct list_head *head)
{
    __list_add(node, head, head->next);
}

static inline void list_add_tail(struct list_head *node,
        struct list_head *head)
{
    __list_add(node, head->prev, head);
}


 

static inline int list_emtpy(struct list_head *head)
{
    return head->next == head;
}

static inline void list_del(struct list_head *node)
{
    node->prev->next = node->next;
    node->next->prev = node->prev;
}

static inline void list_del_init(struct list_head *node)
{
    list_del(node);
    INIT_LIST_HEAD(node);
}

#define offsetof(type, member) \
    ((size_t)(&((type*)0)->member))

#define container_of(ptr, type, member) \
    ((type*)((char*)ptr - offsetof(type, member)))

/* @cur: ?..list_head?..?.复?舵.?
 * @head: 澶磋.?圭.?板.
 */
#define list_for_each(cur, head) \
    for (cur = (head)->next; \
        (cur) != (head); \
        cur = (cur)->next)


 

#define list_for_each_safe(cur, tmp, head) \
    for (cur = (head)->next, tmp = (cur)->next; \
        (cur) != (head); \
        cur = tmp, tmp = (tmp)->next)

/* @ptr: ?..list_head?..?€?ㄧ.澶х??.??..?.复?舵.?
 * @head: 澶磋.?圭.?板.
 */
#define list_for_each_entry(ptr, head, member) \
    for ( ptr = container_of((head)->next, typeof(*(ptr)), member); \
        &(ptr)->member != (head); \
        ptr = container_of((ptr)->member.next, typeof(*(ptr)), member) )

#define list_for_each_entry_safe(ptr, tmp, head, member) \
    for ( ptr = container_of((head)->next, typeof(*(ptr)), member); \
        (&(ptr)->member != (head)) && (tmp = container_of((ptr)->member.next, typeof(*(ptr)), member)); \
        ptr = tmp )

#define list_for_each_continue(cur, head) \
    for (cur = (cur)->next; \
        (cur) != (head); \
        cur = (cur)->next)

#define list_for_each_reverse(cur, head) \
    for (cur = (head)->prev; \
        (cur) != (head); \
        cur = (cur)->prev)
                                                     


josephus.c

 

#include <stdio.h>
#include "list.h"

struct node {
    size_t num;
    struct list_head list;
};


 

static size_t josephus_loop(size_t sum,
{
    size_t check_count = 1;

    LIST_HEAD(head);

    struct node s[sum];
    size_t i = 0;
    //缂.?
    for (i = 0; i < sum; i++) {
        s[i].num = i;
        list_add_tail(&s[i].list, &head);
    }   

    struct node *ptr = NULL;
    struct node *tmp = NULL;
    while (head.next->next != &head) {
        list_for_each_entry_safe(ptr, tmp, &head, list) {
            if (check_count == count) {
                check_count = 1;
                list_del_init(&ptr->list);
                printf("del %ld ", ptr->num);
            } else {
                check_count++;
            }
        }
    }

    return container_of(head.next, struct node, list)->num;
}

int main()
{
    printf("\n%ld\n", josephus_loop(10, 3));

    return 0;
}


 

你可能感兴趣的:(04hashtab-josephus)