Skip to content

Instantly share code, notes, and snippets.

@jubstuff
Created September 5, 2011 13:41
Show Gist options
  • Save jubstuff/1195014 to your computer and use it in GitHub Desktop.
Save jubstuff/1195014 to your computer and use it in GitHub Desktop.
Stack and Queue in Ruby using array
# 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
# 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