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++11 (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;

int InsertionSort(vector<int>& Array, int N, int g)
{
  	int count = 0;
  	for(int i = g; i< N;i++)
    {
    	int tmp = Array[i];
	  	int j = i -g;
      	while(j>=0 && Array[j] > tmp)
        {
          	Array[j+g] = Array[j];
          	j = j-g;
          	count++;
        }
    	Array[j+g] = tmp;
    }
  	return count;
}

void ShellSort(vector<int> Array, int N)
{
  	int count=0;
  	vector<int> G;
  	G.push_back(1);
  	int g=G[0];
  	while(N > g*3+1)
    {
      g=g*3+1;
      G.push_back(g);
    }
  	int m = G.size();
  	for(int i=m-1;i>=0;i--)
    {
      count+=InsertionSort(Array, N, G[i]);
    }
  	cout << m << endl;
  	for(int i=m-1;i>0;i--) cout << G[i] << " ";
  	cout << G[0] << endl;
  	cout << count << endl;
  	for(int i=0;i<N;i++) cout << Array[i] << endl;
}

vector<string> split(const string &s, char delim) {
    vector<string> elems;
    string item;
    for (char ch: s) {
        if (ch == delim) {
            if (!item.empty())
                elems.push_back(item);
            item.clear();
        }
        else {
            item += ch;
        }
    }
    if (!item.empty())
        elems.push_back(item);
    return elems;
}

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
    if(a.second != b.second){
        return a.second > b.second;
    }else{
        return a.first > b.first;
    }
}

class Node;
Node* p;

class Node
{
public:
  long key;
  Node* prev;
  Node* next;
  Node(){}
  Node(long v)
  {
    key=v;
    prev = nullptr;
    next = nullptr;
  }
  
  void insert(long v)
  {
    Node* n = new Node(v);
    next = n;
    n->prev = this;
    n->next = p;
    p->prev = n;
  }
  
  Node* searchNode(long v)
  {
    Node* cur = p->prev;
    while(cur != p && cur->key != v) cur = cur->prev;
    return cur;
  }
  
  void deleteNode(Node* n)
  {
    if(n == p) return;
    n->prev->next = n->next;
    n->next->prev = n->prev;
    delete n;
  }
  
  void deleteKey(long v)
  {
    deleteNode(searchNode(v));
  }
  
  void deleteFirst()
  {
    deleteNode(p->prev);
  }
  
    void deleteLast()
  {
    deleteNode(p->next);
  }

};

void init()
{
  p = new Node();
  p->next = p;//最後
  p->prev = p;//先頭
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
  	cin >> n;
  	//vector< pair<string, long> > p;
	//vector<long> A(N);
  	int idx = 0;
  	//while(cin >> A[idx]) idx++;
  	string command;
  	long num;
  	//vector<long> elem;
  	init();
  	while(cin >> command)
    {
      if(command == "insert")
      {
        cin >> num;
        if(p->prev == p) p->insert(num);
        else p->prev->insert(num);
      }
      else if(command == "delete")
      {
        cin >> num;
        p->deleteKey(num);
      }
      else if(command == "deleteFirst")
      {
        p->deleteFirst();
      }
      else if(command == "deleteLast")
      {
        p->deleteLast();
      }
      idx++;
      if(idx == n) break;
    }
  	Node* o = p->prev;
  	while(p->next != o)
    {
      cout << o->key << " ";
      o = o->prev;
    }
  	cout << p->next->key << endl;
  	return 0;
}