C: more comments in phone number, rename list.h, C99 tests
This commit is contained in:
@@ -1 +1 @@
|
||||
My solutions to some [exercism](https://exercism.io/) exercises.
|
||||
My solutions to some [exercism](https://exercism.org/) exercises.
|
||||
|
@@ -1,8 +1,20 @@
|
||||
#include <malloc.h>
|
||||
|
||||
#include "br-list.h"
|
||||
#include "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 {
|
||||
ll_data_t data;
|
||||
struct list_head list;
|
||||
@@ -40,7 +52,7 @@ size_t list_count(const struct list *list_head)
|
||||
|
||||
/* 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;
|
||||
|
||||
@@ -52,7 +64,7 @@ void list_push(struct list *list, ll_data_t item_data)
|
||||
|
||||
/* 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;
|
||||
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
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
@@ -85,14 +97,14 @@ void list_unshift(struct list *list, ll_data_t item_data)
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
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;
|
||||
|
||||
@@ -118,7 +130,8 @@ void list_delete(struct list *list, ll_data_t data)
|
||||
*/
|
||||
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_del(cur);
|
||||
|
@@ -13,19 +13,19 @@ struct list *list_create(void);
|
||||
size_t list_count(const struct list *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
|
||||
ll_data_t list_pop(struct list *list);
|
||||
ll_data_t list_pop(const struct list *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
|
||||
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
|
||||
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
|
||||
// list will be a dangling pointer after calling this method on it
|
||||
|
Reference in New Issue
Block a user