Question about linked list with multiple pointers.

J

Jae man Lim

Hi, I just learned ruby in class and I've been making a program about a
threaded list, which I add some objects to a linked list and each node
will be connected through multiple pointers of each threads. I've been
working on this all night and it seems like it is not working, and I
have no idea why this wouldn't work.. I am really new to this, so I was
wondering if anyone could help me out, and if my program actually does
make sense that I am on right track... This may look long, but this is a
very simple program, and help would be really appreciated!!

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] = nil
end

#add to threads
itemNode.nextPtrs.keys.each do |i|
current = @threadHeads
# if there is nothing in the thread heads, set item to the thread
heads.
if current.nil?
@threadHeads = 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 = @threadHeads
@threadHeads = itemNode
# otherwise, loop through the list to see where it should be
inserted at.
else
while !(current.nextPtrs).nil? ||
current.nextPtrs.data.send(i) > item.send(i)
current = current.nextPtrs
end
# if current is nil at this point, add it at the end.
if current.nextPtrs.nil?
current.nextPtrs = itemNode
# else, add it at the right position.
else
itemNode.nextPtrs = current.nextPtrs
current.nextPtrs = 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
while current != nil?
current = current.nextPtrs
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top