#include "mod10-1_types.h"

/* adds a new node to the linked list */
Structure *add_node(Structure *first)
{
        Structure *temp, *L;
       
        temp = (Structure *)malloc(sizeof(Structure));
        if (first->next == NULL) {
                temp->next = NULL;
                first->next = temp;
                L = temp;
        } else {
                L = first;
                while (L->next != NULL)
                        L = L->next;
                temp->next = NULL;
                L->next = temp;
                L = temp;
        } /* end of if..else */

        return(L);
       
} /* end add_node */

/* finds a data in a linked list */
int find_id (Structure *first, int id_number, Structure **previous, Structure **current)
{
        int a = 1, b = 0, counter = 1; /* counter means the first node */
       
        (*previous) = first;
        (*current) = first;
        while ((*current)->ID != id_number && (*current)->next != NULL) {
                if (counter == 1) {
                        (*current) = (*current)->next;
                        counter = 0;
                } else {
                        (*previous) = (*previous)->next;
                        (*current) = (*current)->next;
                }
        }
       
        if ((*current)->ID == id_number) {
                return(a);
        } else
                return(b);
}      

/* removes certain nodes from a linked list */
void delete_node(Structure **first, Structure *previous, Structure *current)
{
        if ((*first == NULL) || (previous == NULL) || (current == NULL)) {
                printf("Error, can't remove a empty list \n");
                exit(1);
        }
        if (*first == current) {
                *first = current->next;
                free(current);
        } else {
                previous->next = current->next;
                free(current);
        }
       
        return;
}

/* displays certain names from the linked list */
void display_name(Structure *display)
{
        printf("%d %s %s\n", display->ID, display->firstname, display->lastname);
       
        return;
}      

/* delete any nodes left in the list */
void delete_list(Structure **first)
{
        Structure *next_node;
       
        if ((*first)->next == NULL)
                return;
        else {
                while ((*first)->next != NULL) {
                        next_node = (*first)->next;
                        free(*first);
                        (*first) = next_node;
                }
        }
        free(*first);
       
        return;
} 

