#include<stdio.h>
#include<stdlib.h>
#define MAX 300000
#define DEBUG 0
typedef struct{
    int data;
    struct node * left;
    struct node * right;

}node;

node * get_node(){
    node *tmp =(node *)malloc(sizeof(node));
    tmp->data = 0;
    tmp->left = NULL;
    tmp->right = NULL;

 return tmp;
}

void pl(node *start){
    node *tmp;
    printf("%d n",start->data);
 if(start->right==NULL)return ;
    tmp = start->right;
    while(1){
        printf("%d n",tmp->data);
        if(tmp->right == NULL)break;
        tmp = tmp->right;
    }   
}
void insert(node **start,int toFind,int data){
    node *tmp,*toInsert,*tmpR ;
    int flag = 1;
    if(*start==NULL){ //업을때는 검색없이 삽입
        *start = get_node();
        (*start)->data = data;
        return;
    }
    tmp = *start;

    while(1){
        if(toFind == tmp->data){
            toInsert = get_node();
            tmpR = tmp->right;
            if(tmpR!=NULL)
                tmpR->left = toInsert;
            toInsert->left = tmp;
            toInsert->right = tmp->right;
            tmp->right = toInsert;
            toInsert->data = data;

            return;
        }
        if(tmp->right ==NULL)break;
        tmp = tmp->right;

    }
    toInsert = get_node();
    toInsert->data = data;
    tmp->right = toInsert;
    toInsert->left = tmp;

}
void deletex(node **start,int toFind){
    node *tmp,*tmpl,*tmpr;
    if(*start==NULL)return ;
    tmp=*start;
    if((*start)->data == toFind){
        *start = (*start)->right;
        (*start)->left = NULL;
        free(tmp);
        return;
    }
    while(1){
        if(tmp->data == toFind){
            tmpl        = tmp->left;
            tmpl->right = tmp->right;
            tmpr        = tmp->right;
            tmpr->left  = tmpl;
            free(tmp);
            return ;
        }
        tmp=tmp->right;
    }
}
int main(int* argc,char* argv[]){
    node *start=NULL;
    insert(&start,343, 3);  //첫값
    insert(&start,3  , 34); //3뒤에 붙는 값.
    insert(&start,111, 11); //못찾아서 맨뒤에 붙는 값.
    deletex(&start,3);
    deletex(&start,34);
   
   
    pl(start);
}