#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

/*********************************************************************
 * function:    list add_to_front(list l, void *newdata)             *
 *                                                                   *
 * parameters:  l         list                                       *
 *              *newdata  data to add to list                        *
 *                                                                   *
 * description: Adds data to the front of the list.                  *
 *                                                                   *
 * return values: Returns a pointer to the front of the list.        *
 *********************************************************************/

list add_to_front (list l, void *newdata)
{
  struct list_node *node = (struct list_node *)malloc(sizeof(struct list_node));

  node->data = newdata;
  node->next = l;
  return node;
}

/*********************************************************************
 * function:    list create_list()                                   *
 *                                                                   *
 * description: Creates a new list.                                  *
 *                                                                   *
 * return values: Returns a pointer to the front of the list.        *
 *********************************************************************/

list create_list()
{
  struct list_node *node = (struct list_node *)malloc(sizeof(struct list_node));
  node->data = NULL;
  node->next = NULL;

  return node;
}

/*********************************************************************
 * function:    int destroy_list(list l)                             *
 *                                                                   *
 * parameters:  l       list to be destroyed                         *
 *                                                                   *
 * description: Frees the memory allocated to list items and list.   *
 *                                                                   *
 * return values: Returns 0.                                         *
 *********************************************************************/

int destroy_list(list l)
{
  list next;

  while (l != NULL) {
    next = l->next;
    free(l->data);
    free(l);
    l = next;
  }
  return 0;
}

/*********************************************************************
 * function:    int is_in_list(list l, void *listmember)             *
 *                                                                   *
 * parameters:  l            list to search to                       *
 *              *listmember  member to search for                    *
 *                                                                   *
 * description: Checks if listmember is in list.                     *
 *                                                                   *
 * return values: Returns 1 if listmember is found in list,          *
 *                0 otherwise.                                       *
 *********************************************************************/

int is_in_list(list l, void *listmember)
{
  list next;

  while (l->next != NULL) {
   next = l->next;
   if (strcasecmp(l->data, listmember) == 0)
      return 1;
    l = next;
  }
  return 0;
}



