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

struct stackNode
{
    int data;
    struct stackNode * next;
};

// defining the generic function to PUSH an element into the stack
struct stackNode * push(struct stackNode * top, int element)
{
    //creating a temporary node and assigning the element to be PUSHED
    struct stackNode * temp = (struct stackNode *)malloc(sizeof(struct stackNode));
	if(!temp)
	{
		printf("STACK OVERFLOW");
		return top;
	}
	
    temp -> data = element;
     
    // we need to point the NEXT of this node to the previous TOP
    temp -> next = top;
	//Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
	//Feel free to copy but please acknowledge the site wherever possible
 
    // we need to update the TOP node and then return it
    // we can simply return the 'temp' node as it points to the new TOP node.
    return temp;
}

// defining a function to check if a stack is empty
int isStackEmpty(struct stackNode * top)
{
    if(top == NULL)
        return 1;
    else
        return 0;
}

//Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
//Feel free to copy but please acknowledge the site wherever possible

// defining a function to pop an element from a stack
struct stackNode * pop(struct stackNode * top)
{
    // check if the stack is empty
    if(isStackEmpty(top))
    {
        printf("STACK IS EMPTY...Stack underflow");
        return NULL;
    }
    else
    {
        printf("POP element = %d\n",top -> data);
 
        // we need to delete the top and move to the next element.
 
        struct stackNode * temp = top;
 
        // move to the next node
        top = top -> next;
		
		//Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
		//Feel free to copy but please acknowledge the site wherever possible
        
		// delete the original top
        free(temp);
 
        // return the new top
        return top;
    }
}

// function to get the size of the stack
int getStackSize(struct stackNode * top)
{
    int count = 0;
    while(top)
    {
        count++;
        top = top -> next;
		//Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
		//Feel free to copy but please acknowledge the site wherever possible
    }
    return count;
}

int main(void)
{
	struct stackNode * top = NULL;
	
	top = push(top,1);
	top = push(top,2);
	top = push(top,3);
	top = push(top,4);
	top = push(top,5);
	
	top = pop(top);
	top = pop(top);
	
	return 0;
}