# your code goes here
class List
class Cell
attr_accessor :data,:prev,:nextA
def initialize(data,prev=nil,nextA=nil)
@data=data
@prev=prev
@nextA=nextA
end
end
def initialize()
@top=Cell.new(nil)
end
attr_accessor :top
protected :top,:top=
def del_first
if @top.nextA
@top.nextA=@top.nextA.nextA
end
if @top.nextA==nil
@top.prev=nil
else
@top.nextA.prev=@top
end
end
def insert_first(data)
c1=Cell.new(data,@top,@top.nextA)
if @top.nextA==nil
@top.prev=c1
else
@top.nextA.prev=c1
end
@top.nextA=c1
end
def dell_last
c1=@top.prev
if c1
c1.prev.nextA=nil
if !(@top==c1.prev)
@top.prev=c1.prev
else
@top.prev=nil
end
end
end
def find_dell(data)
c1=@top
while c1.nextA
if c1.nextA.data==data
c2=c1.nextA.nextA
c1.nextA=c2
if c2
c2.prev=c1
else
if !(c1==top)
@top.prev=c1
else
@top.prev=nil
end
end
return
end
c1=c1.nextA
end
end
def to_s
a=[]
c=@top
while c.nextA
a.push(c.nextA.data)
c=c.nextA
end
a.join(" ")
end
end
ls=List.new()
n=STDIN.gets.chomp.to_i
n.times do |i|
as=STDIN.gets.split(" ")
if(as.size==1)
case as[0]
when "deleteFirst"
ls.del_first
when "deleteLast"
ls.dell_last
end
else
case as[0]
when "insert"
ls.insert_first(as[1])
when "delete"
ls.find_dell(as[1])
end
end
end
puts ls.to_s
最終更新:2016年05月30日 05:23