#include <stdio.h>
#include<stdlib.h>
typedef struct Node{
    int data;
    struct Node* next, *prev;
}Node;
Node* insert(Node* tail, int val){
    Node* new = malloc(sizeof(Node));
    new->data = val;
    if(!tail){
        new->prev; new->prev = new;
        return new;
    }
    new->next = tail->next;
    new->prev = tail;
    tail->next->prev = new;
    return new;
}
Node* delete(Node* tail, int k, int* found){
    if(!tail) return NULL;
    Node *cur = tail->next;
    do {
        if(cur->data == k){
            *found = 1;
            if(cur->next == cur){
                free(cur);
                return NULL;
            }
            cur->prev->next = cur->next;
            cur->next->prev = cur->prev;
            if(cur == tail) tail = cur->prev;
            free(cur);
            return tail;
        }
          cur = cur->next;
          }while (cur != tail->next);
          
          
          
          
    }
}