File: list.c

package info (click to toggle)
xsystem35 2.17.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 8,096 kB
  • sloc: ansic: 38,159; java: 4,085; xml: 249; sh: 134; python: 15; makefile: 12
file content (123 lines) | stat: -rw-r--r-- 2,636 bytes parent folder | download | duplicates (2)
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
/*
 * list.c  linked list (for GLib replacement)
 *
 * Copyright (C) 2014-2017 <KichikuouChrome@gmail.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdlib.h>

#include "list.h"

void slist_foreach(SList *list, ListFunc func, void *user_data) {
	for (; list; list = list->next)
		func(list->data, user_data);
}

void slist_free(SList *list) {
	while (list) {
		SList* next = list->next;
		free(list);
		list = next;
	}
}

SList* slist_append(SList *list, void *data) {
	SList* node = malloc(sizeof(SList));
	node->data = data;
	node->next = NULL;

	if (!list)
		return node;

	SList* last = list;
	while (last->next)
		last = last->next;
	last->next = node;
	return list;
}

SList* slist_remove(SList *list, const void *data) {
	if (!list)
		return NULL;

	SList* node = list;
	if (list->data == data) {
		list = list->next;
		free(node);
		return list;
	}

	for (; node->next; node = node->next) {
		if (node->next->data == data) {
			SList* nodeToRemove = node->next;
			node->next = nodeToRemove->next;
			free(nodeToRemove);
			break;
		}
	}
	return list;
}

int slist_index(SList *list, const void *data) {
	int i;
	for (i = 0; list; list = list->next, i++) {
		if (list->data == data)
			return i;
	}
	return -1;
}

unsigned slist_length(SList *list) {
	unsigned len;
	for (len = 0; list; list = list->next)
		len++;
	return len;
}

SList* slist_nth(SList* list, unsigned int n) {
	while (list && n--)
		list = list->next;
	return list;
}

SList* slist_last(SList *list) {
	if (list) {
		while (list->next)
			list = list->next;
	}
	return list;
}

SList* slist_insert_sorted(SList *list, void* data, CompareFunc func) {
	SList* node = malloc(sizeof(SList));
	node->data = data;

	SList* prev = NULL;
	SList* current = list;
	while (current && func(current->data, data) <= 0) {
		prev = current;
		current = current->next;
	}
	if (!prev) {
		node->next = list;
		return node;
	}
	prev->next = node;
	node->next = current;
	return list;
}