# Question with linked list with multiple next pointers

**URL:** <https://www.ruby-forum.com/t/question-with-linked-list-with-multiple-next-pointers/226044>\
**Category:** Mongrel\
**Created:** [July 24, 2009, 7:51am UTC](https://www.ruby-forum.com/t/question-with-linked-list-with-multiple-next-pointers/226044 "2009-07-24T07:51:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![risknet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@risknet](https://www.ruby-forum.com/u/risknet)\
**Post date:** [July 24, 2009, 7:51am UTC](https://www.ruby-forum.com/t/question-with-linked-list-with-multiple-next-pointers/226044/1 "2009-07-24T07:51:02Z")

</div>

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! ☹

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()
