Question with linked list with multiple next pointers

Hi, I just learned some basic stuff about ruby, and I’ve been making a
program about a list with different threads and adding some objects to
the list with multiple pointers. I think my program is supposed to work
fine, but it seems like there are some problems, which I cannot really
figure out.
Could anybody help me finding what my problems are, and actually if my
program makes sense? This seems a lot, but this is a very simple
program… please help me! :frowning:

class LibraryItem
def findIndex
end
end

class Movie < LibraryItem
attr_reader :title, :director, :year, :star

def initialize(title, director, year, *star)
@title = title
@director = director
@year = year
@star = star
end

#find out which indexes are going to be applied for a data object for
node.
def findIndex
if star.size != 0
return [“title”, “director”, “year”, “star”, “creator”]
else
return [“title”, “director”, “year”, “creator”]
end
end

def to_s
if star.size != 0
“Movie: title=’#{title}’ director=#{director} year=#{year}
star=#{star}”
else
“Movie: title=’#{title}’ director=#{director} year=#{year}”
end
end
end

class Album < LibraryItem
attr_reader :title, :artist, :year

def initialize(title, artist, year)
@title = title
@artist= artist
@year = year
end

#find out which indexes are going to be applied for a data object for
node.
def findIndex
return [“title”, “artist”, “year”, “creator”]
end

def to_s
“Album: title=’#{title}’ artist=#{artist} year=#{year}”
end
end

class Book < LibraryItem
attr_reader :title, :author, :year, :page

def initialize(title, author, year, page)
@title = title
@author = author
@year = year
@page = page
end

#find out which indexes are going to be applied for a data object for
node.
def findIndex
return [“title”, “author”, “year”, “page”]
end
def to_s
“Book: title=’#{title}’ author=#{author} year=#{year} page=#{page}”
end
end

class ThreadedList
class Node
attr_reader :data, :next, :nextPtrs
attr_writer :nextPtrs, :next
def initialize(data, nextNode)
@data = data
@nextPtrs = {}
@next = nextNode
end
end

def initialize()
@head = nil
@threadHeads = {“director”, “title”, “author”, “year”, “page”,
“artist”, “star”, “creator”}
end
end

def add(item)
#add to main list
itemNode = Node.new(item, @head)
@head = itemNode

# find out the indexes of the item, and if it does not have a method

to find indexes, print an error message.
if item.findIndex.respond_to? “index”
index = item.findIndex
else
puts “Unable to add an item”
0.upto(info.size - 1) do |i|
item.nextPtrs[index[i]] = nil
end

#add to threads
itemNode.nextPtrs.keys.each do |i|
  current = @threadHeads[i]
  # if there is nothing in the thread heads, set item to the thread

heads.
if current.nil?
@threadHeads[i] = itemNode
# else, if the value of the item is bigger than the item to be
inserted, set the pointers before heads.
else
if current.data.send(i) > item.send(i)
itemNode.nextPtrs[i] = @threadHeads[i]
@threadHeads[i] = itemNode
# otherwise, loop through the list to see where it should be
inserted at.
else
while !(current.nextPtrs[i]).nil? ||
current.nextPtrs[i].data.send(i) > item.send(i)
current = current.nextPtrs[i]
end
# if current is nil at this point, add it at the end.
if current.nextPtrs[i].nil?
current.nextPtrs[i] = itemNode
# else, add it at the right position.
else
itemNode.nextPtrs[i] = current.nextPtrs[i]
current.nextPtrs[i] = itemNode
end
end
end
end
end

print out the original list as it was inserted in order of file

reading.
def print
current = @head
while !current.nil?
puts current.data.to_s
current = current.next
end
end

loop through the hash table of the thread heads and print each out.

def printList
@threadHeads.keys.each do |i|
current = @threadHeads[i]
while current != nil?
current = current.nextPtrs[i]
end
end
end
end

token = []

make the threaded list and do file reading.

list = ThreadedList.new
file = File.open(“a4q1.txt”)
file.each do |line|
token = line.split(/,/)

if the data input is an album, tokenize it and add it to the list.

if token[0] == “Album”
album = Album.new(token[1], token[2], token[3])
list.add(album)

if the data input is a moive, tokenize it and add it to the list.

elsif token[0] == “Movie”
if token.size == 4
movie = Movie.new(token[1], token[2], token[3])
else
movie = Movie.new(token[1], token[2], token[3], token[4])
list.add(movie)
end

in other cases, the data will be a book.

else
book = Book.new(token[1], token[2], token[3], token[4])
list.add(book)
end
end
file.close
puts “** Original Data:”
list.print()

puts “** List:”
list.printList()

puts “** Making modifications:”
list.add(“Ruby”)

puts “** Final Result:”
list.printList()