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

typedef struct Node{
    int data;
    struct Node* prev;
    struct Node* next;
}Node;

struct Node* front = NULL;
struct Node* rear = NULL;

Node* alloc_node(int e){
    Node* p = (Node*)malloc(sizeof(Node));
    p->data = e;
    p->prev = NULL;
    p->next = NULL;
    return p;
}

int is_empty(){
    return front == NULL;
}

void add_front(int e){
    Node* p = alloc_node(e);

    if(is_empty()){
        front = p;
        rear = p;
    }
    else{
        p->next = front;
        front->prev = p;
        front = p;
    }
}

void add_rear(int e){
    Node* p = alloc_node(e);

    if(is_empty()){
        front = p;
        rear = p;
    }
    else{
        rear->next = p;
        p->prev = rear;
        rear = p;
    }
}

void delete_front(){
    if(is_empty()){
        printf("overflow\n");
        exit(0);
    }
    
    Node* p = front;
    printf("%d\n", p->data);

    if(front == rear){
        front = NULL;
        rear = NULL;
    }
    else{
        front = front->next;
        front->prev = NULL;
    }
    free(p);
}

void delete_rear(){
    if(is_empty()){
        printf("underflow\n");
        exit(0);
    }
    
    Node* p = rear;
    printf("%d\n", p->data);

    if(front == rear){
        front = NULL;
        rear = NULL;
    }
    else{
        rear = rear->prev;
        rear->next = NULL;
    }
    free(p);
}

void print_all(){
    if(is_empty()){
        printf("empty\n");
        return;
    }

    Node* t = front;
    while(t != NULL){
        printf("%d ", t->data);
        t = t->next;
    }
    printf("\n");
}

int main(void){

    int n;

    while(1){
        scanf("%d", &n);

        if(n == -1){
            delete_front();
        }
        else if(n == -2){
            delete_rear();
        }
        else if(n == 0){
            print_all();
            break;
        }
        else{
            if(n % 2 == 0){
                add_rear(n);
            }
            else{
                add_front(n);
            }
        }
    }
    
    return 0;
}

















Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: