Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
5
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
typedef struct _node{
    char data;
    struct _node* left;
    struct _node* right;
}Node;
Node *temp,*head=NULL;
Node* creatnode(char c){
    Node* new=(Node*)malloc(sizeof(Node));
    if(head==NULL){
        head=new;
        head->left=NULL;
    }
    new->data=c;
    new->left=temp;
    if(temp->right!=NULL){
        new->right=temp->right;
        temp->right->left=new;
        temp->right=new;
    }else if (temp->right==NULL){
        new->right=NULL;
        temp->right=new;
    }
    return new;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        while(n--){
            char c;
            scanf("%c",&c);
            switch(c){
                case 'L':
                    if(temp->left!=NULL)temp=temp->left;
                    break;
                case 'R':
                    if(temp->right!=NULL)temp=temp->right;
                    break;
                case 'B':
                    temp->left->right=temp->right;
                    temp->right->left=temp->left;
                    Node *cur=temp->left;
                    free(temp);
                    temp=cur;
                    break;
                default:
                    temp=creatnode(c);
                    break;
            }
        }
        temp=head;
        Node* del;
        while(temp!=NULL){
            printf("%c",temp->data);
            del=temp;
            temp=temp->right;
            free(del);
        }
        printf("\n");
    }