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: Time Limit Exceeded)


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

#define MAX 2500000

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

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

	int array[MAX] = { 0 }, a_idx = 0,
		all_number,//入力数
		I_input,//数字
		f_cnt,//ループカウント
		w_cnt,//ループカウント
		dlt_cnt = 0,//deleteした数
		true_cnt = 0;//エラー回避
		

	node *P_node, *FR_P;

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

		
		if(strcmp(P_node->command, instruction[0]) == 0){//先頭に数字を入れる
			array[a_idx] = P_node->number;
			a_idx++;
		}

		else if (strcmp(P_node->command, instruction[1]) == 0) {//数字を指定し、それが配列内にあれば消去してその分を詰める
			w_cnt = a_idx;
			while (w_cnt != -1) {//指定した数字が配列に格納されていないかを検索するループ
				if (P_node->number == array[w_cnt]) {

					while (w_cnt != a_idx + 1) {
						array[w_cnt] = array[w_cnt + 1];
						w_cnt++;
					}

					dlt_cnt += 2;
					a_idx--;
					true_cnt = 1;
					break;
				}
				w_cnt--;
			}
			if (true_cnt == 1) {
				true_cnt = 0;
			}
			else {//指定した数字が配列に格納されていない場合
				dlt_cnt++;
			}
		}

		else if (strcmp(P_node->command, instruction[2]) == 0) {//最後の格納されている数字を消去する
			array[a_idx - 1] = 0;
			dlt_cnt += 2;
			a_idx--;
		}

		else if (strcmp(P_node->command, instruction[3]) == 0) {//最初に格納されている数字を消去してその分詰める
			for (w_cnt = 0; w_cnt < all_number - dlt_cnt; w_cnt++) {
				array[w_cnt] = array[w_cnt + 1];
			}
			dlt_cnt+= 2;
			a_idx--;
		}

		else { break; }

		FR_P = P_node;

		P_node = P_node->next;//次の構造体へ

		free(FR_P);

	}

	
	w_cnt = f_cnt - dlt_cnt - 1;//配列の先頭から出力するため

	while (w_cnt != -1) {
		printf("%d", array[w_cnt]);
		w_cnt--;
		if (w_cnt != -1) {
			putchar(' ');
		}
	}

	putchar('\n');


	return 0;

}