• Stars
    star
    429
  • Rank 98,106 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created almost 14 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C doubly linked list

list

C doubly linked list implementation.

API

Below is the public api currently provided by "list".

list_t *list_new();

Allocate and initialize a list.

list_t *mylist = list_new();

list_node_t *list_node_new(void *val)

Allocate and initialize a list_node_t with the given val.

list_node_t *node = list_node_new("my value");
node->val; // "my value"

list_node_t * list_rpush(list_t *self, list_node_t *node)

Append node to self, returning node.

list_rpush(list, list_node_new("value"));
list->tail->val; // "value"

list_node_t * list_rpop(list_t *self)

Return / detach node from the end of the list, or NULL.

list_node_t *last = list_rpop(names);

list_node_t *list_lpush(list_t *self, list_node_t *node)

Prepend node to self, returning node.

list_lpush(list, list_node_new("value"));
list->head->val; // "value"

list_node_t *list_find(list_t *self, void *val)

Return the list_node_t containing val or NULL.

list_node_t *node = list_find(list, "some value");

list_node_t *list_at(list_t *self, int index)

Return the list_node_t at the given index, where index may also be a negative integer indicating an index from the list tail.

list_at(list, 0);  // first
list_at(list, 1);  // second
list_at(list, -1); // last
list_at(list, -3); // third last

void list_remove(list_t *self, list_node_t *node)

Remove node from the list, freeing it and it's value.

list_remove(list, node);

void list_destroy(list_t *self)

Free the list and all nodes.

list_destroy(list);

list_iterator_t *list_iterator_new(list_t *list, list_direction_t direction)

Allocate and initialize a list_iterator_t with the given direction, where direction may be LIST_HEAD or LIST_TAIL.

list_node_t *node;
list_iterator_t *it = list_iterator_new(list, LIST_HEAD);
while ((node = list_iterator_next(it))) {
  puts(node->val);
}

list_node_t *list_iterator_next(list_iterator_t *self)

Return the next list_node_t or NULL.

void list_iterator_destroy(list_iterator_t *self);

Free the iterator only.

list_iterator_destroy(it);

Examples

list iteration:

list_t *langs = list_new();

list_node_t *c = list_rpush(langs, list_node_new("c"));
list_node_t *js = list_rpush(langs, list_node_new("js"));
list_node_t *ruby = list_rpush(langs, list_node_new("ruby"));

list_node_t *node;
list_iterator_t *it = list_iterator_new(langs, LIST_HEAD);
while ((node = list_iterator_next(it))) {
  puts(node->val);
}

list_iterator_destroy(it);
list_destroy(langs);

stdout:

c
js
ruby

Benchmarks

$ make benchmark

 10,000,000 nodes

              lpush: 0.5000s
              rpush: 0.5000s
               lpop: 0.0312s
               rpop: 0.0312s
   find (last node): 0.0312s
            iterate: 0.0625s
        at(100,000): 0.0000s
      at(1,000,000): 0.0000s
       at(-100,000): 0.0000s

Testing

$ make test

License

(The MIT License)

Copyright (c) 2009-2010 TJ Holowaychuk <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

clib

Package manager for the C programming language.
C
4,748
star
2

entr

A utility for running arbitrary commands when files change. Uses kqueue(2) or inotify(7) to avoid polling. entr responds to file system events by executing command line arguments or by writing to a FIFO. entr was written to provide to make rapid feedback and automated testing natural and completely ordinary.
C
593
star
3

cmocka

An elegant unit testing framework for C with support for mock objects.
C
213
star
4

commander

Commander option parser ported to C - simple API, auto-generated --help
C
203
star
5

buffer

Tiny C string library
C
194
star
6

hash

C hash implementation based on khash
C
168
star
7

sha1

sha1 hash alogrithm
C
163
star
8

coro

Coroutines in C
C
120
star
9

flag

Go-style flag parsing for C
C
117
star
10

lmdb

Symas Lightning Memory-Mapped Database (LMDB)
77
star
11

red-black-tree

Generic red-black tree library (by Julienne Walker).
C
54
star
12

uv

A clib pointer to libuv: Cross-platform asychronous I/O http://libuv.org/
45
star
13

net

extends libuv to support tls/ssl in a simpler API for clibs
C
35
star
14

amp

Abstract Message Protocol C implementation
C
30
star
15

timer

Timer with microsecond resolution
C
30
star
16

logfmt

Fast C logfmt parser.
C
29
star
17

term

Terminal goodies
C
27
star
18

dumpasn1

Display the contents of ASN.1 encoded data
C
23
star
19

int

Binary integer representation utilities.
C
23
star
20

mt19937ar

Mersenne Twister random number generator
C
23
star
21

rle

Run-length encoding.
C
20
star
22

rgba

RGB / RGBA parsing / formatting
C
20
star
23

readline

Tiny C readline library, note: this is not used in CLI-readline's one.
C
19
star
24

inih

Simple .INI file parser
C
19
star
25

container_of

Obtain a pointer to the struct that contains the struct member
C
19
star
26

wildcardcmp

Simple wildcard string comparison for C
C
18
star
27

ms

C str to us / ms utilities
C
18
star
28

debug

Conditional debug logging for C
C
17
star
29

unlikely

gcc branch prediction macros
C
16
star
30

zerodbg

Useful macros for debugging purposes.
C
16
star
31

bytes

byte <> string conversion utilities
C
14
star
32

which

Lookup executable via PATH or given path string
C
13
star
33

check

Super basic CHECK(), DCHECK(), and NOTREACHED() macro implementations.
Shell
12
star
34

strdup

Drop-in replacement for strdup(3) from libc
C
12
star
35

clib-uninstall

clib(1) plugin for uninstalling executables
C
11
star
36

module

A set of macros and functions to make defining a C module easier
C
11
star
37

trigger

Simple event handling library.
C
11
star
38

timestamp

C timestamp util
C
11
star
39

leveldb

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
C++
11
star
40

file

File utilities
C
10
star
41

bench

get wall and cpu time for benchmarking
C
9
star
42

clib-validate

clib(1) plugin for validating a package.json
C
9
star
43

clib-makefile

Interactively generate a `Makefile' for your C lib project with clibs in mind
Shell
9
star
44

sqlite

Embedded SQL database
C
8
star
45

logger

Logger used internally by clib(1)
C
8
star
46

strndup

Safe version of strndup
C
8
star
47

graph

Simple (directed) graph library.
C
7
star
48

clib-package

Internal bits of clib-install(1) for fetching packages
C
5
star
49

build_assert

Routines for build-time assertions
3
star
50

website

The source code for clibs.org.
TypeScript
3
star
51

tig

A clib pointer to https://github.com/jonas/tig
2
star
52

jsmn

A clib pointer to https://github.com/zserge/jsmn: Jsmn is a world fastest JSON parser/tokenizer.
2
star
53

sodium

A clib pointer to libsodium: A modern, portable, easy to use crypto library.
1
star
54

buffet

A clib pointer to https://github.com/alcover/buffet
1
star
55

discount

A clib (clib@next) pointer to https://github.com/Orc/discount
Shell
1
star
56

semver

A clib (clib@next) pointer to https://github.com/EddyLuten/lib-semver
JavaScript
1
star
57

packcc

A clib pointer for https://github.com/arithy/packcc
Shell
1
star