链表(裸题)

Doubly Linked List  Aizu - ALDS1_3_C

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
  • 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

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

题目意思是指写一个支持插入元素,删除特定元素,删除头元素,删除尾元素,打印整条链表操作的链表。

下面是初始化操作:

然后是各种普通操作顺序:

在理解后就可以看代码了,本题为了快速的删除尾,所使用的是环型链表,上代码:

 1     #include <iostream>
 2     #include <cstdio>
 3     #include <cstring>
 4     #include <cstdlib>
 5     using namespace std;
 6
 7     struct Node{
 8       int key;
 9       Node *prev;
10       Node *next;
11     };
12
13     Node *head;
14
15     Node *listSearch(int key){
16       Node *cur = head->next;
17       while(cur != head && cur->key != key){
18         cur = cur->next;
19       }
20       return cur;
21     }
22
23     void init(){
24       head = new Node;
25       head->prev = head;
26       head->next = head;
27     }
28
29     void printlist(){
30       Node *cur = head->next;
31       int isf = 0;
32       while(1){
33         if(cur == head)break;
34         if(isf++ > 0)printf(" ");
35         printf("%d",cur->key);
36         cur = cur->next;
37       }
38       printf("\n");
39     }
40
41     void insert(int key){
42       Node *x = new Node;
43       x->key = key;
44       x->next = head->next;
45       head->next->prev = x;
46       head->next = x;
47       x->prev = head;
48     }
49
50     void deleteNode(Node *t){
51       if(t == head)return;
52       t->prev->next = t->next;
53       t->next->prev = t->prev;
54       free(t);
55     }
56
57     void deleteFirst(){
58       deleteNode(head->next);
59     }
60
61     void deleteLast(){
62       deleteNode(head->prev);
63     }
64
65     void deletekey(int key){
66       deleteNode(listSearch(key));
67     }
68
69     int main(){
70       int key,n,i;
71       char com[20];
72       scanf("%d",&n);
73       init();
74       for(i = 0;i < n; i++){
75         scanf("%s%d",com,&key);
76         if(com[0] == ‘i‘){insert(key);}
77         else if(com[0] == ‘d‘){
78           if(strlen(com) > 6){
79             if(com[6] == ‘F‘)deleteFirst();
80             else if(com[6] == ‘L‘)deleteLast();
81           }
82           else{
83             deletekey(key);
84           }
85         }
86       }
87       printlist();
88       return 0;
89     }  
时间: 2024-10-12 11:44:44

链表(裸题)的相关文章

SZOJ 167 Lca裸题

一道.......一道我改了一周的裸题 无根树建双向边 无根树建双向边 无根树建双向边 重要的事情说三遍(微笑) 还有要开longlong 还有双向边不是双倍边(微笑) 我真是,能把自己气吐血10次就不把自己气吐血9次 [问题描述] 已知一棵nn个点的树,点从1开始标号,树上每条边都有一个正整数边权. 有qq个询问,每个询问由type,u,vtype,u,v三个正整数构成. 当type=1type=1时,询问uu到vv路径上所有边权的二进制异或和. 当type=2type=2时,询问uu到vv路

hdu1159 poj1458 LCS裸题

HDU 1159 题意:找LCS 思路:裸题 n*m的写法,我的写法好像比较奇怪...用一个ci保存s2第i位可以做为s1的公共子序列的最大值,s1的每一位遍历s2,遍历的时候记录前面出现过的ci的最大值,ci一定是一个连序的上升序列,我的好像不是正经的LCS的算法,改天还是要学习一个的 AC代码: #include "iostream" #include "string.h" #include "stack" #include "qu

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

HDOJ-1232 畅通工程【并查集裸题】

题目传送门 : http://acm.hdu.edu.cn/showproblem.php?pid=1232 并查集的裸题 AC code: #include <iostream> #define MAXN 1050 using namespace std; int pre[MAXN]; int Find(int pos) { int r = pos; while (r != pre[r]) r = pre[r]; int i = pos; while (i != r) { int t = p

【不可能的任务22/200】【填坑】bzoj3224 splay裸题

人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 1 #include <cstdio> 2 int root,N=0,n,p,q; 3 int fa[100001],c[100001][2],size[100001],sp[100001]; 4 void rot(int x) 5 { 6 int y=fa[x],k=(c[y][0]==x); 7 size[y]=size[c[y][k]]+size[c[x][k]]+1;size[x]=size[c[x][!k]]+

HDU 1102 最小生成树裸题,kruskal,prim

1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using na

贴一下WC总结里提到的那道裸题吧。。。

[bzoj4034][HAOI2015]T2 试题描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a . 操作 3 :询问某个节点 x 到根的路径中所有点的点权和. 输入 第一行包含两个整数 N, M .表示点数和操作数. 接下来一行 N 个整数,表示树中节点的初始权值. 接下来 N-1 行每行三个正整数 fr, to , 表示该树中存在一条边

tarjan讲解(用codevs1332(tarjan的裸题)讲解)

主要借助这道比较裸的题来讲一下tarjan这种算法 tarjan是一种求解有向图强连通分量的线性时间的算法.(用dfs来实现) 如果两个顶点可以相互通达,则称两个顶点强连通.如果有向图G的每两个顶点都强连通,称G是一个强连通图.有向图的极大强连通子图,称为强连通分量. 在上面这张有向图中1,2,3,4形成了一个强连通分量,而1,2,4,和1,3,4并不是(因为它们并不是极大强连通子图). tarjan是用dfs来实现的(用了tarjan后我们就可以对图进行缩点(当然这道裸题用不到)) 这道题只要

poj1006 ( hdu1370 ):中国剩余定理裸题

裸题,没什么好说的 第一个中国剩余定理 写暴力都过了..可见这题有多水 代码: #include<iostream> #include<stdio.h> #include<math.h> #include<string> #include<map> #include<algorithm> using namespace std; #define MAX 200000000 #define ull unsigned long long