Doubly Linked List
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 注意: 指令和数字不能同时输入, 比如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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode 430. Flatten a Multilevel Doubly Linked List
原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/ 题目: You a ...
- 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 ...
随机推荐
- MVC JSON JavaScriptSerializer 进行序列化或反序列化时出错
MVC control中返回json格式数据一般都是如下格式 [HttpPost] public ActionResult CaseAudit(string name) { var data =&qu ...
- ansible 命令详解{图片详解}
本文内容来至于http://www.zsythink.net 文件操作模块 命令操作模块 cron 包管理模块
- 使用SVN进行源码管理
阅读目录: 1.SVN服务端配置 1.1 创建版本库 1.2 创建用户 1.3 设置用户权限 2.SVN客户端使用 2.1 向SVN服务器中导入源码 2.1.1 直接通过TortoiseSVN向SVN ...
- 【mysql】mysql数据库安装
今天一直在测功能,整理用例,所以没有去调项目,想到之前有小伙伴求助数据库安装,还远程了一番,所以,就整理一下,数据库在测试工作中还是挺重要的,不能本地测试改个数据都去找开发小哥哥吧,是不是不太好呢,妹 ...
- jqgrid 上移下移单元格
在表格中常常需要调整表格中数据的显示顺序,我用的是jqgrid,实现原理就是将表中的行数保存到数据库中,取数据时按行进行排序 1.上移,下移按钮 <a href="javascript ...
- SQLAlchemy基本操作和常用技巧
点击打开链接 Python的ORM框架SQLAlchemy基本操作和常用技巧,包含大量实例,非常好的一个学习SQLAlchemy的教程,需要的朋友可以参考下 python编程语言下的一款开源软件.提供 ...
- 【Shell】shell 判断文件夹或文件是否存在
1.文件夹不存在则创建,文件夹是directory if [ ! -d "/data/" ];then mkdir /data else echo "文件夹已经存在&qu ...
- 2017年11月3日 VS三大类&数组&VS的冒泡排序&集合&泛型集合
三大类 共分为两个大类: 基本数据型&引用类型 基本数据型---值类型---整型---常用的整型: Int , 长整型: Long, 小整型: byle, 中整型 short --浮点型 - ...
- Spring Chapter4 WebSocket 胡乱翻译 (一) 一个例子
因为没有基础,不知道从哪里入手. 文档里的例子,https://github.com/rstoyanchev/spring-websocket-portfolio,这个除了WebSocket,还整了S ...
- 个人MySQL股票数据库的建立日记
#!/usr/bin/python# -*- coding: UTF-8 -*- import tushare as tsfrom sqlalchemy import create_engine co ...