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

#define SUCCESS 0
#define FAIL -1
#define TRUE 0
#define FALSE -1
#define ALLOCFAIL -2
#define EMPTY -3


typedef struct Node{
    struct Node* next;
    struct Node* prev;
    int data;
} node;

int push(node **head, int datainput){
   /* Initialization of new node */
   node* newptr = (node*) malloc(sizeof(node));    
   if(newptr == NULL){
       return ALLOCFAIL;
   }
   newptr->next = NULL;
   newptr->data = datainput;

   /* Check for empty list */
   if(*head == NULL){
       newptr->prev = NULL;
       *head = newptr; // change where head is pointing to
       return SUCCESS;
   }

   /* Get to the end of list*/
   node* headptr = *head;
   while(headptr->next != NULL){
       headptr = headptr->next;
   }

   headptr->next = newptr;
   newptr->prev = headptr;
   return SUCCESS;
}

int printlist(node* head){
    /* Check if valid node or empty list */
    if(head == NULL){
        return EMPTY;
    }

    /* Move to first node if not already */
    node* firstptr = head;
    while(firstptr->prev != NULL){
        firstptr = firstptr->prev;
    }

    /* Print entire list*/
    while(firstptr != NULL){
        if(firstptr->next != NULL){
            printf("%d -> ", firstptr->data);
        }
        else{
            printf("%d", firstptr->data);
        }
        firstptr = firstptr->next;
    }

	puts("");

    return SUCCESS;
}


int main(void)
{
	node *head = NULL; // <-- important initialization

	push(&head, 10);
	push(&head, 20);
	push(&head, 30);
	push(&head, 40);

	printlist(head);

	return 0;
}

