#include <stdlib.h>
#include "dll.h"

void list_init(struct list_head *head)
{
	head->next=head;
	head->prev=head;
}

void list_add(struct list_head *new, struct list_head *head)
{
	new->next=head->next;
	head->next=new;
	new->prev=head;
	new->next->prev=new;
}

void list_add_tail(struct list_head *new, struct list_head *head)
{
        new->prev=head->prev;
	head->prev=new;
        new->next=head;
	new->prev->next=new;
}

struct list_head* list_del(struct list_head *entry)
{
	entry->prev->next=entry->next;
	entry->next->prev=entry->prev;
	entry->next=entry->prev=0;
	return entry;	
}


void list_move(struct list_head *entry, struct list_head *head)
{
	list_del(entry);
	list_add(entry, head);
}

void list_move_tail(struct list_head *entry, struct list_head *head)
{
	list_del(entry);
	list_add_tail(entry, head);
}

int list_empty(struct list_head *head)
{
	if ((head->next==head) && (head->prev==head))
		return 1;
	else
		return 0;
}

struct list_head* list_element(struct list_head *head, int element)
{
	int i=0;
	struct list_head *next;
	next=head;
	while(((next=next->next)!=head) && (++i))
		if (i==element)
			return next;
	return 0;
}		


void list_free(struct list_head *head)
{
  struct list_head *itr;

  itr = head->next;
  while (itr != head) {
    itr = itr->next;
    free(itr->prev);
  }
  free(head);
}
