|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +typedef struct GraphNode { |
| 6 | + char *key; |
| 7 | + char **values; |
| 8 | +} GraphNode; |
| 9 | + |
| 10 | +GraphNode graph[] = { |
| 11 | + {"you", (char *[]){"alice", "bob", "claire", NULL}}, |
| 12 | + {"bob", (char *[]){"anuj", "peggy", NULL}}, |
| 13 | + {"alice", (char *[]){"peggy", NULL}}, |
| 14 | + {"claire", (char *[]){"thom", "jonny", NULL}}, |
| 15 | + {"anuj", NULL}, |
| 16 | + {"peggy", NULL}, |
| 17 | + {"thom", NULL}, |
| 18 | + {"jonny", NULL}, |
| 19 | +}; |
| 20 | + |
| 21 | +typedef struct QueueNode { |
| 22 | + char *name; |
| 23 | + struct QueueNode *next; |
| 24 | +} QueueNode; |
| 25 | + |
| 26 | +typedef struct Queue { |
| 27 | + struct QueueNode *head; |
| 28 | + struct QueueNode *tail; |
| 29 | +} Queue; |
| 30 | + |
| 31 | +void enqueue(Queue *queue, char *name) { |
| 32 | + QueueNode *node = (QueueNode *)malloc(sizeof(QueueNode)); |
| 33 | + node->name = name; |
| 34 | + node->next = NULL; |
| 35 | + if (queue->tail) { |
| 36 | + queue->tail->next = node; |
| 37 | + queue->tail = node; |
| 38 | + } else { |
| 39 | + queue->head = queue->tail = node; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +char *dequeue(Queue *queue) { |
| 44 | + if (!queue->head) |
| 45 | + return NULL; |
| 46 | + QueueNode *node = queue->head; |
| 47 | + char *name = node->name; |
| 48 | + queue->head = queue->head->next; |
| 49 | + if (!queue->head) |
| 50 | + queue->tail = NULL; |
| 51 | + free(node); |
| 52 | + return name; |
| 53 | +} |
| 54 | + |
| 55 | +int queue_empty(Queue *queue) { return queue->head == NULL; } |
| 56 | + |
| 57 | +void free_queue(Queue *queue) { |
| 58 | + while (queue->head) { |
| 59 | + QueueNode *node = queue->head; |
| 60 | + queue->head = queue->head->next; |
| 61 | + free(node); |
| 62 | + } |
| 63 | + queue->tail = NULL; |
| 64 | +} |
| 65 | + |
| 66 | +char **get_neighbors(char *name) { |
| 67 | + for (int i = 0; i < sizeof(graph) / sizeof(graph[0]); i++) { |
| 68 | + if (strcmp(graph[i].key, name) == 0) { |
| 69 | + return graph[i].values; |
| 70 | + } |
| 71 | + } |
| 72 | + return NULL; |
| 73 | +} |
| 74 | + |
| 75 | +int person_is_seller(char *name) { |
| 76 | + int len = strlen(name); |
| 77 | + return len > 0 && name[len - 1] == 'm'; |
| 78 | +} |
| 79 | + |
| 80 | +int contains(char **list, int count, char *name) { |
| 81 | + for (int i = 0; i < count; i++) { |
| 82 | + if (strcmp(list[i], name) == 0) { |
| 83 | + return 1; |
| 84 | + } |
| 85 | + } |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +int search(char *name) { |
| 90 | + Queue queue = {NULL, NULL}; |
| 91 | + char **neighbors = get_neighbors(name); |
| 92 | + if (neighbors) { |
| 93 | + for (int i = 0; neighbors[i]; i++) { |
| 94 | + enqueue(&queue, neighbors[i]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + char **searched = NULL; |
| 99 | + int searched_count = 0; |
| 100 | + int searched_cap = 0; |
| 101 | + |
| 102 | + int found = 0; |
| 103 | + while (!queue_empty(&queue)) { |
| 104 | + char *person = dequeue(&queue); |
| 105 | + if (!contains(searched, searched_count, person)) { |
| 106 | + if (person_is_seller(person)) { |
| 107 | + printf("%s is a mango seller\n", person); |
| 108 | + found = 1; |
| 109 | + break; |
| 110 | + } |
| 111 | + char **neighbors = get_neighbors(person); |
| 112 | + if (neighbors) { |
| 113 | + for (int i = 0; neighbors[i]; i++) { |
| 114 | + enqueue(&queue, neighbors[i]); |
| 115 | + } |
| 116 | + } |
| 117 | + if (searched_count >= searched_cap) { |
| 118 | + searched_cap = searched_cap ? searched_cap * 2 : 4; |
| 119 | + searched = realloc(searched, searched_cap * sizeof(char *)); |
| 120 | + } |
| 121 | + searched[searched_count++] = person; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + free_queue(&queue); |
| 126 | + free(searched); |
| 127 | + return found; |
| 128 | +} |
| 129 | + |
| 130 | +int main() { |
| 131 | + search("you"); |
| 132 | + return 0; |
| 133 | +} |
0 commit comments