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)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 200

typedef struct node {
	//データ//
	char command[13];//命令
	int number;//数字
	//データ//
	struct node *prev;//前へ
	struct node *next;//次へ
}node;

int main(void)
{
	char instruction[4][12] = { "insert", "delete", "deleteLast", "deleteFirst", };//配列変数commandを単一変数で格納する

	int	all_number,//入力数
		f_cnt,//ループカウント
		w_cnt,//ループカウント
		dlt_cnt = 0,//deleteした数
		true_cnt = 0;//エラー回避
		

	node *P_node, *FR_P, *P_first, *P_last, *F_node, *F2_node, *L_node;

	scanf("%d", &all_number);
	
	P_node = malloc(sizeof(node));//最初の構造体を作る


	for (f_cnt = 0; f_cnt < all_number; f_cnt++) {//構造体に双方向連結のためのポインタと、コマンドを入れるループ

		scanf("%s", P_node->command);

		if (strcmp(P_node->command, instruction[0]) == 0 || strcmp(P_node->command, instruction[1]) == 0) {//"insert" or "delete"であった場合
			scanf("%d", &P_node->number);
		}
		else {
			P_node->number = 0;
		}
		

		if (f_cnt + 1 == all_number) {//最後の構造体ならNULL
			P_node->next = NULL;
		}
		else {
			P_node->next = malloc(sizeof(node));
		}


		if (f_cnt == 0) {//最初の構造体ならそれ以前の構造体は存在しないのでNULL
			P_node->prev = NULL;
			P_first = P_node;
		}

		if (f_cnt + 1 == all_number) {}//最後の構造体なら何も入力しない
		else {
			P_node->next->prev = P_node;//次の構造体のprevに前の構造体（今判定している構造体）のポインタを入れる
		}

	

		if(strcmp(P_node->command, instruction[0]) == 0){
			P_last = P_node;
			P_node = P_node->next;//次の構造体へ
		}

		else if (strcmp(P_node->command, instruction[1]) == 0) {//数字を指定し、それが配列内にあれば消去してその分を詰める
			F_node = P_node;//現在判定している構造体を保存
			while (P_node != NULL) {
				P_node = P_node->prev;//さかのぼって判定していく

				if (P_node->number == F_node->number) {

					if (P_node == P_first) {
						P_first = P_first->next;
						P_first->prev = NULL;
					}
					else if (P_node == P_last) {
						P_last = P_last->prev;
					}
					else {
						P_node->prev->next = P_node->next;//消すnodeの前のnextを一つ後にずらす
						P_node->next->prev = P_node->prev;//消すnodeの次のprevを一つ前のnodeにずらす
					}
					free(P_node);//該当した数字を削除する
					break;
				}
				if (P_node->prev == NULL) {
					break;
				}

			}

			if (F_node->next != NULL) {
				P_node = F_node->next;
				F_node->next->prev = P_last;
				P_last->next = F_node->next;
				free(F_node);
			}
		}

		else if (strcmp(P_node->command, instruction[2]) == 0) {//firstに格納されている数字を消去する
			if (P_first == P_last) {//firstとlastが同じ
				FR_P = P_node;
				if (P_node->next != NULL) {
					P_node = P_node->next;
					P_node->prev = NULL;
				}
				free(P_first);
				P_first = P_node;
				P_last = P_node;
				free(FR_P);
			}
			else {
				FR_P = P_first;
				P_first = P_first->next;
				P_first->prev = NULL;
				free(FR_P);
				FR_P = P_node;
				if (P_node->next != NULL) {
					P_node->next->prev = P_node->prev;
				}
				P_node->prev->next = P_node->next;
				P_node = P_node->next;
				free(FR_P);
				
			}
		}

		else if (strcmp(P_node->command, instruction[3]) == 0) {//lastに格納されている数字を消去してその分詰める
			if (P_last->prev == NULL) {//firstとlastが同じ
				FR_P = P_node;
				if (P_node->next != NULL) {
					P_node = P_node->next;
					P_node->prev = NULL;
				}
				free(P_first);
				P_first = P_node;
				P_last = P_node;
				free(FR_P);
			}
			else {
				FR_P = P_last;
				P_last = P_last->prev;
				P_last->next = P_node->next;
				free(FR_P);
				FR_P = P_node;
				if (P_node->next != NULL) {
					P_node->next->prev = P_last;
				}
				P_last->next = P_node->next;
				//多分ここがおかしい　　　　　　　　　　//////////////////////////////////////////////////////////////////////
				P_node = P_node->next;
				free(FR_P);
			}
		}

		else { break; }

	}

	
	P_node = P_last;

	while (1) {
		printf("%d", P_node->number);
		if (P_node->prev == NULL) {
			break;
		}
		P_node = P_node->prev;
		putchar(' ');
	}

	putchar('\n');


	return 0;

}