/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 *
 * Function to manage doubled linked list
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */




#include "stonage.h"
#include "list.h"

/**
 * List allocator
 * @return a Linked list
 */

List *
List_Alloc(void)
{
  List *chunk;
  chunk = (List *)malloc(sizeof(List));
  chunk->data=NULL;
  chunk->next=chunk;
  chunk->prev=chunk;

  return chunk;
}

/**
 * Add this element to the list
 * @param list pointer to the old list
 * @param data pointer to some element to add to the list
 * @return the new adress of the list
 */
List *
List_Append(List *l, void *data)
{
  List *nl;
  List *last;

  nl = List_Alloc();
  nl->data = data;

  if (l==NULL)
    return nl;

  last = l->prev;
  last->next=nl;
  nl->prev=last;
  l->prev=nl;
  nl->next=l;

  return l;
}


void
List_Print(const List *ref)
{
  const List *l=ref;
  do
   {
     trace("addr=%x,data=%x,prev=%x,next=%x",(int)l,(int)l->data,(int)l->prev,(int)l->next);
     l=l->next;
   } while (l!=ref);
}


