[352] | 1 | /*
|
---|
| 2 | * Copyright (C) 2009 by Matthias Ringwald
|
---|
| 3 | *
|
---|
| 4 | * Redistribution and use in source and binary forms, with or without
|
---|
| 5 | * modification, are permitted provided that the following conditions
|
---|
| 6 | * are met:
|
---|
| 7 | *
|
---|
| 8 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 9 | * notice, this list of conditions and the following disclaimer.
|
---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 12 | * documentation and/or other materials provided with the distribution.
|
---|
| 13 | * 3. Neither the name of the copyright holders nor the names of
|
---|
| 14 | * contributors may be used to endorse or promote products derived
|
---|
| 15 | * from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
---|
| 18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
---|
| 21 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
---|
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
---|
| 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
---|
| 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
---|
| 27 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 28 | * SUCH DAMAGE.
|
---|
| 29 | *
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | /*
|
---|
| 33 | * linked_list.h
|
---|
| 34 | *
|
---|
| 35 | * Created by Matthias Ringwald on 7/13/09.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #ifndef __LINKED_LIST_H
|
---|
| 39 | #define __LINKED_LIST_H
|
---|
| 40 |
|
---|
| 41 | #if defined __cplusplus
|
---|
| 42 | extern "C" {
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 | typedef struct linked_item {
|
---|
| 46 | struct linked_item *next; // <-- next element in list, or NULL
|
---|
| 47 | void *user_data; // <-- pointer to struct base
|
---|
| 48 | } linked_item_t;
|
---|
| 49 |
|
---|
| 50 | typedef linked_item_t * linked_list_t;
|
---|
| 51 |
|
---|
| 52 | typedef struct {
|
---|
| 53 | int advance_on_next;
|
---|
| 54 | linked_item_t * prev; // points to the item before the current one
|
---|
| 55 | linked_item_t * curr; // points to the current item (to detect item removal)
|
---|
| 56 | } linked_list_iterator_t;
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | void linked_item_set_user(linked_item_t *item, void *user_data); // <-- set user data
|
---|
| 60 | void * linked_item_get_user(linked_item_t *item); // <-- get user data
|
---|
| 61 | int linked_list_empty(linked_list_t * list);
|
---|
| 62 | void linked_list_add(linked_list_t * list, linked_item_t *item); // <-- add item to list as first element
|
---|
| 63 | void linked_list_add_tail(linked_list_t * list, linked_item_t *item); // <-- add item to list as last element
|
---|
| 64 | int linked_list_remove(linked_list_t * list, linked_item_t *item); // <-- remove item from list
|
---|
| 65 | linked_item_t * linked_list_get_last_item(linked_list_t * list); // <-- find the last item in the list
|
---|
| 66 |
|
---|
| 67 | //
|
---|
| 68 | // iterator for linked lists. alloes to remove current element. also robust against removal of current element by linked_list_remove
|
---|
| 69 | //
|
---|
| 70 | void linked_list_iterator_init(linked_list_iterator_t * it, linked_list_t * list);
|
---|
| 71 | int linked_list_iterator_has_next(linked_list_iterator_t * it);
|
---|
| 72 | linked_item_t * linked_list_iterator_next(linked_list_iterator_t * it);
|
---|
| 73 | void linked_list_iterator_remove(linked_list_iterator_t * it);
|
---|
| 74 |
|
---|
| 75 | void test_linked_list(void);
|
---|
| 76 |
|
---|
| 77 | #if defined __cplusplus
|
---|
| 78 | }
|
---|
| 79 | #endif
|
---|
| 80 |
|
---|
| 81 | #endif // __LINKED_LIST_H
|
---|