List *l; /* list */
int i; /* counter */
int *x; /* pointer to integer */
l = l_new(); /* make new list */
for (i = 0; i < 10; i++) {
x = (int *) malloc (sizeof(int)); /* allocate new int */
*x = i; /* set integer to i */
l_append(l, x); /* add it to the list */
}
return l;
}
void print_list(List *l) {
int *item; /* list item */
l_fforeach(l, item) { /* loop over list */
printf("%d ", *item); /* print the integer */
} l_endfor;
printf("\n");
}
void remove_evens(List *l) {
int *y;
l_rwd(l); /* moves the current pointer
to the head of the list */
while (!l_atend(l)) { /* repeat until the end of
the list is reached */
y = l_get(l); /* set y to the current item */
if (even(*y)) {
l_rm(l); /* if y points to an even
integer, remove the item
from the list */
free(y);
} else {
l_nxt(l): /* otherwise advance the
current pointer */
}
}
}
void destroy_int_list(List *l) {
int *item; /* list item */
l_fforeach(l, item) { /* loop over list */
free(item); /* free integer memory */
} l_endfor;
l_free(l); /* free the list */
}
void graph_example() {
Graph *g;
Node *n1, *n2;
UEdge *ue;
char buf[1024];
/* read the graph in from a file */
g = graph_read_from_file("graph1.graph");
/* verify successful read */
if (!g) {
warn("Error reading graph from graph1.graph.\n");
return;
}
/* for every pair of distinct nodes, do */
l_fforeach(graph_nodes(g), n1) {
l_fforeach(graph_nodes(g), n2) {
if (n1 != n2) { /* if n1 and n2 are
unique */
/* if there is no uedge between n1 and n2 */
if (!graph_getuedge_byendpoints(g, n1, n2)) {
/* add a new one */
ue = graph_add_uedge(g, n1, n2);
/* give the edge an attribute called "label" */
sprintf(buf, "%s -- %s", node_name(n1),
node_name(n2));
uedge_setattr(ue, "label", buf);
}
}
} l_endfor;
} l_endfor;
/* write graph to file */
graph_write_to_file("graph1.modified.graph", g);
}
typedef enum {white, gray, black} Color;
List *graph_dfs_2(Graph *g) {
List *dfs_nl; /* DFS node list */
Node *n; /* graph node */
Color *c;
/* create the DFS nodelist */
dfs_nl = l_new();
/* Set all nodes to white */
l_fforeach(graph_nodes(g), n) {
c = (Color *) malloc (sizeof(Color));
*c = white; /* create a Color structure */
node_set_user_info(n, c); /* and make it the user info */
} l_endfor; /* of the node */
/* For every node, do */
l_fforeach(graph_nodes(g), n) {
visit_node_2(n, dfs_nl); /* Start the DFS here */
} l_endfor;
return dfs_nl;
}
List *graph_dfs(Graph *g) {
List *dfs_nl; /* DFS node list */
Node *n; /* graph node */
/* create the DFS nodelist */
dfs_nl = l_new();
/* ensure the "visited?" attribute is un-set on all nodes */
l_fforeach(graph_nodes(g), n) {
node_delattr(n, "visited?");
} l_endfor;
/* For every node, do */
l_fforeach(graph_nodes(g), n) {
visit_node(n, dfs_nl); /* Start the DFS here */
} l_endfor;
return dfs_nl;
}
void visit_node(Node *n, List *dfs_nl) {
Edge *e;
Node *c_n;
/* Ignore the node if it is has been or is being visited */
if (node_getattr(n, "visited?")) return;
/* We need to visit this node, so mark it as being visted */
node_setattr(n, "visited?", "is being visited");
/* visit all the child nodes */
l_fforeach(node_outedges(n), e) {
c_n = edge_dst(e); /* get the child node */
visit_node(c_n, dfs_nl); /* visit the node */
} l_endfor;
/* All children have been visited, mark this node as visited */
node_setattr(n, "visited?", "yes");
/* Add it to the end of the DFS nodelist */
l_append(dfs_nl, n);
}
void visit_node_2(Node *n, List *dfs_nl) {
Edge *e;
Node *c_n;
Color *c;
/* get the user_info field of this node */
c = node_get_user_info(n);
/* Ignore the node if it is black (already been visited)
or gray (is being visited) */
if ((*c == black) || (*c == gray)) return;
/* Mark this node gray (being visited) */
*c = gray;
/* visit all the child nodes */
l_fforeach(node_outedges(n), e) {
c_n = edge_dst(e); /* get the child node */
visit_node_2(c_n, dfs_nl); /* visit the node */
} l_endfor;
/* All children have been visited, mark this node black */
*c = black;
/* Add it to the end of the DFS nodelist */
l_append(dfs_nl, n);
}