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

#define INS ("insert")
#define DEL ("delete")
#define DEL_FST ("deleteFirst")
#define DEL_LST ("deleteLast")

#define CMD_TOP	 (0)
#define CMD_END	 (1)
#define CMD_DATA (-1)
typedef struct aaa{
	int		   data;
	struct aaa *next_p;
	struct aaa *back_p;
} TBL_T;

TBL_T	*top_p;


/**------------ SUB ROUTIONE -------------*/
int func_ins(int cmd,int reg_dt)
{
	TBL_T *wk_p;

	if (cmd != CMD_TOP) {
		printf("fun_ins ERROR\n");
		return -1;
	}

	if ((wk_p=(TBL_T*)malloc(sizeof(TBL_T)))==NULL) {
		printf("fun_ins ERROR 2\n");
		return -2;
	}

	wk_p->data = reg_dt;

	if (top_p == NULL) {
		wk_p->next_p = NULL;
		wk_p->back_p = NULL;
	}
	else {
		top_p->back_p = wk_p;
		wk_p->next_p=top_p;
		wk_p->back_p = NULL;
	}

	top_p = wk_p;
	return (int)wk_p;
}

int func_del(int cmd,int del_dt)
{
	TBL_T	*wk_p,*wk2_p;

	if (top_p == NULL) {
		return 0;
	}
	switch(cmd) {
	case CMD_TOP:
		wk_p = top_p;
		wk2_p = wk_p->next_p;
		wk2_p->back_p = NULL;
//		top_p->back_p = NULL;
		top_p = wk2_p;
		break;
	case CMD_END:
		wk_p = top_p;
		while (1) {
			if (wk_p->next_p == NULL) {
				wk2_p = wk_p->back_p;
				wk2_p->next_p = NULL;
				break;
			}
			else {
				wk_p = wk_p->next_p;
			}
		}
		break;
	case CMD_DATA:
		wk_p = top_p;
		while (1) {
			if (wk_p->data == del_dt) {
				wk2_p = wk_p->back_p;
				if (wk2_p != NULL) {
					wk2_p->next_p = wk_p->next_p;
				}
				wk2_p = wk_p->next_p;
				if (wk2_p != NULL) {
					wk2_p->back_p = wk_p->back_p;
				}
				break;
			}

			if (wk_p->next_p == NULL) {
				return 0;
			}
			else {
				wk_p = wk_p->next_p;
			}
		}
		break;
	default:
		printf("fun_del ERROR \n");
		return -1;
	}
	free(wk_p);
	return (int)wk_p;
}

void func_dsp(void)
{
	TBL_T	*wk_p;
????????????int     wk;

	if (top_p == NULL) {
		return;
	}

	wk_p = top_p;
	while (1) {
                wk = wk_p->data;
		if (wk_p->next_p == NULL) {
		        printf("%d",wk);
			break;
		}
		else {
		        printf("%d ",wk);
			wk_p = wk_p->next_p;
		}
	}
}

void func_free(void)
{
	TBL_T	*wk_p;


	while (1) {
		if (top_p == NULL) {
			return;
		}
		wk_p = top_p;
		top_p = wk_p->next_p;
		free(wk_p);
	}
}
/**------------ SUB ROUTIONE -------------*/

int main()
{
	int     n,i;

	char	name[32];
	int		data;
	TBL_T	*wk_p;
	char 	buff[96];


	/** INIT **/
	top_p = NULL;

	/** INPUT **/
	gets(&(buff[0]));
	sscanf(&(buff[0]),"%d",&n);

	for (i=0 ; i<n ; i++) {
		gets(&(buff[0]));
		sscanf(&(buff[0]),"%s %d",name,&data);
		if (strcmp(name,INS)==0) {
			func_ins(CMD_TOP,data);
		}
		else {
			if (strcmp(name,DEL)==0) {
				func_del(CMD_DATA,data);
			}
			else {
				if (strcmp(name,DEL_FST)==0) {
					func_del(CMD_TOP,NULL);
				}
				else{
					if (strcmp(name,DEL_LST)==0) {
						func_del(CMD_END,NULL);
					}
					else {
						printf("ERROR\n");
					}
				}
			}
		}
	}

	func_dsp();
	func_free();
	return 0;
}