From 351db3f36637daa7b8e5a275734035b3f33288e5 Mon Sep 17 00:00:00 2001 From: ergz Date: Sat, 15 Jul 2023 11:33:23 -0700 Subject: [PATCH] implements the q operations --- c/breadth-first-search.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/c/breadth-first-search.c b/c/breadth-first-search.c index 5aa7acd..79be5db 100644 --- a/c/breadth-first-search.c +++ b/c/breadth-first-search.c @@ -5,12 +5,12 @@ typedef struct QNode { int value; struct QNode* next; - struct QNode* prev; } QNode; typedef struct Q { QNode* head; QNode* tail; + int length; } Q; typedef struct TreeNode { @@ -35,7 +35,6 @@ TreeNode* new_tree_node(int value) { QNode* new_qnode(int value) { QNode* node = malloc(sizeof(QNode)); node->next = NULL; - node->prev = NULL; node->value = value; return (node); @@ -65,6 +64,26 @@ void add_child_right(TreeNode* parent, TreeNode* node) { } } +// always add at tail +void q_add_node(Q* q, QNode* node) { + if (q->length == 0) { + q->head = node; + q->tail = node; + q->length++; + } else { + q->tail->next = node; + q->tail = node; + q->length++; + } +} + +// always remove from head +void q_remove_node(Q* q) { + QNode* n = q->head; + q->head = n->next; + free(n); +} + /* 10 5 7 @@ -84,4 +103,4 @@ int main() { add_child_left(root->right, new_tree_node(88)); add_child_right(root->right, new_tree_node(14)); -} \ No newline at end of file +}