/* -*- c -*-
 * Time-stamp: <2005-04-14 23:23:25 rsmith>
 * 
 * list/list.h
 * Copyright (C) 2002--2005 R.F. Smith <rsmith@xs4all.nl>. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */


/* If you don't want assertions, #define NDEBUG */
/*#define NDEBUG*/

#include <assert.h>
#include "list.h"

/* This enables you to see which files have been used in building a
 * binary. N.B, __attribute is a GCC extension, so we test for __GNUC__. */
#ifdef __GNUC__
static char *SCMID __attribute__ ((unused)) = 
"$Id: list.c 33 2005-04-16 21:47:48Z rsmith $";
#endif

struct list *list_next(struct list *l)
{
	if (l == NULL)
		return NULL;
	return l->next;
}

struct list *list_prev(struct list *l)
{
	if (l == NULL)
		return NULL;
	return l->prev;
}

struct list *list_head(struct list *l)
{
	if (l == NULL)
		return NULL;
	while (l->prev != NULL) {
		l = l->prev;
	}
	return l;
}

struct list *list_tail(struct list *l)
{
	if (l == NULL)
		return NULL;
	while (l->next != NULL) {
		l = l->next;
	}
	return l;
}

struct list *list_nth(struct list *l, int n)
{
	if (l == NULL)
		return NULL;
	assert(n>=0);	/* extra warning */
	if (n < 0)
		return NULL;
	l = list_head(l);
	if (n == 0) {
		return l;
	}
	while (n > 0 && l != NULL) {
		l = list_next(l);
		n--;
	}
	return l;
}

struct list *list_append(struct list *l, struct list *newitem)
{
	struct list *head = list_head(l);
	assert(newitem!=NULL);
	l = list_tail(l);
	if (l != NULL) {
		l->next = newitem;
		newitem->prev = l;
	} else {	/* l is NULL */
		newitem->prev = NULL;
		head = newitem;
	}
	newitem->next = NULL;
	return head;
}

struct list *list_prepend(struct list *l, struct list *newitem)
{
	assert(newitem!=NULL);
	l = list_head(l);
	if (l != NULL) {
		l->prev = newitem;
		newitem->next = l;
	} else {	/* l is NULL */
		newitem->next = NULL;
	}
	newitem->prev = NULL;
	return newitem;
}
/* EOF list.c$ */


syntax highlighted by Code2HTML, v. 0.9.1