C: more comments in phone number, rename list.h, C99 tests

This commit is contained in:
2021-09-05 18:52:45 +02:00
parent c1a62521c4
commit dd929471f2
4 changed files with 28 additions and 15 deletions

View File

@@ -1 +1 @@
My solutions to some [exercism](https://exercism.io/) exercises. My solutions to some [exercism](https://exercism.org/) exercises.

View File

@@ -1,8 +1,20 @@
#include <malloc.h> #include <malloc.h>
#include "br-list.h" #include "list.h"
#include "linked_list.h" #include "linked_list.h"
/* How it works :
*
* The usual way to use linked lists is to have a list_head whith
* 'next' pointing to first node, and 'prev' pointing to last node.
* Each node has also 'prev' and 'next' pointers.
*
* Here, list_head points to the node own list_head structure,
* and each node list_head points to next/previous node list_head.
*
* Advantage: We don't need to manipulate the pointers, all lists
* use the same code, independently of the object (node) structure.
*/
struct list_node { struct list_node {
ll_data_t data; ll_data_t data;
struct list_head list; struct list_head list;
@@ -40,7 +52,7 @@ size_t list_count(const struct list *list_head)
/* inserts item at back of a list /* inserts item at back of a list
*/ */
void list_push(struct list *list, ll_data_t item_data) void list_push(const struct list *list, const ll_data_t item_data)
{ {
struct list_node *p; struct list_node *p;
@@ -52,7 +64,7 @@ void list_push(struct list *list, ll_data_t item_data)
/* deletes an element /* deletes an element
*/ */
static ll_data_t _list_del(struct list_head *list) static ll_data_t _list_del(const struct list_head *list)
{ {
struct list_node *node; struct list_node *node;
ll_data_t data; ll_data_t data;
@@ -66,14 +78,14 @@ static ll_data_t _list_del(struct list_head *list)
/* removes item from back of a list /* removes item from back of a list
*/ */
ll_data_t list_pop(struct list *list) ll_data_t list_pop(const struct list *list)
{ {
return list_empty((struct list_head*)list) ? -1 : _list_del(list->prev); return list_empty((struct list_head*)list) ? -1 : _list_del(list->prev);
} }
/* inserts item at front of a list /* inserts item at front of a list
*/ */
void list_unshift(struct list *list, ll_data_t item_data) void list_unshift(const struct list *list, const ll_data_t item_data)
{ {
struct list_node *p; struct list_node *p;
@@ -85,14 +97,14 @@ void list_unshift(struct list *list, ll_data_t item_data)
/* removes item from front of a list /* removes item from front of a list
*/ */
ll_data_t list_shift(struct list *list) ll_data_t list_shift(const struct list *list)
{ {
return list_empty((struct list_head*)list) ? -1 : _list_del(list->next); return list_empty((struct list_head*)list) ? -1 : _list_del(list->next);
} }
/* finds a element that matches data /* finds a element that matches data
*/ */
static struct list_node *_node_find(struct list *list, ll_data_t data) static struct list_node *_node_find(const struct list *list, const ll_data_t data)
{ {
struct list_node *p; struct list_node *p;
@@ -105,7 +117,7 @@ static struct list_node *_node_find(struct list *list, ll_data_t data)
/* deletes a node that holds the matching data /* deletes a node that holds the matching data
*/ */
void list_delete(struct list *list, ll_data_t data) void list_delete(const struct list *list, const ll_data_t data)
{ {
struct list_node *p; struct list_node *p;
@@ -118,7 +130,8 @@ void list_delete(struct list *list, ll_data_t data)
*/ */
void list_destroy(struct list *list) void list_destroy(struct list *list)
{ {
struct list_head *cur, *tmp; struct list_head *cur;
__typeof(cur) tmp;
list_for_each_safe(cur, tmp, (struct list_head *)list) list_for_each_safe(cur, tmp, (struct list_head *)list)
_list_del(cur); _list_del(cur);

View File

@@ -13,19 +13,19 @@ struct list *list_create(void);
size_t list_count(const struct list *list); size_t list_count(const struct list *list);
// inserts item at back of a list // inserts item at back of a list
void list_push(struct list *list, ll_data_t item_data); void list_push(const struct list *list, const ll_data_t item_data);
// removes item from back of a list // removes item from back of a list
ll_data_t list_pop(struct list *list); ll_data_t list_pop(const struct list *list);
// inserts item at front of a list // inserts item at front of a list
void list_unshift(struct list *list, ll_data_t item_data); void list_unshift(const struct list *list, const ll_data_t item_data);
// removes item from front of a list // removes item from front of a list
ll_data_t list_shift(struct list *list); ll_data_t list_shift(const struct list *list);
// deletes a node that holds the matching data // deletes a node that holds the matching data
void list_delete(struct list *list, ll_data_t data); void list_delete(const struct list *list, const ll_data_t data);
// destroys an entire list // destroys an entire list
// list will be a dangling pointer after calling this method on it // list will be a dangling pointer after calling this method on it