From 0c632fce0ee4e9cba2fb4a813cf6f0411eb80b64 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Tue, 21 Feb 2023 12:29:45 +0100 Subject: [PATCH] created connection linked list in chat-misc --- src/chat/misc.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/chat/misc.c b/src/chat/misc.c index 57b4c3a..d97d5e7 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -19,6 +19,7 @@ along with this program. If not, see . #include #include +#include #include #include #include @@ -26,6 +27,86 @@ along with this program. If not, see . #include +//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) +typedef struct node +{ + int connection; + struct node *next; +} node_t; //SINGLE LINKED LIST + +node_t *head = NULL; + +void push_to_list(int connection) +{ + //CREATE NODE + node_t *new_node = malloc(sizeof(node_t)); + node_t *buffer = head; + + new_node -> connection = connection; + new_node -> next = NULL; + + if (head == NULL) //INIT LIST + { + head = new_node; + } else + { + while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST + + buffer -> next = new_node; //LINK + } +} + +void remove_node(node_t *node) +{ + node_t *buffer_1 = head; + node_t *buffer_2; + + while (buffer_1 -> next != NULL) //GO TROUGH EVERY ELEMENT IN LIST + { + if (buffer_1 == node) break; //FOUND (IF THE WHILE GOES TROUGH THE WHOLE LIST WITHOUT THE break, I ASSUME THE LAST NODE IS THE CORRECT ONE) + + buffer_1 = buffer_1 -> next; + } + + if (node != buffer_1) return; //node WASN'T FOUND + + if (buffer_1 == head) //node WAS THE FIRST NODE IN THE LIST + { + //UNLINK + head = buffer_1 -> next; + } else //wdyt + { + //GET THE NODE BEFORE node + buffer_2 = head; + + while (buffer_2 -> next != buffer_1) buffer_2 = buffer_2 -> next; + + //UNLINK + buffer_2 -> next = buffer_1 -> next; + } + + //DEALLOCATION + free(node); +} + +node_t *get_node(int connection) +{ + if (head == NULL) return NULL; //EMPTY LIST + + node_t *buffer = head; + while (buffer -> next != NULL) + { + if (buffer -> connection == connection) return buffer; + + buffer = buffer -> next; + } + + if (connection != buffer -> connection) buffer = NULL; //PREVENT FROM RETURNING INVALID NODE + + return buffer; +} + +//GLOBAL void send_socket(char *text, int socket) { unsigned short text_length = (unsigned short) strlen(text);