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<string.h>
#include<stdlib.h>

#define N 200

struct node{
  int key;
  struct node *next;
  struct node *prev;
};
typedef struct node *NodePointer;

NodePointer make_lnode(int , NodePointer, NodePointer );
void listprint(void);
NodePointer finditem(int);
void insert(int);
void delete(int);
void deleteFirst();
void deleteLast();

NodePointer head, tail;

int main(){
  int i, num, val, n;
  char str[N][20];

  head = tail = make_lnode(0, NULL, NULL);
    
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s",str[i]);
    if(strcmp(str[i],"insert")==0){
      scanf("%d",&val);
      insert(val);
    }
    else if(strcmp(str[i],"delete")==0){
      scanf("%d",&val);
      delete(val);
    }
    else if(strcmp(str[i],"deleteFirst")==0){
      deleteFirst();
    }
    else {
      deleteLast();
    }
  }
  listprint();
  
  return 0;
}
NodePointer make_lnode(int keydata, NodePointer prev, NodePointer next){
  NodePointer n;

  n = (NodePointer)malloc(sizeof(struct node));

  n->key = keydata;
  n->next = next;
  n->prev = prev;
  return n;
}
void listprint(void){
  NodePointer n;
  int i;

  for(n = head->next; n != tail; n = n->next){
    printf("%d ",n->key);
  }
  printf("%d\n",tail->key);
}
NodePointer finditem(int value){
  NodePointer n;

  for(n = head; n->next != tail; n = n->next){
    if(n->next->key == value) return n;
  }
   if(n->next->key == value) return n;
  return NULL;
}
void insert(int keydata){
  NodePointer newnode;

  static int i=0;

   if(i==0){

     tail->key = keydata;
    tail->prev = head;
    head->next = tail;
    i++;
  }
      else if(i ==1){
       newnode = make_lnode(keydata, head, tail);
       head->next->prev = newnode;
       head->next = newnode;
       i++;
       }
 else {
    newnode = make_lnode(keydata, head, head->next);
    newnode->next->prev = newnode;
    head->next = newnode;
    }
}
void deleteFirst(){

  NodePointer delnode, n;
 n = (NodePointer)malloc(sizeof(struct node));
  n = head;
  delnode = n->next;
  n->next = delnode->next;
  n->next->prev = head;
  free(delnode);
}
void deleteLast(){

  NodePointer delnode, n;
 n = (NodePointer)malloc(sizeof(struct node));
  n = tail;
  delnode = n;
  tail = n->prev;

  free(delnode);
}
void delete(int keydata){
  NodePointer n, delnode;
 n = (NodePointer)malloc(sizeof(struct node));
  n = finditem(keydata);

    if(n->next == tail){
      deleteLast();
    }
    else if(n->next == head->next){
      deleteFirst();
    }
    else{
      delnode = n->next;
      n->next = delnode->next;
      n->next->prev = n;
      free(delnode);
  }
}