Ruby Logo

Queue & Stack Patterns

Master queue (FIFO) and stack (LIFO) patterns with Ruby implementations and practical applications.

Home Ruby Queue & Stack Patterns

Queue & Stack Patterns

Queues and stacks are fundamental data structures that follow specific access patterns. Queues use FIFO (First In, First Out) while stacks use LIFO (Last In, First Out). Ruby provides built-in Queue class and you can implement stacks using arrays.

Queue (FIFO) - First In, First Out

Queue: A queue follows FIFO (First In, First Out) principle. Elements are added to the rear and removed from the front. Perfect for task processing, breadth-first search, and maintaining order.

Ruby's Built-in Queue Class

# Thread-safe queue operations
require
'thread'

queue = Queue.new

# Adding elements (enqueue)
queue << "first"
queue.push("second")
queue.enq("third")

# Removing elements (dequeue)
puts
queue.pop
# "first" (FIFO)
puts
queue.deq
# "second"
puts
queue.shift
# "third"

# Queue properties
puts
queue.empty?
# true
puts
queue.size
# 0

Array-based Queue Implementation

# Simple queue using array
class
SimpleQueue
def initialize
@items = []
end
def enqueue(item)
@items << item
end
def dequeue
@items.shift
end
def empty?
@items.empty?
end
def size
@items.size
end
def peek
@items.first
end
end

Stack (LIFO) - Last In, First Out

Stack: A stack follows LIFO (Last In, First Out) principle. Elements are added and removed from the same end (top). Perfect for function calls, undo operations, and depth-first search.

Array-based Stack Implementation

# Stack using array (push/pop from end)
stack = []

# Adding elements (push)
stack.push("first")
stack << "second"
stack.push("third")

# Removing elements (pop)
puts
stack.pop
# "third" (LIFO)
puts
stack.pop
# "second"
puts
stack.pop
# "first"

# Stack properties
puts
stack.empty?
# true
puts
stack.size
# 0

Custom Stack Class

# Custom stack class
class
Stack
def initialize
@items = []
end
def push(item)
@items << item
end
def pop
@items.pop
end
def peek
@items.last
end
def empty?
@items.empty?
end
def size
@items.size
end
end

Practical Applications

Queue Applications

# Task processing queue
class
TaskProcessor
def initialize
@queue = []
end
def add_task(task)
@queue << task
puts "Added task: #{task}"
end
def process_next
return nil if @queue.empty?
task = @queue.shift
puts "Processing: #{task}"
task
end
def queue_size
@queue.size
end
end

processor = TaskProcessor.new
processor.add_task("Send email")
processor.add_task("Generate report")
processor.add_task("Backup database")

processor.process_next
# "Send email"
processor.process_next
# "Generate report"

Stack Applications

# Undo/Redo functionality
class
UndoManager
def initialize
@undo_stack = []
@redo_stack = []
end
def do_action(action)
@undo_stack.push(action)
@redo_stack.clear
puts "Did: #{action}"
end
def undo
return nil if @undo_stack.empty?
action = @undo_stack.pop
@redo_stack.push(action)
puts "Undid: #{action}"
action
end
def redo
return nil if @redo_stack.empty?
action = @redo_stack.pop
@undo_stack.push(action)
puts "Redid: #{action}"
action
end
end

Interactive Practice: Queue & Stack Patterns

Practice Time: Try these queue and stack examples and experiment with different FIFO/LIFO patterns. Understanding these data structures will make you a more effective Ruby programmer.

Interactive Code Runner

Ruby Code Editor
Output will appear here when you run the code...

Queue & Stack Best Practices

✅ Do This

  • Use Queue for thread-safe operations
  • Use arrays for simple queue/stack implementations
  • Implement proper error handling for empty structures
  • Use queues for task processing and BFS
  • Use stacks for undo/redo and DFS

❌ Avoid This

  • Using wrong data structure for the problem
  • Not handling empty queue/stack cases
  • Using Queue when thread safety isn't needed
  • Forgetting FIFO vs LIFO principles
  • Not considering performance implications

Queue & Stack Mastery Checklist

  • Queue (FIFO): Understand First In, First Out principle
  • Stack (LIFO): Understand Last In, First Out principle
  • Built-in Queue: Use Ruby's thread-safe Queue class
  • Array Implementation: Implement queues and stacks with arrays
  • Operations: Master enqueue/dequeue and push/pop operations
  • Applications: Apply to task processing, undo/redo, and algorithms
  • Error Handling: Handle empty queue/stack cases properly
  • Performance: Understand time and space complexity

Next Steps: Practice with queues for task processing and stacks for undo operations. These fundamental data structures are essential for many algorithms and real-world applications!

Quick Navigation

Related Topics

Video Tutorial

Watch and learn queue & stack patterns

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!