add linked list code
This commit is contained in:
parent
21de678ec1
commit
e8d87d3d37
4 changed files with 177 additions and 0 deletions
15
Makefile
Normal file
15
Makefile
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
CFLAGS := -ggdb3 -O2 -Wall -I.
|
||||||
|
|
||||||
|
|
||||||
|
LIBIFUPDOWN_SRC = \
|
||||||
|
libifupdown/list.c
|
||||||
|
LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o}
|
||||||
|
|
||||||
|
|
||||||
|
libifupdown.a: ${LIBIFUPDOWN_OBJ}
|
||||||
|
ar -rcs $@ ${LIBIFUPDOWN_OBJ}
|
||||||
|
|
||||||
|
all: libifupdown.a
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f ${LIBIFUPDOWN_OBJ}
|
21
libifupdown/libifupdown.h
Normal file
21
libifupdown/libifupdown.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* libifupdown/libifupdown.h
|
||||||
|
* Purpose: main header file for libifupdown
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* This software is provided 'as is' and without any warranty, express or
|
||||||
|
* implied. In no event shall the authors be liable for any damages arising
|
||||||
|
* from the use of this software.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBIFUPDOWN_LIBIFUPDOWN_H__GUARD
|
||||||
|
#define LIBIFUPDOWN_LIBIFUPDOWN_H__GUARD
|
||||||
|
|
||||||
|
#include "libifupdown/list.h"
|
||||||
|
|
||||||
|
#endif
|
99
libifupdown/list.c
Normal file
99
libifupdown/list.c
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
/*
|
||||||
|
* libifupdown/list.c
|
||||||
|
* Purpose: linked lists
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* This software is provided 'as is' and without any warranty, express or
|
||||||
|
* implied. In no event shall the authors be liable for any damages arising
|
||||||
|
* from the use of this software.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "libifupdown/list.h"
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
struct lif_node {
|
||||||
|
struct lif_node *prev, *next;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lif_list {
|
||||||
|
struct lif_node *head, *tail;
|
||||||
|
size_t length;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LIF_LIST_FOREACH(iter, head) \
|
||||||
|
for ((iter) = (head); (iter) != NULL; (iter) = (iter)->next)
|
||||||
|
|
||||||
|
#define LIF_LIST_FOREACH_SAFE(iter, iter_next, head) \
|
||||||
|
for ((iter) = (head); (iter) != NULL; (iter) = (iter_next), (iter_next) = (iter) != NULL ? (iter)->next : NULL)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
lif_node_insert(struct lif_node *node, void *data, struct lif_list *list)
|
||||||
|
{
|
||||||
|
struct lif_node *tnode;
|
||||||
|
|
||||||
|
node->data = data;
|
||||||
|
|
||||||
|
if (list->head == NULL)
|
||||||
|
{
|
||||||
|
list->head = list->tail = node;
|
||||||
|
list->length = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tnode = list->head;
|
||||||
|
|
||||||
|
node->next = tnode;
|
||||||
|
tnode->prev = node;
|
||||||
|
|
||||||
|
list->head = node;
|
||||||
|
list->length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
lif_node_insert_tail(struct lif_node *node, void *data, struct lif_list *list)
|
||||||
|
{
|
||||||
|
struct lif_node *tnode;
|
||||||
|
|
||||||
|
node->data = data;
|
||||||
|
|
||||||
|
if (list->tail == NULL)
|
||||||
|
{
|
||||||
|
list->head = list->tail = node;
|
||||||
|
list->length = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tnode = list->tail;
|
||||||
|
|
||||||
|
node->prev = tnode;
|
||||||
|
tnode->next = node;
|
||||||
|
|
||||||
|
list->tail = node;
|
||||||
|
list->length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
lif_node_delete(struct lif_node *node, struct lif_list *list)
|
||||||
|
{
|
||||||
|
list->length--;
|
||||||
|
|
||||||
|
if (node->prev == NULL)
|
||||||
|
list->head = node->next;
|
||||||
|
else
|
||||||
|
node->prev->next = node->next;
|
||||||
|
|
||||||
|
if (node->next == NULL)
|
||||||
|
list->tail = node->prev;
|
||||||
|
else
|
||||||
|
node->next->prev = node->prev;
|
||||||
|
}
|
42
libifupdown/list.h
Normal file
42
libifupdown/list.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* libifupdown/list.h
|
||||||
|
* Purpose: linked lists
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* This software is provided 'as is' and without any warranty, express or
|
||||||
|
* implied. In no event shall the authors be liable for any damages arising
|
||||||
|
* from the use of this software.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBIFUPDOWN_LIST_H__GUARD
|
||||||
|
#define LIBIFUPDOWN_LIST_H__GUARD
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
struct lif_node {
|
||||||
|
struct lif_node *prev, *next;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lif_list {
|
||||||
|
struct lif_node *head, *tail;
|
||||||
|
size_t length;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern void lif_node_insert(struct lif_node *node, void *data, struct lif_list *list);
|
||||||
|
extern void lif_node_insert_tail(struct lif_node *node, void *data, struct lif_list *list);
|
||||||
|
extern void lif_node_delete(struct lif_node *node, struct lif_list *list);
|
||||||
|
|
||||||
|
#define LIF_LIST_FOREACH(iter, head) \
|
||||||
|
for ((iter) = (head); (iter) != NULL; (iter) = (iter)->next)
|
||||||
|
|
||||||
|
#define LIF_LIST_FOREACH_SAFE(iter, iter_next, head) \
|
||||||
|
for ((iter) = (head); (iter) != NULL; (iter) = (iter_next), (iter_next) = (iter) != NULL ? (iter)->next : NULL)
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue