ref: cfd7660a044c8bd3ad0c33a237155b05248c0b0b
dir: /list.c/
#include <u.h>
#include <libc.h>
#include "ds.h"
#include "list.h"
// Return an allocated, empty, list.
List*
mklist(void)
{
List *l = ecalloc(1, sizeof (List));
l->size = 0;
return l;
}
// Prepend to the front of the list.
List*
listprepend(List *l, void *datum)
{
if(l == nil)
// Nil pointer
return l;
ListNode *n = ecalloc(1, sizeof (ListNode));
if(l->head == nil){
// Empty list, be the head
l->head = n;
}else{
// Replace head
n->next = l->head;
l->head = n;
}
l->head->datum = datum;
l->size++;
return l;
}
// Append to the end of the list.
List*
listappend(List *l, void *datum)
{
if(l == nil)
// Nil pointer
return l;
ListNode *n = ecalloc(1, sizeof (ListNode));
if(l->head == nil){
// Empty list, be the head
l->head = n;
}else{
// Replace tail
ListNode *ln;
for(ln = l->head; ln->next != nil; ln = ln->next)
;
ln->next = n;
n->next = nil;
}
n->datum = datum;
l->size++;
return l;
}