aboutsummaryrefslogtreecommitdiff
path: root/sem3/algo/mm2/linked/llist.c
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-10-12 16:26:42 +0200
committerJulian T <julian@jtle.dk>2020-10-12 16:54:48 +0200
commit4e05a55e373bd315e721d534a1711fec4c0054c5 (patch)
tree4fc0141760c2730ed79aaa4c7aa60ea45cc66cc0 /sem3/algo/mm2/linked/llist.c
parentb7f9cf43c8a9ab3400cbb30d5e1cadb0c6c2cf23 (diff)
Moved to linux kernel inspired clang-format
Diffstat (limited to 'sem3/algo/mm2/linked/llist.c')
-rw-r--r--sem3/algo/mm2/linked/llist.c127
1 files changed, 62 insertions, 65 deletions
diff --git a/sem3/algo/mm2/linked/llist.c b/sem3/algo/mm2/linked/llist.c
index 8044e0b..e4166ee 100644
--- a/sem3/algo/mm2/linked/llist.c
+++ b/sem3/algo/mm2/linked/llist.c
@@ -3,92 +3,89 @@
#include <string.h>
#include <stdio.h>
-
#define OUT_OFF_BOUNDS 2
void llist_init(llist_t *list)
{
- /* Zero out structure */
- list->head = list->root = NULL;
-
- list->len = 0;
+ /* Zero out structure */
+ list->head = list->root = NULL;
+ list->len = 0;
}
void llist_append(llist_t *list, int val)
{
- /* Insert node after HEAD */
- list->head = node_insert(list->head, val);
+ /* Insert node after HEAD */
+ list->head = node_insert(list->head, val);
- /* Check if was list is empty */
- if ( list->len == 0 ) {
- /* Set root */
- list->root = list->head;
- }
+ /* Check if was list is empty */
+ if (list->len == 0) {
+ /* Set root */
+ list->root = list->head;
+ }
- /* Increase count */
- list->len++;
+ /* Increase count */
+ list->len++;
}
node_t *llist_get_node(llist_t *list, unsigned int index)
{
- /* Check if we have it */
- if ( index >= list->len ) {
- return NULL;
- }
-
- /* Find the best way to go down the list */
- int direc = index > (list->len / 2) ? -1 : 1;
-
- /* Setup start location */
- int pos = direc > 0 ? 0 : list->len-1;
- node_t *node = direc > 0 ? list->root : list->head;
-
- /* Go to index */
- while ( pos != index ) {
- /* TODO kinda risky, we trust our math and len */
- node = direc > 0 ? node->next : node->prev;
-
- pos += direc;
- }
-
- return node;
+ /* Check if we have it */
+ if (index >= list->len) {
+ return NULL;
+ }
+
+ /* Find the best way to go down the list */
+ int direc = index > (list->len / 2) ? -1 : 1;
+
+ /* Setup start location */
+ int pos = direc > 0 ? 0 : list->len - 1;
+ node_t *node = direc > 0 ? list->root : list->head;
+
+ /* Go to index */
+ while (pos != index) {
+ /* TODO kinda risky, we trust our math and len */
+ node = direc > 0 ? node->next : node->prev;
+
+ pos += direc;
+ }
+
+ return node;
}
int llist_get(llist_t *list, unsigned int index)
{
- /* Get node */
- node_t *node = llist_get_node(list, index);
- if ( node == NULL ) {
- /* Yes i know this is stupid */
- return -1;
- }
-
- /* Return value */
- return node->val;
+ /* Get node */
+ node_t *node = llist_get_node(list, index);
+ if (node == NULL) {
+ /* Yes i know this is stupid */
+ return -1;
+ }
+
+ /* Return value */
+ return node->val;
}
-
int llist_pop(llist_t *list, unsigned int index)
{
- /* Get node */
- node_t *node = llist_get_node(list, index);
- if ( node == NULL ) {
- /* Yes i know this is stupid */
- return -1;
- }
-
- /* Update root and head if we delete it */
- if ( node == list->root ) {
- list->root = node->next;
- }
- if ( node == list->head ) {
- list->head = node->prev;
- }
-
- /* Keep len up to date */
- list->len--;
-
- /* Delete stuff */
- return node_pop(node);
+ /* Get node */
+ node_t *node = llist_get_node(list, index);
+ if (node == NULL) {
+ /* Yes i know this is stupid */
+ return -1;
+ }
+
+ /* Update root and head if we delete it */
+ if (node == list->root) {
+ list->root = node->next;
+ }
+ if (node == list->head) {
+ list->head = node->prev;
+ }
+
+ /* Keep len up to date */
+ list->len--;
+
+ /* Delete stuff */
+ return node_pop(node);
}