summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-05 15:12:30 -0600
committermo khan <mo.khan@gmail.com>2020-07-05 15:12:30 -0600
commit6db0bd189b34d83867e1117f822013704b6fc358 (patch)
tree328bc24c7dad135024fd082ec76e50b8dbc4df05
parent6c5cbdb6669e7d47103f3ca40bbd2cf8b0895274 (diff)
Add doxygen comments
-rw-r--r--src/01/06/min_stack.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/01/06/min_stack.c b/src/01/06/min_stack.c
index f800943..7aa02ae 100644
--- a/src/01/06/min_stack.c
+++ b/src/01/06/min_stack.c
@@ -2,6 +2,14 @@
#include <stdio.h>
#include <stdlib.h>
+/**
+ * The equivalent of a constructor for initialzing
+ * a new Node in a linked list.
+ *
+ * @param data The data to bind to the node
+ * @param next The next node to point to. NULL for a new head.
+ * @return The new linked list Node.
+ */
static Node *new (int data, Node *next) {
Node *node = malloc(sizeof(Node));
node->next = next;
@@ -9,6 +17,12 @@ static Node *new (int data, Node *next) {
return node;
}
+/**
+ * The equivalent of a constructor for initialzing
+ * a new min Stack.
+ *
+ * @return The new Stack
+ */
Stack *initialize(void) {
Stack *self = malloc(sizeof(Stack));
self->head = NULL;
@@ -17,8 +31,19 @@ Stack *initialize(void) {
return self;
}
+/**
+ * Returns the number of items on the stack.
+ *
+ * @param self The stack to investigate.
+ */
int size(Stack *self) { return self->size; }
+/**
+ * Pushes a new item on to a Stack
+ *
+ * @param self The stack to push the data on to
+ * @param data The data to push on to the Stack
+ */
void push(Stack *self, int data) {
if (!self->min || (data < self->min->data))
self->min = new (data, self->min);
@@ -27,6 +52,12 @@ void push(Stack *self, int data) {
self->size++;
}
+/**
+ * Iterates through each item in a linked list.
+ *
+ * @param head The head of the linked list
+ * @block The callback function to invoke on each item in the list.
+ */
void each(Node *head, Visitor block) {
Node *tmp = head;
@@ -36,6 +67,11 @@ void each(Node *head, Visitor block) {
}
}
+/**
+ * Returns the minimum value in the Stack.
+ *
+ * @param self The stack to investigate
+ */
int min(Stack *self) {
if (self->min)
return self->min->data;
@@ -55,6 +91,11 @@ int min(Stack *self) {
return (int)NULL;
}
+/**
+ * Pops off the item from the top of the Stack.
+ *
+ * @param self The stack to pop an item off of.
+ */
int pop(Stack *self) {
if (!self->head)
return (int)NULL;
@@ -70,8 +111,18 @@ int pop(Stack *self) {
return data;
}
+/**
+ * Prints a visual representation a Node in the linked list.
+ *
+ * @param node The node to print.
+ */
void print_node(Node *node) { printf("[%d]", node->data); }
+/**
+ * A helper function to print out a visual representation of a Stack.
+ *
+ * @param stack the Stack to print out.
+ */
void inspect(Stack *stack) {
printf("\t");
each(stack->head, &print_node);