Skip to content

Instantly share code, notes, and snippets.

@simplexityx
Created February 3, 2017 00:05
Show Gist options
  • Save simplexityx/b3d63f4d801e3a09948020aa2a51f3aa to your computer and use it in GitHub Desktop.
Save simplexityx/b3d63f4d801e3a09948020aa2a51f3aa to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include "set.h"
typedef struct set set_t;
typedef struct node node_t;
struct set{
node_t *root;
int size;
cmpfunc_t cmpfunc;
};
struct node{
node_t *lchild;
node_t *rchild;
void* elem;
};
node_t *createnode(void* elem){
node_t *node = malloc(sizeof(node_t));
node->elem=elem;
node->lchild=NULL;
node->rchild=NULL;
return node;
}
set_t *set_create(cmpfunc_t cmpfunc){
set_t *newset = malloc(sizeof(set_t));
newset->root = NULL;
newset->size = 0;
return newset;
}
void printset(set_t *set){
printf("%d", set->root);
}
void set_add(set_t *set, void *elem){
node_t *node;
node = createnode(elem);
//node_t *tmp=set->root;
if (set->root==NULL){
set->root=node;
}
}
#ifndef SET_H
#define SET_H
typedef int (*cmpfunc_t)(void *, void *);
/*
* The type of sets.
*/
struct node;
typedef struct node node_t;
struct set;
typedef struct set set_t;
void printset(set_t *set);
set_t *sort(set_t *set);
/*
* Creates a new set using the given comparison function
* to compare elements of the set.
*/
set_t *set_create(cmpfunc_t cmpfunc);
/*
* Destroys the given set. Subsequently accessing the set
* will lead to undefined behavior.
*/
void set_destroy(set_t *set);
/*
* Returns the size (cardinality) of the given set.
*/
int set_size(set_t *set);
/*
* Adds the given element to the given set.
*/
void set_add(set_t *set, void *elem);
/*
* Returns 1 if the given element is contained in
* the given set, 0 otherwise.
*/
int set_contains(set_t *set, void *elem);
/*
* Returns the union of the two given sets; the returned
* set contains all elements that are contained in either
* a or b.
*/
set_t *set_union(set_t *a, set_t *b);
/*
* Returns the intersection of the two given sets; the
* returned set contains all elements that are contained
* in both a and b.
*/
set_t *set_intersection(set_t *a, set_t *b);
/*
* Returns the set difference of the two given sets; the
* returned set contains all elements that are contained
* in a and not in b.
*/
set_t *set_difference(set_t *a, set_t *b);
/*
* Returns a copy of the given set.
*/
set_t *set_copy(set_t *set);
/*
* The type of set iterators.
*/
struct set_iter;
typedef struct set_iter set_iter_t;
/*
* Creates a new set iterator for iterating over the given set.
*/
set_iter_t *set_createiter(set_t *set);
/*
* Destroys the given set iterator.
*/
void set_destroyiter(set_iter_t *iter);
/*
* Returns 0 if the given set iterator has reached the end of the
* set, or 1 otherwise.
*/
int set_hasnext(set_iter_t *iter);
/*
* Returns the next element in the sequence represented by the given
* set iterator.
*/
void *set_next(set_iter_t *iter);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment