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