#include <stdio.h>
#include <stdlib.h>


typedef struct Node
{
	int key;
	struct Node *next;
} Node;

typedef struct Stack
{
	int size;
	Node *top;
} Stack;

void init_stack(Stack *s);
void push(Stack *s,int val);
int pop(Stack *s);
int is_empty(Stack *s);
int stack_top(Stack *s);
void print_stack(Stack *s);
int stack_size(Stack *s);

int main(void){
    
    Stack s;
    init_stack(&s);
    printf("Top=%d\n",stack_top(&s));
    printf("Pop=%d\n",pop(&s));
    push(&s,1); 
    push(&s,2);
    push(&s,3);
    push(&s,4);   
    print_stack(&s);
    printf("size=%d\n", stack_size(&s));
    
    printf("Top=%d\n",stack_top(&s));
    printf("Pop=%d\n",pop(&s));
    printf("Top=%d\n",stack_top(&s));
        
    push(&s,5); 
    push(&s,6);
    push(&s,7);   
    push(&s,8);   
    push(&s,9);   
    push(&s,10);  
	print_stack(&s);    

	printf("size=%d\n", stack_size(&s));
     
    printf("Top=%d\n",stack_top(&s));
    printf("Pop=%d\n",pop(&s));
    printf("Top=%d\n",stack_top(&s));
    printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("Pop=%d\n",pop(&s));
	printf("size=%d\n", stack_size(&s));

    return 0;
}

void init_stack(Stack *s)
{
    s->size=0;
    s->top=NULL;     
}

void push(Stack *s,int val)
{
    Node *new_node = (Node*)malloc(sizeof(Node));  
    new_node->key=val; 
	new_node->next = s->top;
    s->top = new_node;
    s->size++;
}

int pop(Stack *s)
{
    int val;
    struct Node *tmp;
    if (s->size > 0)
    {
       val = (s->top)->key;
       tmp = s->top;
       s->top = (s->top)->next;
       s->size--;
       free(tmp);
       return val;
    }
    else return -1;
}

int is_empty(Stack *s)
{
	if (s->size == 0) return 1;
	else return 0;
}

int stack_top(Stack *s)
{
    if (is_empty(s)) return -1;
    else return (s->top)->key;
}

void print_stack(Stack *s)
{
	if (is_empty(s))
		printf("Stack is empty!\n");
	else
	{	
		Node *t = s->top;
		
		printf("Stack:\n");
		while(t!=NULL)
		{
			printf("%d\n", t->key);            
			t=t->next;
		}
		printf("\n");
	}
}

int stack_size(Stack *s)
{
	return s->size;
}
