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
command
command
...
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

注意: 指令和数字不能同时输入, 比如deleteFirst只有指令没有数字, 数字要在相应的判断条件中输入

#include <iostream>
#include <list>
#include <cstdio>
using namespace std; int main()
{
int n, num;
list<int> l;
char s[20];
scanf("%d", &n);
for(int i = 0; i < n; ++ i)
{
scanf("%s", s);
if(s[0] == 'i')
{
scanf("%d", &num);
l.push_front(num);
}
else if(s[6] == 'F')
{
l.pop_front();
}
else if(s[6] == 'L')
{
l.pop_back();
}
else if(s[0] == 'd')
{
scanf("%d", &num);
for(list<int>::iterator it = l.begin(); it != l.end(); it ++)
{
if(*it == num)
{
l.erase(it);
break;
}
}
}
} int i = 0;
for(list<int>::iterator it = l.begin(); it != l.end(); it ++)
{
if(i ++) printf(" ");
printf("%d", *it);
}
printf("\n"); return 0;
}

  

Doubly Linked List的更多相关文章

  1. Convert a given Binary Tree to Doubly Linked List

    The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...

  2. [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...

  3. [LeetCode] Flatten a Multilevel Doubly Linked List 压平一个多层的双向链表

    You are given a doubly linked list which in addition to the next and previous pointers, it could hav ...

  4. [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  5. Convert Binary Search Tree to Doubly Linked List

    Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...

  6. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  7. [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  8. LeetCode 430. Flatten a Multilevel Doubly Linked List

    原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/ 题目: You a ...

  9. LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List

    题目 Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right point ...

随机推荐

  1. mysql记录数据库中重复的字段的数据

    SELECT    SUM(co)FROM    (        SELECT            telephone,            count(telephone) AS co     ...

  2. 解决IE9 IE8的跨域 请求问题

    /// <summary> /// 根据url获取对应的HTML /// </summary> /// <param name="url">&l ...

  3. 最好用的数据存储Easy Save2讲解

    转载:http://www.manew.com/thread-100109-1-1.html   今天抽时间学习了“Easy Save2”插件,版本v2.6.3  我个人觉得这个插件是做数据存取最好的 ...

  4. SVM 之 MATLAB 实现代码

    MATLAB 中 SVM 实现 直接上代码 main.m %% Initialize data clear, clc, close all; load('data.mat'); y(y == 0) = ...

  5. 前端测试框架 puppeteer 文档翻译

    puppeteer puppeteer 是一个通过DevTools 协议提供高级API 来控制 chrome,chromium 的 NODE库; puppeteer默认运行在 headless 模式, ...

  6. Javascript怎么跳出循环,嵌套循环。

    今天要实现一个功能,在数组a中的每一项,对应数组b中的每一项,如果对应上了就给数组b的checked增加ture属性,如果查找不到就给数组b的checked增加false属性. 如果有哪里写的不对欢迎 ...

  7. 如何取得GridView被隐藏列的值

    如何取得GridView被隐藏列的值         分类:             ASP.net              2009-06-25 12:47     943人阅读     评论(1 ...

  8. SpringSecurity 3.2入门(2)环境搭建

    由于目前Spring官方只提供Meven的下载方式,为了能以最快的速度入门使用框架,这里提供百度网盘下载链接. 注:本入门教程默认已经配置成功SpringMVC框架. 1.web.xml配置 < ...

  9. double类型计算

    下面两个例子体现两个运算规则 一.四舍五入 //四舍五入 double doublenum = Math.Round(12.5, MidpointRounding.AwayFromZero); //两 ...

  10. spring cloud Eureka 服务注册发现与调用

    记录一下用spring cloud Eureka搭建服务注册与发现框架的过程. 为了创建spring项目方便,使用了STS. 一.Eureka注册中心 1.新建项目-Spring Starter Pr ...