nya
This commit is contained in:
parent
6161b74ffc
commit
eeffd774de
11 changed files with 265 additions and 703 deletions
31
graphs/graph.c
Normal file
31
graphs/graph.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
typedef enum color_t {
|
||||
WHITE, GREY, BLACK
|
||||
} color;
|
||||
|
||||
// unweighted graph
|
||||
|
||||
typedef struct graph_t {
|
||||
char *neibs; // [N*N]
|
||||
size_t N;
|
||||
int unordered;
|
||||
} graph;
|
||||
|
||||
graph *graph_new(size_t nodesm int is_ordered) {
|
||||
graph *t = (graph *) malloc(sizeof(graph));
|
||||
t->N = nodes;
|
||||
t->unoreded = !is_ordered;
|
||||
t->neibs = (char *) calloc(sizeof(char), nodes * nodes);
|
||||
return t;
|
||||
}
|
||||
|
||||
void graph_delete(graph *g) {
|
||||
free(g->neibs);
|
||||
free(g);
|
||||
}
|
||||
|
||||
void graph_add_edge(graph *t, int from, int to) {
|
||||
t->neibs[from * t->N + to] = 1;
|
||||
if (t->unordered) {
|
||||
t->neibs[to * t->N + from] = 1;
|
||||
}
|
||||
}
|
||||
6
graphs/main.c
Normal file
6
graphs/main.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include "queue.c"
|
||||
#include "graph.c"
|
||||
|
||||
int main() {
|
||||
queue *q = queue_new(5);
|
||||
}
|
||||
36
graphs/queue.c
Normal file
36
graphs/queue.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef int T;
|
||||
typedef struct queue_s {
|
||||
T *body, *begin, *end, *body_end;
|
||||
} queue;
|
||||
|
||||
queue* queue_new(size_t size) {
|
||||
queue *t = (queue *)malloc(sizeof(queue));
|
||||
assert(t != NULL);
|
||||
t->body = (int *)malloc(sizeof(T)*size);
|
||||
t->begin = t->end = t->body;
|
||||
t->body_end = t->body + size;
|
||||
return t;
|
||||
}
|
||||
|
||||
void queue_delete(queue *t) {
|
||||
free(t->body);
|
||||
free(t);
|
||||
}
|
||||
|
||||
void queue_enqueue(queue *t, T el) {
|
||||
assert(t->end < t->body_end);
|
||||
*t->end++ = el;
|
||||
}
|
||||
|
||||
int queue_empty(queue const *t) {
|
||||
return t->begin == t->end;
|
||||
}
|
||||
|
||||
T queue_dequeue(queue *t) {
|
||||
return *t->begin++;
|
||||
}
|
||||
|
||||
Reference in a new issue