#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


// typedef struct length {
//     int len;
// } len;

typedef struct node {
    // int index;
    char val;
    struct node *next;
    // struct length len;
} node_t;

/* push to the beginning of the list */
// void push(node_t ** head, int val) {
//     node_t * new_node;
//     new_node = malloc(sizeof(node_t));

//     new_node->val = val;
//     new_node->next = *head;
//     *head = new_node;
// }

/* Push to the end of the list */
void push(node_t *head, char val) {
    node_t *current = head;
    // if (current == NULL){
    //     printf("init list first.\n");
    //     exit(EXIT_FAILURE);
    // }
    while (current->next != NULL) {
        current = current->next;
    }

    /* now we can add a new variable */
    current->next = malloc(sizeof(node_t));

    // current->next->index=current->index+1;
    // current->next->len=current->len;
    current->next->val = val;
    // current->len.len +=1;
    current->next->next = NULL;
}

/* Pop the last item (top of the list) */
char pop(node_t * head) {
    char retval = 0;

    /* if there is only one item in the list, remove it */
    if (head->next == NULL) {
        retval = head->val;
        // head->len.len-=1;
        free(head);
        return retval;
    }

    /* get to the second to last node in the list */
    node_t * current = head;
    while (current->next->next != NULL) {
        current = current->next;
    }

    /* now current points to the second to last item of the list, so let's remove current->next */
    retval = current->next->val;
    // current->len.len -= 1;
    free(current->next);
    current->next = NULL;
    return retval;

}

void print_list(node_t * head) {
    node_t * current = head;
    printf("[");
    while (current->next != NULL) {
        printf("%c,", current->val);
        current = current->next;
    }
    printf("%c",current->val);
    printf("]\n");
}


void init(node_t *head){
    
    if (head == NULL) {
        exit(EXIT_FAILURE);
    }
    // head->index=0;
    // head->len.len=1;
    head->val = 'a';
    head->next = NULL;
}