Created
March 18, 2017 09:12
-
-
Save helloqiu/94c50cc10b919a366f73f1282415b252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by helloqiu on 2017/3/18. | |
// | |
#ifndef LIST_LIST_H | |
#define LIST_LIST_H | |
struct list_head { | |
struct list_head *next, *prev; | |
}; | |
static __inline void __list_add(struct list_head *newx, struct list_head *prev, struct list_head *next) { | |
next->prev = newx; | |
newx->next = next; | |
newx->prev = prev; | |
prev->next = newx; | |
} | |
static __inline void list_add(struct list_head *newx, struct list_head *head) { | |
__list_add(newx, head, head->next); | |
} | |
static __inline void list_add_tail(struct list_head *newx, struct list_head *head) { | |
__list_add(newx, head->prev, head); | |
} | |
static __inline void INIT_LIST_HEAD(struct list_head *list) { | |
list->next = list; | |
list->prev = list; | |
} | |
static __inline struct list_head *list_get_tail(struct list_head *list) { | |
if (list->next == list) { | |
return list; | |
} else { | |
return list_get_tail(list->next); | |
} | |
} | |
#define list_for_each(pos, head) \ | |
for (pos = (head)->next; pos != (head); pos = pos->next) | |
#endif //LIST_LIST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "set.h" | |
int main() { | |
struct set a, b; | |
INIT_SET(&a); | |
INIT_SET(&b); | |
for (int i = 0; i < 10; i++) { | |
add_item(&a, i); | |
add_item(&b, i + 1); | |
} | |
print_set(intersection(&(a), &(b))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by helloqiu on 2017/3/18. | |
// | |
#ifndef LIST_SET_H | |
#define LIST_SET_H | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include "list.h" | |
#define container_of(posx, type, member) \ | |
((char *)posx - offsetof(type, member)) | |
struct set { | |
int value; | |
struct list_head list; | |
}; | |
static __inline void INIT_SET(struct set *s) { | |
INIT_LIST_HEAD(&s->list); | |
s->value = NULL; | |
} | |
static __inline void add_item(struct set *s, int n) { | |
if (s->value == NULL) { | |
s->value = n; | |
return; | |
} else { | |
struct set *temp; | |
temp = malloc(sizeof(struct set)); | |
INIT_SET(temp); | |
temp->value = n; | |
list_add(&(temp->list), &(s->list)); | |
} | |
} | |
static __inline void add_set(struct set *s, struct set *new_s) { | |
list_add(&(new_s->list), list_get_tail(&(s->list))); | |
} | |
static __inline struct set *intersection(struct set *a, struct set *b) { | |
struct set *c; | |
c = malloc(sizeof(struct set)); | |
INIT_SET(c); | |
struct set *temp_b; | |
struct set *temp_a; | |
struct list_head *pos_b; | |
struct list_head *pos_a; | |
list_for_each(pos_b, &(b->list)) { | |
temp_b = (struct set *) container_of(pos_b, struct set, list); | |
list_for_each(pos_a, &(a->list)) { | |
temp_a = (struct set *) container_of(pos_a, struct set, list); | |
if (temp_a->value == temp_b->value) { | |
add_item(c, temp_a->value); | |
break; | |
} | |
} | |
} | |
return c; | |
} | |
static __inline void print_set(struct set *s) { | |
struct list_head *pos; | |
struct set *temp; | |
list_for_each(pos, &(s->list)) { | |
temp = (struct set *) container_of(pos, struct set, list); | |
printf("%d\n", temp->value); | |
} | |
} | |
#endif //LIST_SET_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment