aboutsummaryrefslogtreecommitdiffstats
path: root/src/list.h
blob: 0b43a2b39c0f3400e7af34019b4522e3301f4ed1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
 *
 * cpulimit - a CPU limiter for Linux
 *
 * Copyright (C) 2005-2012, by:  Angelo Marletta <angelo dot marletta at gmail dot com> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef __LIST__

#define __LIST__

#ifndef  TRUE
    #define TRUE 1
    #define FALSE 0
#endif

struct list_node {
    //pointer to the content of the node
    void *data;
    //pointer to previous node
    struct list_node *previous;
    //pointer to next node
    struct list_node *next;
};

struct list {
    //first node
    struct list_node *first;
    //last node
    struct list_node *last;
    //size of the search key in bytes
    int keysize;
    //element count
    int count;
};

/*
 * Initialize a list, with a specified key size
 */
void init_list(struct list *l,int keysize);

/*
 * Add a new element at the end of the list
 * return the pointer to the new node
 */
struct list_node *add_elem(struct list *l,void *elem);

/*
 * Delete a node
 */
void delete_node(struct list *l,struct list_node *node);

/*
 * Delete a node from the list, even the content pointed by it
 * Use only when the content is a dynamically allocated pointer
 */
void destroy_node(struct list *l,struct list_node *node);

/*
 * Check whether a list is empty or not
 */
int is_empty_list(struct list *l);

/*
 * Return the element count of the list
 */
int get_list_count(struct list *l);

/*
 * Return the first element (content of the node) from the list
 */
void *first_elem(struct list *l);

/*
 * Return the first node from the list
 */
struct list_node *first_node(struct list *l);

/*
 * Return the last element (content of the node) from the list
 */
void *last_elem(struct list *l);

/*
 * Return the last node from the list
 */
struct list_node *last_node(struct list *l);

/*
 * Search an element of the list by content
 * the comparison is done from the specified offset and for a specified length
 * if offset=0, the comparison starts from the address pointed by data
 * if length=0, default keysize is used for length
 * if the element is found, return the node address
 * else return NULL
 */
struct list_node *xlocate_node(struct list *l,void *elem,int offset,int length);

/*
 * The same of xlocate_node(), but return the content of the node
 */
void *xlocate_elem(struct list *l,void *elem,int offset,int length);

/*
 * The same of calling xlocate_node() with offset=0 and length=0
 */
struct list_node *locate_node(struct list *l,void *elem);

/*
 * The same of locate_node, but return the content of the node
 */
void *locate_elem(struct list *l,void *elem);

/*
 * Delete all the elements in the list
 */
void clear_list(struct list *l);

/*
 * Delete every element in the list, and free the memory pointed by all the node data
 */
void destroy_list(struct list *l);

#endif
Un proyecto texto-plano.xyz