Created
April 30, 2014 11:16
-
-
Save miwarin/e7a21ed5ba6d6bdc26b9 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
#: coding: utf-8 | |
class Queue | |
def initialize(size) | |
@queue = Array.new(size) | |
@head = 0 | |
@tail = 0 | |
end | |
def get() | |
item = @queue[@head] | |
@head = (@head + 1) % @queue.length | |
return item | |
end | |
def put(item) | |
@queue[@tail] = item | |
@tail = (@tail + 1) % @queue.length | |
end | |
def size() | |
return @queue.length | |
end | |
end | |
def main(argv) | |
max = 5 | |
queue = Queue.new(max) | |
0.upto(max - 1) {|n| | |
queue.put(n) | |
} | |
0.upto(queue.size() - 1) {|n| | |
puts queue.get() | |
} | |
end | |
main(ARGV) | |
=begin | |
>ruby queue0.rb | |
0 | |
1 | |
2 | |
3 | |
4 | |
=end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment