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

//##################### Struktur und Funktionen zur Stack-Definition #####################
typedef struct NODE {
	int elem;
	struct NODE *next;
} node;

node *empty(node *head){
	head=(node *)malloc(sizeof(node));
	head->elem=0;
	head->next=NULL;
	return head;
}

char isempty(node *head){
	if(head->next==NULL)
		return 1;
	return 0;
}

void push(node *head, int elem){
	node *neu=(node *)malloc(sizeof(node));
	neu->next=head->next;
	neu->elem=elem;
	head->next=neu;
}

int pop(node *head){
	int zahl;
	if(head->next!=NULL){
		node *h=head->next;
		head->next=h->next;
		zahl=h->elem;
		free(h);
		return zahl;
	}
	return -1;
}
//########################################################################################

void print_stack(node *head){
	node *h=head;
	printf("Head->");
	while(h->next!=NULL){
		h=h->next;
		printf("%d->", h->elem);
	}
	printf("NULL\n\n");
}


int main(void){
	node *head;
	int zzahl, zzahl2, i;
	srand(time(NULL));
	zzahl=rand()%50;
	head=empty(head);

	for(i=0; i<zzahl; i++){
		if(rand()%2){
			if(!isempty(head))
				printf("pop()=%d,  ", pop(head));
		}
		else{
			zzahl2=rand()%100+1;
			push(head, zzahl2);
			printf("push(%d),  ", zzahl2);
		}
	}
	printf("\b\b\b   \n\nAusgabe des Stacks:\n  ");
	print_stack(head);

	return 0;
}

