Created
September 5, 2011 13:41
-
-
Save jubstuff/1195014 to your computer and use it in GitHub Desktop.
Stack and Queue in Ruby using array
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
# queue.rb | |
# | |
# Using array as queue | |
# | |
# Combine use of push and shift | |
# | |
# =OUTPUT= | |
# | |
# $ ruby1.9.1 queue.rb | |
# ["one", "two", "three"] | |
# one | |
# two | |
# three | |
# [] | |
# | |
queue = [] | |
queue.push "one" | |
queue.push "two" | |
queue.push "three" | |
p queue | |
puts queue.shift | |
puts queue.shift | |
puts queue.shift | |
p queue |
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
# stack.rb | |
# | |
# Using array as stack | |
# | |
# Combine use of push and pop | |
# | |
# =OUTPUT= | |
# | |
# $ruby1.9.1 stack.rb | |
# ["one", "two", "three"] | |
# three | |
# two | |
# one | |
# [] | |
# | |
stack = [] | |
stack.push "one" | |
stack.push "two" | |
stack.push "three" | |
p stack | |
puts stack.pop | |
puts stack.pop | |
puts stack.pop | |
p stack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment