summaryrefslogtreecommitdiffstats
path: root/link.c
diff options
context:
space:
mode:
Diffstat (limited to 'link.c')
-rw-r--r--link.c64
1 files changed, 55 insertions, 9 deletions
diff --git a/link.c b/link.c
index 60e701d..26372e7 100644
--- a/link.c
+++ b/link.c
@@ -1,30 +1,50 @@
#include "link.h"
+#include "util.h"
int
-list_len(struct link *list)
+list_len(struct link *head)
{
struct link *iter;
int len;
+ ASSERT(head != NULL);
+
len = 0;
- for (iter = list; iter; iter = iter->next)
+ for (iter = head->next; iter; iter = iter->next)
len += 1;
return len;
}
+int
+list_ffind(struct link *head, struct link *link)
+{
+ struct link *iter;
+
+ ASSERT(head != NULL);
+
+ for (iter = head->next; iter && iter != link; iter = iter->next);
+
+ return (iter == link);
+}
+
struct link *
-list_back(struct link *link)
+link_back(struct link *link)
{
+ ASSERT(link != NULL);
+
for (; link->next; link = link->next);
+
return link;
}
void
-link_prepend(struct link *list, struct link *link)
+link_prepend(struct link *cur, struct link *link)
{
- link->prev = list->prev;
- link->next = list;
+ ASSERT(cur != NULL && link != NULL);
+
+ link->prev = cur->prev;
+ link->next = cur;
if (link->prev)
link->prev->next = link;
@@ -33,10 +53,12 @@ link_prepend(struct link *list, struct link *link)
}
void
-link_append(struct link *list, struct link *link)
+link_append(struct link *cur, struct link *link)
{
- link->prev = list;
- link->next = list->next;
+ ASSERT(cur != NULL && link != NULL);
+
+ link->prev = cur;
+ link->next = cur->next;
if (link->prev)
link->prev->next = link;
@@ -47,8 +69,32 @@ link_append(struct link *list, struct link *link)
void
link_pop(struct link *link)
{
+ ASSERT(link != NULL);
+
if (link->prev)
link->prev->next = link->next;
if (link->next)
link->next->prev = link->prev;
}
+
+struct link *
+link_iter(struct link *link, int n)
+{
+ int i;
+
+ for (i = 0; i < n; i++) {
+ if (!link) return NULL;
+ link = link->next;
+ }
+
+ return link;
+}
+
+void
+link_push_back(struct link *cur, struct link *link)
+{
+ struct link *back;
+
+ back = link_back(cur);
+ link_append(back, link);
+}