• 欢迎访问废江网站,承蒙遇见 QQ群
  • 本站将致力于推送优质的java知识以及算法,开源代码!

Doubly Linked List

AOJ 站点默认 4年前 (2020-04-05) 5846次浏览 已收录 44个评论 扫描二维码
文章目录[隐藏]

Question

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

Meaning

写链表实现双向链表的操作

Solution

这题坑我了三四个小时,oj测试数据中的第四组删除一个根本就不存在的数据。所以代码只能ac前面三个测试。用的是教材上面的双向链表。后面经过一个大佬给我修改了一下bug,原本在deleem函数中是不能删除最后一个节点的。但是还是不能ac,很恶心,不想看这一题了。感觉弄了好久啥也没学到。

Coding

Summary


废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Doubly Linked List
喜欢 (2)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址