AcWing 826. 单链表 数组模拟单链表
原题链接
简单
作者:
皓首不倦
,
2020-08-07 16:47:47
,
所有人可见
,
阅读 432
from collections import deque
M = int(input())
# 默认下标是0的位置是虚拟头结点,所以多分一个位置
node = [0] * (M+1)
next = [0] * (M+1)
next[0] = -1
avai_pos = deque() # 可用的位置
for i in range(1, M+1):
avai_pos.append(i)
time_stamp2pos = {0:0} # 时间戳到存放索引的映射
time_stamp = 1
for _ in range(M):
s = input()
if s[0] == 'H':
val = int(s.split()[1])
new_pos = avai_pos.popleft()
next[new_pos] = next[0]
next[0] = new_pos
node[new_pos] = val
time_stamp2pos[time_stamp] = new_pos
time_stamp += 1
elif s[0] == 'I':
k, val = map(int, s.split()[1:])
cur_pos = time_stamp2pos[k]
new_pos = avai_pos.popleft()
next[new_pos] = next[cur_pos]
next[cur_pos] = new_pos
node[new_pos] = val
time_stamp2pos[time_stamp] = new_pos
time_stamp += 1
else:
k = int(s.split()[1])
cur_pos = time_stamp2pos[k]
if next[cur_pos] != -1:
next_pos = next[cur_pos]
next[cur_pos] = next[next_pos]
next[next_pos] = -1
avai_pos.append(next_pos)
cur_pos = next[0]
ans = []
while cur_pos !=- 1:
ans.append(node[cur_pos])
cur_pos = next[cur_pos]
print(' '.join(map(str, ans)))