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 ...
随机推荐
- python 爬虫系列04-电影天堂连接爬虫
学习的第四个爬虫 from lxml import etree import requests BASE_D = 'http://www.dytt8.net' headers = { 'User-Ag ...
- Windows x64位通过PEB获得Kernel32基地址
在64位系统下 gs:[0x30] 指向TEB gs:[0x60] 指向PEB kd> dt _TEB nt!_TEB +0x000 NtTib : _NT_TIB +0x000 Excepti ...
- 常用shell脚本(持续更新中)
1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...
- html结构内容拾忆
文本格式化: <b>This text is bold</b><!--定义粗体文本.--> <strong>This text is strong< ...
- 11、幻灯片:Slides
/* ---ts----*/ import { Page,Slides } from 'ionic-angular'; import { ViewChild } from '@angular/core ...
- 在Android源码中如何吧so库打包编译进入apk, 集成第三方库(jar和so库)
集成第三方so和jar包 include $(CLEAR_VARS) #jar包编译 LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :=securit ...
- Spring Chapter4 WebSocket 胡乱翻译 (二)
书接上文,Spring Chapter4 WebSocket 胡乱翻译 (一) 4.4.4. 消息流 一旦暴露了STOMP端点,Spring应用程序就成为连接客户端的STOMP代理. 本节介绍服务器端 ...
- 用单例模式解决临界区(CRITICAL_SECTION)的使用问题
一.前言 最近,在项目中涉及到多线程访问临界资源的问题.为了保护临界资源,可以是使用互斥量或者是使用临界区.由于,我不需要在多进程中同步,又为了效率的考量,所以选择了使用临界区的方式.但是,在使用临界 ...
- oracle学习篇九:同义词
Oracle数据库中提供了同义词管理的功能.Oracle同义词是数据库方案对象的一个别名,经常用于简化对象访问和提高对象访问的安全性. 在Oracle中对用户的管理是使用权限的方式来管理的,也就是说, ...
- python 初识
一.Python介绍 Python简介 Python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...