From 2c2448cc94b8f17ac699814a75110411d57f3bea Mon Sep 17 00:00:00 2001 From: scratko Date: Sun, 7 Apr 2024 03:07:42 +0300 Subject: BFS, queue files Fixed remaining direction check for pacman (old version was commented out). Breadth First Search for red ghost. Changed switch style. --- queue.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 queue.c (limited to 'queue.c') diff --git a/queue.c b/queue.c new file mode 100644 index 0000000..dd3dc1b --- /dev/null +++ b/queue.c @@ -0,0 +1,70 @@ +#include "queue.h" +#include + +void queue_init(struct queue *q) +{ + q->first = NULL; + q->last = NULL; +} + +void queue_push(struct queue *q, const struct coordinates *data) +{ + struct item *new_element = malloc(sizeof(struct item)); + new_element->point = *data; + new_element->next = NULL; + if(!q->first) + q->first = q->last = new_element; + else { + q->last->next = new_element; + q->last = q->last->next; + } +} + +int empty(const struct queue *q) +{ + return q->first ? 0 : 1; +} + +struct coordinates queue_front(const struct queue *q) +{ + return q->first->point; +} + +void pop(struct queue *q) +{ + struct item *tmp = q->first; + q->first = q->first->next; + free(tmp); +} + +int equal_points(struct coordinates first_point, + struct coordinates second_point) +{ + return first_point.x == second_point.x && first_point.y == second_point.y; +} + +int is_consist_point(const struct queue *q, struct coordinates target_point) +{ + enum { false, true }; + struct item *tmp_item = q->first; + struct coordinates tmp_point; + while(tmp_item != NULL) { + tmp_point = tmp_item->point; + if(equal_points(tmp_point, target_point)) + return true; + tmp_item = tmp_item->next; + } + return false; +} + +void queue_clear(struct queue *q) +{ + struct item *tmp; + while(q->first != NULL) { + tmp = q->first; + q->first = q->first->next; + free(tmp); + } + q->first = NULL; + q->last = NULL; +} -- cgit v1.2.3