Problem:
Doubly Linked List

Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.

delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.

deleteFirst: delete the first element from the list.

deleteLast: delete the last element from the list.

Input

The input is given in the following format:

n

command1

command2

...

commandn

In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

insert x

delete x

deleteFirst

deleteLast

Output

Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints

The number of operations ≤ 2,000,000

The number of delete operations ≤ 20

0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106

For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1

7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5

Sample Output 1

6 1 2

Sample Input 2

9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast

Sample Output 2

1

Solution in C (judge verdict: Accepted)

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

typedef struct PLIST{
	struct PLIST *pre;
	struct PLIST *next;
	int key;
}plist;

plist *head;
plist *tail;

int compStr(char *str0, char *str1){
	int i = 0;
	for( i = 0; str0[i] != '\0' || str1[i] != '\0'; i++ ){
		if( str0[i] != str1[i] ){ return 0; }
	}
	if( str0[i] != str1[i] ){ return 0; }
	return 1;
}

void insert(plist *p, plist *next){
	if( p == NULL ){ return; }
	if( next != NULL ){
		p->next = next;
		next->pre = p;
	}else{
		p->next = NULL;
		tail = p;
	}
	p->pre = NULL;
	head = p;
	return;
}

void delete(plist *p){
	if( p == NULL ){ return; }
	if( p->pre != NULL ){
		p->pre->next = p->next;
	}else{
		head = p->next;
	}
	if( p->next != NULL ){
		p->next->pre = p->pre;
	}else{
		tail = p->pre;
	}
	free(p);
	return;
}

int main(){
	int i = 0;
	int n = 0;
	char com[1024];
	int x = 0;
	scanf("%d", &n);
	plist *temp;
	for( i = 0; i < n; i++ ){
		scanf("%s", com);
		if( compStr(com, "insert\0") ){
			scanf("%d", &x);
			temp = (plist*)malloc(sizeof(plist));
			temp->key = x;
			insert(temp, head);
		}
		if( compStr(com, "delete\0") ){
			scanf("%d", &x);
			temp = head;
			while( temp != NULL ){
				if( temp->key == x ){ break; }
				temp = temp->next;
			}
			delete(temp);
		}
		if( compStr(com, "deleteFirst\0") ){
			delete(head);
		}
		if( compStr(com, "deleteLast\0") ){
			delete(tail);
		}
		/*
		temp = head;
		if( temp != NULL ){
			printf("%d", temp->key);
			while( (temp = temp->next) != NULL ){ printf(" %d", temp->key); }
		}
		printf("\n");
		*/
	}
	temp = head;
	if( temp != NULL ){
		printf("%d", temp->key);
		while( (temp = temp->next) != NULL ){ printf(" %d", temp->key); }
	}
	printf("\n");
	return 0;
}