/************************************************************************/
/* Copyright 2002 MBARI							*/
/************************************************************************/
/* Summary  : Linked List library for OASIS3				*/
/* Filename : list.c							*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : OASIS Mooring Replacement (OASIS3)			*/
/* Revision: 0.1							*/
/* Created  : 10/14/2002						*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/************************************************************************/
/* Modification History:						*/
/* 10oct2002 rah - created from OASIS list.s				*/
/************************************************************************/

#include <mbariTypes.h>			/* MBARI type definitions	    */
#include <mbariConst.h>			/* MBARI constants		    */
#include <list.h>			/* OASIS linked list definitions    */


Void	 list_init( LstHead *lp );
Node	 *list_head( LstHead *lp );
Void	 list_add( LstHead *lp, Node *np );
Node	 *list_get( LstHead *lp, Node *np );
Node	 *list_first( LstHead *lp );
Node	 *list_next( Node *np );


/************************************************************************/
/* Function    : list_init						*/
/* Purpose     : Initialize Linked List					*/
/* Inputs      : Ptr to LstHead						*/
/* Outputs     : None							*/
/************************************************************************/
Void	list_init( LstHead *lp )
{
  lp->lst_head = NULLNODE;
  lp->lst_tail = NULLNODE;

} /* list_init() */


/************************************************************************/
/* Function    : list_head						*/
/* Purpose     : Delete and return head of linked list			*/
/* Inputs      : Ptr to LstHead						*/
/* Outputs     : None							*/
/************************************************************************/
Node	*list_head( LstHead *lp )
{
  Reg Node	*np;

  if ((np = lp->lst_head) == NULLNODE)
    return(NULLNODE);

  lp->lst_head = np->lst_next;

  if (np->lst_next == NULLNODE)
    np->lst_tail = NULLNODE;
  else
    np->lst_next->lst_prev = NULL;

  return(np);

} /* list_head() */


/************************************************************************/
/* Function    : list_add						*/
/* Purpose     : Add a node to the end of a linked list			*/
/* Inputs      : List to add to, node to add				*/
/* Outputs     : None							*/
/************************************************************************/
Void	 list_add( LstHead *lp, Node *np )
{
  np->lst_next = NULLNODE;
  np->lst_prev = lp->lst_tail;

  if (lp->lst_tail == NULLNODE)
    lp->lst_head = np;
  else
    lp->lst_tail->lst_next = np;

  lp->lst_tail = np;

} /* list_add() */


/************************************************************************/
/* Function    : list_get						*/
/* Purpose     : Delete and return a given node from linked list	*/
/* Inputs      : List to get from, node to delete			*/
/* Outputs     : None							*/
/************************************************************************/
Node	 *list_get( LstHead *lp, Node *np )
{
  if (np->lst_prev == NULLNODE)
    lp->lst_head = np->lst_next;
  else
    np->lst_prev->lst_next = np->lst_next;

  if (np->lst_next == NULLNODE)
    lp->lst_tail = np->lst_prev;
  else
    np->lst_next->lst_prev = np->lst_prev;

  return(np);

} /* list_get() */


/************************************************************************/
/* Function    : list_first						*/
/* Purpose     : Return first entry of linked list			*/
/* Inputs      : Ptr to LstHead						*/
/* Outputs     : None							*/
/************************************************************************/
Node	 *list_first( LstHead *lp )
{
    return(lp->lst_head);

} /* list_first() */


/************************************************************************/
/* Function    : list_next						*/
/* Purpose     : Return next entry of linked list			*/
/* Inputs      : Ptr to LstHead						*/
/* Outputs     : None							*/
/************************************************************************/
Node	 *list_next( Node *np )
{
    return(np->lst_next);

} /* list_next() */
