AcWing 827. 双链表
原题链接
简单
作者:
皓首不倦
,
2020-08-07 17:43:04
,
所有人可见
,
阅读 411
'''
用数组模拟单链表
'''
M = int(input())
# 默认下标是0的位置是虚拟头结点,所以多分一个位置
node = [0] * (M+1)
next = [0] * (M+1)
prev = [0] * (M+1)
next[0] = -1
prev[0] = -1
time_stamp = 1
tail = 0
for _ in range(M):
s = input()
if s[0] == 'L':
val = int(s.split()[1])
next_pos = next[0]
node[time_stamp] = val
next[time_stamp] = next_pos
prev[time_stamp] = 0
if next_pos != -1:
prev[next_pos] = time_stamp
next[0] = time_stamp
if next[time_stamp] == -1:
tail = time_stamp
time_stamp += 1
elif s[0] == 'R':
val = int(s.split()[1])
node[time_stamp] = val
next[time_stamp] = -1
prev[time_stamp] = tail
next[tail] = time_stamp
tail = time_stamp
time_stamp += 1
elif s[0] == 'I':
k, val = map(int, s.split()[1:])
prev_pos, next_pos = -1, -1
if s[1] == 'L':
prev_pos = prev[k]
next_pos = k
else:
prev_pos = k
next_pos = next[k]
node[time_stamp] = val
prev[time_stamp] = prev_pos
next[time_stamp] = next_pos
next[prev_pos] = time_stamp
if next_pos != -1:
prev[next_pos] = time_stamp
if next[time_stamp] == -1:
tail = time_stamp
time_stamp += 1
elif s[0] == 'D':
k = int(s.split()[1])
prev_pos = prev[k]
next_pos = next[k]
next[prev_pos] = next_pos
if next_pos != -1:
prev[next_pos] = prev_pos
if next[prev_pos] == -1:
tail = prev_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)))
python?