nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
alist.c
Go to the documentation of this file.
1 #include <stddef.h>
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 
7 #include "alist.h"
8 
23 struct alist {
24  struct alist *next;
25  struct alist *prev;
26 };
27 
28 struct alist *alist_append(struct alist *list, struct alist *new)
29 {
30  new->next = NULL;
31 
32  if (!list) {
33  new->prev = new;
34 
35  list = new;
36  } else {
37  new->prev = list->prev;
38  new->prev->next = new;
39  list->prev = new;
40  }
41 
42  return list;
43 }
44 
45 struct alist *alist_prepend(struct alist *list, struct alist *new)
46 {
47  if (!list) {
48  new->prev = new;
49  new->next = NULL;
50  } else {
51  if (list->prev->next) {
52  list->prev->next = new;
53  }
54 
55  new->prev = list->prev;
56  new->next = list;
57 
58  list->prev = new;
59  }
60 
61  return new;
62 }
63 
64 struct alist *alist_remove(struct alist *list, struct alist *l)
65 {
66  if (!list || !l) {
67  return NULL;
68  }
69 
70  if (l == list) {
71  list = l->next;
72  } else {
73  l->prev->next = l->next;
74  }
75 
76  if (l->next) {
77  l->next->prev = l->prev;
78  }
87  else if (list) {
88  list->prev = l->prev;
89  }
90 
91  return list;
92 }
93 
94 struct alist *alist_next(struct alist *l)
95 {
96  return l ? l->next : NULL;
97 }
98 
99 struct alist *alist_prev(struct alist *l)
100 {
101  return l ? l->prev : NULL;
102 }
103 
104 int alist_count(struct alist *l)
105 {
106  register int i;
107  struct alist *n;
108 
109  i = 0;
110  alist_foreach(l, n) {
111  i++;
112  }
113 
114  return i;
115 }
116 
117 struct alist *alist_nth(struct alist *l, int nth)
118 {
119  register int i;
120  struct alist *n;
121 
122  i = 0;
123  for (n = l; n; n = n->next) {
124  if (i == nth) {
125  return n;
126  }
127  i++;
128  }
129 
130  return NULL;
131 }
132 
133 /* End of a file */