Skip to content

Commit

Permalink
Simple List and Graph code. Buggy.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwgoldman committed Jun 21, 2011
1 parent f50ede3 commit f404786
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 0 deletions.
134 changes: 134 additions & 0 deletions graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// graph.c
// adjacencylist
//
// Created by Sam Goldman on 6/21/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include "graph.h"
#include <stdlib.h>

int compare_edges(const void *aa, const void *bb);
void vertex_free(void *data);

int compare_edges(const void *aa, const void *bb) {
const Edge *a = aa;
const Edge *b = bb;
return b->weight - a->weight;
}

void vertex_free(void *data) {
Vertex *vertex = data;
list_free(vertex->edges);
free(vertex);
}

Graph *graph_create() {
Graph *graph = malloc(sizeof(Graph));
graph->vertices = list_create(vertex_free);
graph->count = 0;
return graph;
}

Vertex *vertex_create(void *data) {
Vertex *vertex = malloc(sizeof(Vertex));
vertex->data = data;
vertex->edges = list_create(free);
return vertex;
}

Edge *edge_create(Vertex *vertex, double weight) {
Edge *edge = malloc(sizeof(Edge));
edge->vertex = vertex;
edge->weight = weight;
return edge;
}

void graph_add_vertex(Graph *graph, Vertex *vertex) {
list_add_data(graph->vertices, vertex);
graph->count++;
}

void graph_add_vertex_sorted(Graph *graph, Vertex *vertex, int(*cmp)(const void *a, const void *b)) {
list_add_data_sorted(graph->vertices, vertex, cmp);
graph->count++;
}

void graph_remove_vertex(Graph *graph, Vertex *vertex) {
Node *n = graph->vertices->head;
Node *prev_n = NULL;
while (n) {
if (n->data == vertex) {
if (!prev_n) {
graph->vertices->head = n->next;
}
else {
prev_n->next = n->next;
}
vertex_free(vertex);
free(n);
}
else {
vertex_remove_edge_to_vertex(n->data, vertex);
}
prev_n = n;
n = n->next;
}
graph->count--;
}

void vertex_add_edge(Vertex *vertex, Edge *edge) {
list_add_data(vertex->edges, edge);
}

void vertex_add_edge_sorted(Vertex *vertex, Edge *edge) {
list_add_data_sorted(vertex->edges, edge, compare_edges);
}

void vertex_remove_edge(Vertex *vertex, Edge *edge) {
list_remove_data(vertex->edges, edge);
}

void vertex_add_edge_to_vertex(Vertex *from, Vertex *to, double weight) {
Edge *edge = edge_create(to, weight);
list_add_data(from->edges, edge);
}

void vertex_add_edge_to_vertex_sorted(Vertex *from, Vertex *to, double weight) {
Edge *edge = edge_create(to, weight);
list_add_data_sorted(from->edges, edge, compare_edges);
}

void vertex_remove_edge_to_vertex(Vertex *from, Vertex *to) {
Node *e = from->edges->head;
Node *prev_e = NULL;
while (e) {
if (((Edge *)e->data)->vertex == to) {
if (!prev_e) {
from->edges->head = e->next;
}
else {
prev_e = e->next;
}
free(e->data);
free(e);
break;
}
prev_e = e;
e = e->next;
}
}

void graph_sort_vertices(Graph *graph, int(*cmp)(const void *a, const void *b)) {
list_sort(graph->vertices, cmp);
}

void vertex_sort_edges(Vertex *vertex) {
list_sort(vertex->edges, compare_edges);
}

void graph_free(Graph *graph) {
list_free(graph->vertices);
free(graph);
}
45 changes: 45 additions & 0 deletions graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// graph.h
// adjacencylist
//
// Created by Sam Goldman on 6/21/11.
// Copyright 2011 Sam Goldman. All rights reserved.
//

#include "list.h"

typedef struct Edge {
struct Vertex *vertex;
double weight;
} Edge;

typedef struct Vertex {
void *data;
struct List *edges;
} Vertex;

typedef struct Graph {
struct List *vertices;
int count;
} Graph;

Graph *graph_create();
Vertex *vertex_create(void *data);
Edge *edge_create(Vertex *vertex, double weight);

void graph_add_vertex(Graph *graph, Vertex *vertex);
void graph_add_vertex_sorted(Graph *graph, Vertex *vertex, int(*cmp)(const void *a, const void *b));
void graph_remove_vertex(Graph *graph, Vertex *vertex);

void vertex_add_edge(Vertex *vertex, Edge *edge);
void vertex_add_edge_sorted(Vertex *vertex, Edge *edge);
void vertex_remove_edge(Vertex *vertex, Edge *edge);

void vertex_add_edge_to_vertex(Vertex *from, Vertex *to, double weight);
void vertex_add_edge_to_vertex_sorted(Vertex *from, Vertex *to, double weight);
void vertex_remove_edge_to_vertex(Vertex *from, Vertex *to);

void graph_sort_vertices(Graph *graph, int(*cmp)(const void *a, const void *b));
void vertex_sort_edges(Vertex *vertex);

void graph_free(Graph *graph);
151 changes: 151 additions & 0 deletions list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//
// list.c
// adjacencylist
//
// Created by Sam Goldman on 6/21/11.
// Copyright 2011 Sam Goldman. All rights reserved.
//

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

Node *node_create(void *data);

List *list_create(node_data_free_callback_t node_data_free_callback) {
List *list = malloc(sizeof(List));
list->head = NULL;
list->node_data_free_callback = node_data_free_callback;
return list;
}

Node *node_create(void *data) {
Node *node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}

void list_add_data(List *list, void *data) {
Node *node = node_create(data);
node->next = list->head;
list->head = node->next;
}

void list_add_data_sorted(List *list, void *data, int (*cmp)(const void *a, const void *b)) {
Node *node = node_create(data);
Node *n = list->head;
Node *prev_n = NULL;
while (n && cmp(n->data, data) < 0) {
prev_n = n;
n = n->next;
}
node->next = n;
if (!prev_n) {
list->head = node;
}
else {
prev_n->next = node;
}
}

void list_remove_data(List *list, void *data) {
Node *n = list->head;
Node *prev_n = NULL;
while (n) {
if (n->data == data) {
if (!prev_n) {
list->head = n->next;
}
else {
prev_n->next = n->next;
}
list->node_data_free_callback(data);
free(n);
break;
}
prev_n = n;
n = n->next;
}
}

void list_free(List *list) {
Node *n = list->head;
while (n) {
Node *next_n = n->next;
list->node_data_free_callback(n->data);
free(n);
n = next_n;
}
free (list);
}

void list_sort(List *list, int(*cmp)(const void *a, const void *b)) {
Node *p, *q, *e, *tail;
int insize, nmerges, psize, qsize, i;

Node *head = list->head;
if (!head) {
return;
}

insize = 1;

while (1) {
p = head;
list = NULL;
tail = NULL;

nmerges = 0;

while (p) {
nmerges++;
q = p;
psize = 0;
for (i = 0; i < insize; i++) {
psize++;
q = q->next;
if (!q) {
break;
}
}
qsize = insize;
while (psize > 0 || (qsize > 0 && q)) {
if (psize == 0) {
e = q;
q = q->next;
qsize--;
}
else if (qsize ==0 || !q) {
e = p;
p = p->next;
psize--;
}
else if (cmp(p->data, q->data) <= 0) {
e = p;
p = p->next;
psize--;
}
else {
e = q;
q = q->next;
qsize--;
}
if (tail) {
tail->next = e;
}
else {
head = e;
}
tail = e;
}
p = q;
}
tail->next = NULL;

if (nmerges <= 1) {
list->head = head;
}

insize *= 2;
}
}
26 changes: 26 additions & 0 deletions list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// list.h
// adjacencylist
//
// Created by Sam Goldman on 6/21/11.
// Copyright 2011 Sam Goldman. All rights reserved.
//

typedef void (*node_data_free_callback_t)(void *);

typedef struct Node {
void *data;
struct Node *next;
} Node;

typedef struct List {
Node *head;
node_data_free_callback_t node_data_free_callback;
} List;

List *list_create(node_data_free_callback_t node_data_free_callback);
void list_add_data(List *list, void *data);
void list_add_data_sorted(List *list, void *data, int (*cmp)(const void *a, const void *b));
void list_remove_data(List *list, void *data);
void list_free(List *list);
void list_sort(List *list, int(*cmp)(const void *a, const void *b));

0 comments on commit f404786

Please sign in to comment.