A singly linked list that implements queue.
Each macro parameters mean:
parameter | description |
---|---|
name |
the name of data structure. |
type |
the type that data structure stores. |
malloc |
memory allocation function. |
free |
memory deallocation function. |
Initializes structures and function prototypes.
#include "sll.h"
INIT_SLL_TYPE(int, int);
Initializes function definitions.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_FUNC(int, int, malloc, free);
Initializes both structures and functions.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_##name sll_##name##_new(void);
Constructs a new, empty linked list.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
sll = sll_int_new();
struct sll_##name sll_##name##_from(const type *arr, const unsigned long len);
Allocate a linked list and fill it by arr
`s items.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 5);
struct sll_##name sll_##name##_copy(const struct sll_##name *sll);
Constructs a deeply copied linked list from given sll
.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll1, sll2;
int arr[] = { 0, 1, 2, 3, 4 };
sll1 = sll_int_from(arr, 5);
sll2 = sll_int_copy(&sll2);
int sll_##name##_push(struct sll_##name *sll, const type val);
Appends an element val
to the back of the sll
. It returns -1 as error if
memory allocation is failed.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 5);
sll_int_push(&sll, 5);
int sll_##name##_pop(struct sll_##name *sll, type *val);
Removes the first element from the sll
and assigns into val
. It returns -1
as error if memory deallocation is failed.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
int val;
sll = sll_int_from(arr, 5);
sll_int_pop(&sll, &val);
int sll_##name##_get(struct sll_##name *sll, type *val, const unsigned long idx);
Assigns the element at idx
into val
in sll
. It returs -1 as error if the
idx
is out of range.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
int val;
sll = sll_int_from(arr, 5);
sll_int_get(&sll, &val, 3);
int sll_##name##_set(struct sll_##name *sll, const type val, const unsigned long len);
Assigns the val
at idx
in sll
. It returns -1 as error if the idx
is out
of range.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 5);
sll_int_get(&sll, 0, 3);
int sll_##name##_append(struct sll_##name *sll, const type *arr, const unsigned long len);
Appends an arr
to the back of the sll
. It returns -1 as error if memory
allocation is failed.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 5);
sll_int_append(&sll, arr, 5);
int sll_##name##_insert(struct sll_##name *sll, const type val, const unsigned long idx);
Inserts the val
to the idx
in sll
. It returns -1 as error if idx
is out
of range or memory allocation is failed.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 3, 4 };
sll = sll_int_from(arr, 4);
sll_int_insert(&sll, 2, 2);
int sll_##name##_remove(struct sll_##name *sll, type *val, const unsigned long idx);
Removes the element at the idx
and assigns it into val
. It returns -1 as
error if idx
is out of range or memory deallocation is failed.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
int val;
sll = sll_int_from(arr, 4);
sll_int_remove(&sll, &val, 2);
int sll_##name##_shrink(struct sll_##name *sll, const unsigned long len);
Shrinks the length of sll
to len
. It returns -1 as erro if len
is greater
than the length of sll
or memory deallocation is failed.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 4);
sll_int_shrink(&sll, 3);
unsigned long sll_##name##_len(struct sll_##name *sll);
Returns the length of sll
.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
unsigned long len;
sll = sll_int_from(arr, 4);
len = sll_int_len(&sll);
void sll_##name##_free(struct sll_##name *sll);
Deallocates the sll
from memory and fills NULL and zeros into the internal
fields to prevent use-after-free.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 4);
sll_int_free(&sll);
struct sll_##name##_iter sll_##name##_iter(struct sll_##name *sll);
Constructs an iterator of sll
.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
struct sll_int_iter iter;
int arr[] = { 0, 1, 2, 3, 4 };
sll = sll_int_from(arr, 5);
iter = sll_int_iter(&sll);
int sll_##name##_next(struct sll_##name##_iter *iter, type *val);
Assigns the next element in sll
into val
. It returns -1 as error if the end
of range is reached.
#include "sll.h"
#include <stdlib.h>
INIT_SLL_BOTH(int, int, malloc, free);
struct sll_int sll;
struct sll_int_iter iter;
int arr[] = { 0, 1, 2, 3, 4 };
int val;
sll = sll_int_from(arr, 5);
iter = sll_int_iter(&sll);
sll_int_next(&iter, &val);