#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __Node{
int Data;
struct __Node* Next;
struct __Node* Prev;
}Node;
Node* NewNode(int data);
void AddendNode(Node* head, Node* end);
void RemoveNode(Node* head, Node* node);
void DeleteNode(Node* node);
void DestroyNode(Node* head);
void push(Node** head, int data);
int pop(Node** head);
int size(Node* head);
int empty(Node* head);
int top(Node* head);
int main() {
Node* head=NULL;
int N, X;
char str[100];
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%s", str);
if (!strcmp(str,"push")) {
scanf("%d", &X);
push(&head, X);
}
else if (!strcmp(str,"pop")) {
printf("%d\n", pop(&head));
}
else if (!strcmp(str,"size")) {
printf("%d\n", size(head));
}
else if (!strcmp(str,"empty")) {
printf("%d\n", empty(head));
}
else if (!strcmp(str,"top")) {
printf("%d\n", top(head));
}
}
return 0;
}
Node* NewNode(int data){
Node* tmp=(Node*)malloc(sizeof(Node));
tmp->Data = data;
tmp->Next = NULL;
tmp->Prev = NULL;
return tmp;
}
void AddendNode(Node* head, Node* end){
while(head->Next!=NULL){
head=head->Next;
}
head->Next = end;
if(end!=NULL) end->Prev = head;
}
void RemoveNode(Node* head, Node* node){
while(head->Next == node){
head= head->Next;
}
head->Next = node->Next;
node->Next->Prev = head;
DeleteNode(node);
}
void DeleteNode(Node* node){
free(node);
}
void DestroyNode(Node* head){
Node* tmp;
while(head->Next!=NULL){
tmp = head;
head=head->Next;
DeleteNode(tmp);
}
}
void push(Node** head, int data) {
Node* tmp = *head;
(*head)= NewNode(data);
AddendNode(*head, tmp);
}
int pop(Node** head){
if(*head==NULL) return -1;
int data = (*head)->Data;
Node* tmp = *head;
*head=(*head)->Next;
if(*head!=NULL) (*head)->Prev = NULL;
DeleteNode(tmp);
return data;
}
int size(Node* head){
int count =0;
while(head!=NULL){
count++;
head=head->Next;
}
return count;
}
int empty(Node* head){
if(head==NULL) return 1;
return 0;
}
int top(Node* head) {
if(head==NULL) return -1;
return head->Data;
}