summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2022-06-20 00:31:02 +0200
committerpgen <p.gen.progs@gmail.com>2022-06-20 01:06:52 +0200
commit6bc96ccc668003af9ca2d3056792ac783277faba (patch)
treed8f41a64f60db3e14636f5d102cdbe41dc9d82bd
parent57e04fef7b7ee198818ae06a07f418c11faedd7a (diff)
Various small changes
- add/fix const keyword. - add comments. - rename variables.
-rw-r--r--list.c36
-rw-r--r--list.h4
-rw-r--r--smenu.c2
-rw-r--r--smenu.h2
-rw-r--r--utils.c2
-rw-r--r--utils.h2
-rw-r--r--xmalloc.h2
7 files changed, 31 insertions, 19 deletions
diff --git a/list.c b/list.c
index 467111a..daf6a56 100644
--- a/list.c
+++ b/list.c
@@ -26,11 +26,13 @@
#include "list.h"
static ll_node_t *
-ll_partition(ll_node_t * l, ll_node_t * h, int (*comp)(void *, void *),
+ll_partition(ll_node_t * l, ll_node_t * h,
+ int (*comp)(void const *, void const *),
void (*swap)(void **, void **));
static void
-ll_quicksort(ll_node_t * l, ll_node_t * h, int (*comp)(void *, void *),
+ll_quicksort(ll_node_t * l, ll_node_t * h,
+ int (*comp)(void const *, void const *),
void (*swap)(void **, void **));
/* ========================== */
@@ -82,8 +84,8 @@ ll_append(ll_t * const list, void * const data)
node->data = data;
node->next = NULL;
-
node->prev = list->tail;
+
if (list->tail)
list->tail->next = node;
else
@@ -91,7 +93,7 @@ ll_append(ll_t * const list, void * const data)
list->tail = node;
- ++list->len;
+ ++list->len; /* One more node in the list. */
}
#if 0
@@ -112,8 +114,8 @@ ll_prepend(ll_t * const list, void * const data)
node->data = data;
node->prev = NULL;
-
node->next = list->head;
+
if (list->head)
list->head->prev = node;
else
@@ -121,7 +123,7 @@ ll_prepend(ll_t * const list, void * const data)
list->head = node;
- ++list->len;
+ ++list->len; /* One more node in the list. */
}
#endif
@@ -148,7 +150,7 @@ ll_insert_before(ll_t * const list, ll_node_t * node, void * const data)
node->prev->next = new_node;
node->prev = new_node;
- ++list->len;
+ ++list->len; /* One more node in the list. */
}
}
#endif
@@ -176,7 +178,7 @@ ll_insert_after(ll_t * const list, ll_node_t * node, void * const data)
node->next->prev = new_node;
node->next = new_node;
- ++list->len;
+ ++list->len; /* One more node in the list. */
}
}
#endif
@@ -187,7 +189,8 @@ ll_insert_after(ll_t * const list, ll_node_t * node, void * const data)
/* http://www.geeksforgeeks.org/quicksort-for-linked-list */
/* ====================================================== */
static ll_node_t *
-ll_partition(ll_node_t * l, ll_node_t * h, int (*comp)(void *, void *),
+ll_partition(ll_node_t * l, ll_node_t * h,
+ int (*comp)(void const *, void const *),
void (*swap)(void **, void **))
{
/* Considers last element as pivot, places the pivot element at its */
@@ -224,7 +227,8 @@ ll_partition(ll_node_t * l, ll_node_t * h, int (*comp)(void *, void *),
/* http://www.geeksforgeeks.org/quicksort-for-linked-list */
/* ======================================================== */
static void
-ll_quicksort(ll_node_t * l, ll_node_t * h, int (*comp)(void *, void *),
+ll_quicksort(ll_node_t * l, ll_node_t * h,
+ int (*comp)(void const *, void const *),
void (*swap)(void **, void **))
{
if (h != NULL && l != h && l != h->next)
@@ -239,7 +243,7 @@ ll_quicksort(ll_node_t * l, ll_node_t * h, int (*comp)(void *, void *),
/* A linked list sort function. */
/* ============================ */
void
-ll_sort(ll_t * list, int (*comp)(void *, void *),
+ll_sort(ll_t * list, int (*comp)(void const *, void const *),
void (*swap)(void **, void **))
{
/* Call the recursive ll_quicksort function. */
@@ -255,6 +259,8 @@ ll_delete(ll_t * const list, ll_node_t * node)
{
if (list->head == list->tail)
{
+ /* We delete the last remaining element from the list. */
+ /* """"""""""""""""""""""""""""""""""""""""""""""""""" */
if (list->head == NULL)
return 0;
@@ -262,23 +268,29 @@ ll_delete(ll_t * const list, ll_node_t * node)
}
else if (node->prev == NULL)
{
+ /* We delete the first element from the list. */
+ /* """""""""""""""""""""""""""""""""""""""""" */
list->head = node->next;
list->head->prev = NULL;
}
else if (node->next == NULL)
{
+ /* We delete the last element from the list. */
+ /* """"""""""""""""""""""""""""""""""""""""" */
list->tail = node->prev;
list->tail->next = NULL;
}
else
{
+ /* We delete an element from the list. */
+ /* """"""""""""""""""""""""""""""""""" */
node->next->prev = node->prev;
node->prev->next = node->next;
}
free(node);
- --list->len;
+ --list->len; /* One less node in the list. */
return 1;
}
diff --git a/list.h b/list.h
index 19efd10..125c318 100644
--- a/list.h
+++ b/list.h
@@ -53,14 +53,14 @@ ll_insert_after(ll_t * const list, ll_node_t * node, void * const data);
#endif
void
-ll_sort(ll_t * list, int (*comp)(void *, void *),
+ll_sort(ll_t * list, int (*comp)(void const *, void const *),
void (*swap)(void **, void **));
int
ll_delete(ll_t * const list, ll_node_t * node);
ll_node_t *
-ll_find(ll_t * const, void * const, int (*)(const void *, const void *));
+ll_find(ll_t * const, void * const, int (*)(void const *, void const *));
void
ll_init(ll_t * list);
diff --git a/smenu.c b/smenu.c
index 0882cd2..a01a203 100644
--- a/smenu.c
+++ b/smenu.c
@@ -852,7 +852,7 @@ make_ini_path(char * name, char * base)
/* Compare the pin order of two pinned word in the output list. */
/* ============================================================ */
int
-tag_comp(void * a, void * b)
+tag_comp(void const * a, void const * b)
{
output_t * oa = (output_t *)a;
output_t * ob = (output_t *)b;
diff --git a/smenu.h b/smenu.h
index 4a73ba2..e4a31aa 100644
--- a/smenu.h
+++ b/smenu.h
@@ -430,7 +430,7 @@ void
help(win_t * win, term_t * term, long last_line);
int
-tag_comp(void * a, void * b);
+tag_comp(void const * a, void const * b);
void
tag_swap(void ** a, void ** b);
diff --git a/utils.c b/utils.c
index eda54f8..f06e015 100644
--- a/utils.c
+++ b/utils.c
@@ -39,7 +39,7 @@ interval_new(void)
/* same return values as for strcmp. */
/* ======================================= */
int
-interval_comp(void * a, void * b)
+interval_comp(void const * a, void const * b)
{
interval_t * ia = (interval_t *)a;
interval_t * ib = (interval_t *)b;
diff --git a/utils.h b/utils.h
index 94d29a3..25e4812 100644
--- a/utils.h
+++ b/utils.h
@@ -33,7 +33,7 @@ interval_t *
interval_new(void);
int
-interval_comp(void * a, void * b);
+interval_comp(void const * a, void const * b);
void
interval_swap(void ** a, void ** b);
diff --git a/xmalloc.h b/xmalloc.h
index d724da1..759568a 100644
--- a/xmalloc.h
+++ b/xmalloc.h
@@ -11,7 +11,7 @@ void *
xmalloc(size_t size);
void *
-xcalloc(size_t num, size_t size);
+xcalloc(size_t n, size_t size);
void *
xrealloc(void * ptr, size_t size);