shithub: ircs

ref: 9e1f7d750e447544d6893712d1bc25ba5d93e010
dir: /list.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"

int listallocs;

void
listfree(List **lp)
{
	List *l, *n;

	for(l = *lp; l != nil; l = n){
		n = l->next;
		free(l);
		listallocs--;
	}
	*lp = nil;
}

int
listadd(List **lp, void *val)
{
	List *l;

	for(l = *lp; l != nil; l = l->next)
		if(l->val == val)
			break;
	if(l != nil)
		return 0;
	l = mallocz(sizeof(List), 1);
	if(l == nil)
		sysfatal("mallocz: %r");
	listallocs++;
	l->val = val;
	l->next = *lp;
	*lp = l;
	return 1;
}

int
listdel(List **lp, void *val)
{
	List *l, *prev;

	prev = nil;
	for(l = *lp; l != nil; l = l->next){
		if(l->val == val)
			break;
		prev = l;
	}
	if(l == nil)
		return 0;
	if(prev == nil)
		*lp = l->next;
	else
		prev->next = l->next;
	free(l);
	listallocs--;
	return 1;
}