Commit 50aed9b
Changed files (2)
src
src/01/02b/doubly_linked_list.c
@@ -147,6 +147,22 @@ static void print(Node *node) {
printf("(nil<%d>%d)", node->data, node->next->data);
else
printf("(%d<%d>nil)", node->prev->data, node->data);
+ printf(" ");
+}
+
+/**
+ * Iterates through each node in a linked list.
+ *
+ * @param self The node to start iterating from.
+ * @param block The callback function to invoke on each node.
+ */
+void each(Node *self, Visitor block) {
+ Node *tmp = self;
+
+ while (tmp) {
+ block(tmp);
+ tmp = tmp->next;
+ }
}
/**
@@ -155,14 +171,7 @@ static void print(Node *node) {
* @param node The head of the linked list
*/
void inspect(Node *node) {
- if (!node)
- return;
-
printf("[ ");
- while (node) {
- print(node);
- printf(" ");
- node = node->next;
- }
+ each(node, &print);
printf("]\n");
}
src/01/02b/doubly_linked_list.h
@@ -5,6 +5,7 @@ struct node {
};
typedef struct node Node;
+typedef void (Visitor)(Node *);
Node *initialize(int data);
Node *add(Node *head, int data);