Created
November 8, 2012 12:12
-
-
Save koleksiuk/4038437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queue | |
def initialize(val) | |
@head = Container.new(val) | |
end | |
def add(val) | |
temp = Container.new(val) | |
head = @head | |
loop do | |
break unless head.next | |
head = head.next | |
end | |
head.next = temp | |
temp.val = val | |
end | |
def remove(val) | |
head = @head | |
loop do | |
break unless head.next != nil && head.next.val != val | |
head = head.next | |
end | |
if head.next | |
head.next = head.next.next | |
end | |
end | |
def print | |
puts "------------" | |
head = @head | |
loop do | |
puts head.val | |
head = head.next | |
break unless head | |
end | |
end | |
end | |
class Container | |
def initialize(val) | |
@val = val | |
@next = nil | |
end | |
attr_accessor :val, :next | |
end | |
q = Queue.new(3) | |
q.add(4) | |
q.add(6) | |
q.add(19) | |
q.remove(6) | |
q.add(8) | |
q.print | |
# >> ------------ | |
# >> 3 | |
# >> 4 | |
# >> 19 | |
# >> 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment