File: list.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (134 lines) | stat: -rw-r--r-- 2,822 bytes parent folder | download | duplicates (7)
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
#include <stdlib.h>

static LST(node_t) *LST(create)(LST_ITEM_T *item, LST(node_t) *next)
{
	LST(node_t) *new_node = (LST(node_t)*) malloc(sizeof(LST(node_t)));
	new_node->item = *item;
	new_node->next = next;
	return new_node;
}

LST(node_t) *LST(prepend)(LST(node_t) *list, LST_ITEM_T *item)
{
	LST(node_t) *new_head = LST(create)(item, list);
	return new_head;
}

LST(node_t) *LST(insert_after)(LST(node_t) *node, LST_ITEM_T *item)
{
	LST(node_t) *new_node;
	if (node == NULL)
		return NULL;
	new_node = LST(create)(item, node->next);
	node->next = new_node;
	return new_node;
}

LST(node_t) *LST(insert_after_nth)(LST(node_t) *list, int n, LST_ITEM_T *item)
{
	return LST(insert_after)(LST(nth)(list, n), item);
}

LST(node_t) *LST(remove_front)(LST(node_t) *list)
{
	LST(node_t) *new_head;
	if (list == NULL)
		return NULL;
	new_head = list->next;
	free(list);
	return new_head;
}

LST(node_t) *LST(remove)(LST(node_t) *list, LST(node_t) *node)
{
	LST(node_t) *prev_node;
	if (list == NULL)
		return NULL;
	if (list == node)
		return LST(remove_front)(list);
  for (prev_node = list; prev_node->next != NULL; prev_node = prev_node->next) {
		if (prev_node->next == node) {
			prev_node->next = node->next;
			free(node);
			break;
		}
	}
	return list;
}

LST(node_t) *LST(remove_item)(LST(node_t) *list, LST_ITEM_T *item)
{
	LST(node_t) *prev_node, *node;
	if (list == NULL)
		return NULL;
	if (LST(compare_func)(&list->item, item))
		return LST(remove_front)(list);
  for (prev_node = list, node = prev_node->next; node != NULL; prev_node = prev_node->next, node = node->next) {
		if (LST(compare_func)(&node->item, item)) {
			prev_node->next = node->next;
			free(node);
			break;
		}
	}
	return list;
}

LST(node_t) *LST(find)(LST(node_t) *list, LST_ITEM_T *item)
{
	LST(node_t) *node;
  for (node = list; node != NULL; node = node->next) {
		if (LST(compare_func)(&node->item, item)) {
			return node;
		}
	}
	return NULL;
}

LST(node_t) *LST(nth)(LST(node_t) *list, int n)
{
	int i;
  for (i = 0; i < n && list != NULL; i++)
		list = list->next;
	return list;
}

LST(node_t) *LST(last)(LST(node_t) *list)
{
	if (list == NULL)
		return NULL;
	while (list->next != NULL)
		list = list->next;
	return list;
}

size_t LST(length)(LST(node_t) *list)
{
	size_t len = 0;
	if (list == NULL)
		return 0;
	for (; list != NULL; list = list->next)
		len++;
	return len;
}

int LST(get_index)(LST(node_t) *list, LST(node_t) *node)
{
	int i;
	if (list == NULL)
		return -1;
	for (i = 0; list != node && list != NULL; list = list->next)
		i++;
	if (list == NULL)
		return -1;
	return i;
}

void LST(free)(LST(node_t) *list)
{
	LST(node_t) *node, *next_node;
	if (list == NULL)
		return;
  for (node = list, next_node = node->next; next_node != NULL; node = next_node, next_node = next_node->next)
		free(node);
	free(node);
}