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)

// id: ALDS1_3_C
// CPU: 00:28sec  Memory: 32972KB
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define MAX_LEN 11

struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node {
	ElementType element;
	Position prior;
	Position next;
};

List create_list(void);
void make_empty(List l);
void delete_list(List l);
bool is_empty(List l);
void insert(List l, ElementType x);
void delete(List l, ElementType x);
Position find(List l, ElementType x);
void delete_first(List l);
void delete_last(List l);
void print_list(List l);
static void terminate(const char *message);

int main(void)
{
	int n;
	List dlist = create_list();

	scanf("%d", &n);

	for ( int i = 0; i < n; ++i ) {
		char command[MAX_LEN + 1];
		int key;

		scanf("%s", command);
		
		if ( strcmp(command, "insert") == 0 ) {
			scanf("%d", &key);
			insert(dlist, key);
		}
		else if ( strcmp(command, "delete") == 0 ) {
			scanf("%d", &key);
			delete(dlist, key);
		}
		else if ( strcmp(command, "deleteFirst") == 0 )
			delete_first(dlist);
		else
			delete_last(dlist);
	}

	print_list(dlist);

	delete_list(dlist);

	return 0;
}

List create_list(void)
{
	List l;

	l = malloc(sizeof(struct Node));
	if ( l == NULL )
		terminate("Error in create: list could not be created.");

	l->prior = l;
	l->next = l;

	return l;
}

void make_empty(List l)
{
	while ( !is_empty(l) )
		delete_first(l);
}

void delete_list(List l)
{
	make_empty(l);

	free(l);
}

bool is_empty(List l)
{
	return l->next == l;
}

void insert(List l, ElementType x)
{
	Position tmp = malloc(sizeof(struct Node));

	if ( tmp == NULL ) {
		delete_list(l);
		terminate("Error in insert: fail to allocate space for the new node.");
	}

	tmp->element = x;
	tmp->next = l->next;
	tmp->prior = l;

	l->next = tmp;
	tmp->next->prior = tmp;
}

void delete(List l, ElementType x)
{
	Position p;

	p = find(l, x);

	if ( p != NULL ) {
		p->prior->next = p->next;
		p->next->prior = p->prior;
		
		free(p);
	}
}

Position find(List l, ElementType x)
{
	Position p;

	for ( p = l->next; p != l && p->element != x; p = p->next )
		;

	if ( p == l )
		p = NULL;

	return p;
}

void delete_first(List l)
{
	Position tmp = l->next;

	if ( is_empty(l) ) {
		delete_list(l);
		terminate("Error in delete_first: list is empty.");
	}

	l->next = tmp->next;
	tmp->next->prior = l;

	free(tmp);
}

void delete_last(List l)
{
	Position tmp = l->prior;

	if ( is_empty(l) ) {
		delete_list(l);
		terminate("Error in delete_last: list is empty.");
	}

	tmp->prior->next = l;
	l->prior = tmp->prior;

	free(tmp);
}

void print_list(List l)
{
	Position p = l->next;

	if ( p != l ) {
		printf("%d", p->element);
		p = p->next;
	}

	while ( p != l ) {
		printf(" %d", p->element);
		p = p->next;
	}

	printf("\n");
}

static void terminate(const char *message)
{
	printf("%s\n", message);
	exit(EXIT_FAILURE);
}