#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node;
struct Node* top = NULL;
Node* alloc_node(int e){
Node* p = (Node*)malloc(sizeof(Node));
p->data = e;
p->next = NULL;
return p;
}
int free_node(Node* p){
int result = p->data;
free(p);
return result;
}
int is_empty(){
if(top == NULL){
return 1;
}
else{
return 0;
}
}
void push(int e){
Node* p = alloc_node(e);
if(is_empty()){
top = p;
}
else{
p->next = top;
top = p;
}
}
int pop(){
if(is_empty()){
printf("underflow\n");
exit(0);
}
else{
Node* p = top;
top = top->next;
return free_node(p);
}
}
int print_stack(Node* t){
if(is_empty()){
printf("empty\n");
exit(0);
}
else{
while(t != NULL){
printf("%d ", t->data);
t = t->next;
}
printf("\n");
}
}
int main(void){
int n;
while(1){
scanf("%d", &n);
if(n == 0){
print_stack(top);
break;
}
else if(n == -1){
pop();
}
else if(n > 0){
push(n);
}
}
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: