如何为链接列表

时间:2017-04-21 19:01:24

标签: ruby

我正在尝试在Ruby中实现单独的链接列表,它可以正常工作,直到我尝试删除列表中的所有元素。删除元素似乎有效,但删除最后一个元素并调用to_s方法会导致运行时错误,即使我试图遍历列表而下一个Item不等于nil。

这是我的代码:

#----- NODE CLASS -----#
class Node
  attr_accessor :data, :link  # automatically generated getters & setters

  # Constructor/Initializer #
  def initialize(data = nil, link = nil)
      @data = data
      @link = link
  end

  # toString method #
  def to_s
    @data
  end 
end #End node class


#----- AbstractList Class -----#
class AbstractList
  @@listSize

  def getSize
    return @@listSize

  end

  # Constructor/Initializer #
  def initialize()
   @head = nil 
   @@listSize = 0
  end

  # insert at the front of the list #
  def insertFront(item)
    if @head == nil
      @head = Node.new(item)
    else
      temp = Node.new(item)
      temp.link = @head
      @head = temp
    end
    @@listSize += 1
  end

  # insert at the front of the list #
  def insertBack(item)
    if @head == nil
      @head = Node.new(item)
    else
      current = @head

      while current.link != nil
        current = current.link
      end 
      newNode = Node.new(item, nil)
      current.link = newNode
    end
    @@listSize += 1
  end

  # remove from the front of the list #    
  def removeFront()
    if empty()
      raise "List is empty"
    end
    current = @head
    @head = @head.link
    current = nil
    @@listSize -= 1
  end  

  # check if list is empty # 
  def empty()
    result = false
    if @@listSize == 0
      result = true
    end
    return result
  end


  # toString method #
  def to_s
    result = []
    current = @head

    while current.link != nil 
      result << current.data
      current = current.link
    end
    result << current.data
    return result
  end

end #End AbstractList class


#----- MAIN METHOD -----#
#node = Node.new("Ten", "twenty")
#print node.to_s

list = AbstractList.new()
list.insertFront("One")
list.insertFront("Two")
list.insertFront("Three")
list.insertFront("Four")

list.insertBack(10)
list.insertBack(20)
list.insertBack(30)
list.insertBack(40)

print list.to_s 
puts "  List size => #{list.getSize}"

list.removeFront()
list.removeFront()
list.removeFront()
list.removeFront()
list.removeFront()
list.removeFront()
list.removeFront()

print list.to_s
puts "  List size => #{list.getSize}"

list.removeFront()

当我删除最后一个元素并打印时,它打印出以下内容并导致错误:

>["Four", "Three", "Two", "One", 10, 20, 30, 40]  List size => 8
>[40]  List size => 1
>List size => 0
>in `to_s': undefined method `link' for nil:NilClass (NoMethodError)

并且错误指向该行

while current.link != nil

在抽象列表的to_s方法中。

1 个答案:

答案 0 :(得分:0)

删除最后一个元素后,@head设置为nil 现在,您在列表中调用to_s

def to_s
  result = []
  current = @head

  while current.link != nil 
    result << current.data
    current = current.link
  end
  result << current.data
  return result
end

此处current = @head分配给nil

在下一步while current.link != nil中,它会检查link nil是否发生了错误。

解决方案:将您的while条件更改为while current and current.link != nil

def to_s
  result = []
  current = @head

  while current and current.link != nil 
    result << current.data
    current = current.link
  end
  result << current.data if current
  return result
end