mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-04 02:33:11 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
145
en/codes/ruby/chapter_stack_and_queue/array_deque.rb
Normal file
145
en/codes/ruby/chapter_stack_and_queue/array_deque.rb
Normal file
@@ -0,0 +1,145 @@
|
||||
=begin
|
||||
File: array_deque.rb
|
||||
Created Time: 2024-04-05
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Deque based on circular array ###
|
||||
class ArrayDeque
|
||||
### Get deque length ###
|
||||
attr_reader :size
|
||||
|
||||
### Constructor ###
|
||||
def initialize(capacity)
|
||||
@nums = Array.new(capacity, 0)
|
||||
@front = 0
|
||||
@size = 0
|
||||
end
|
||||
|
||||
### Get deque capacity ###
|
||||
def capacity
|
||||
@nums.length
|
||||
end
|
||||
|
||||
### Check if deque is empty ###
|
||||
def is_empty?
|
||||
size.zero?
|
||||
end
|
||||
|
||||
### Enqueue at front ###
|
||||
def push_first(num)
|
||||
if size == capacity
|
||||
puts 'Double-ended queue is full'
|
||||
return
|
||||
end
|
||||
|
||||
# Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
# Add num to the front of the queue
|
||||
@front = index(@front - 1)
|
||||
# Add num to front of queue
|
||||
@nums[@front] = num
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### Enqueue at rear ###
|
||||
def push_last(num)
|
||||
if size == capacity
|
||||
puts 'Double-ended queue is full'
|
||||
return
|
||||
end
|
||||
|
||||
# Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
rear = index(@front + size)
|
||||
# Front pointer moves one position backward
|
||||
@nums[rear] = num
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### Dequeue from front ###
|
||||
def pop_first
|
||||
num = peek_first
|
||||
# Move front pointer backward by one position
|
||||
@front = index(@front + 1)
|
||||
@size -= 1
|
||||
num
|
||||
end
|
||||
|
||||
### Dequeue from rear ###
|
||||
def pop_last
|
||||
num = peek_last
|
||||
@size -= 1
|
||||
num
|
||||
end
|
||||
|
||||
### Access front element ###
|
||||
def peek_first
|
||||
raise IndexError, 'Deque is empty' if is_empty?
|
||||
|
||||
@nums[@front]
|
||||
end
|
||||
|
||||
### Access rear element ###
|
||||
def peek_last
|
||||
raise IndexError, 'Deque is empty' if is_empty?
|
||||
|
||||
# Initialize double-ended queue
|
||||
last = index(@front + size - 1)
|
||||
@nums[last]
|
||||
end
|
||||
|
||||
### Return array for printing ###
|
||||
def to_array
|
||||
# Elements enqueue
|
||||
res = []
|
||||
for i in 0...size
|
||||
res << @nums[index(@front + i)]
|
||||
end
|
||||
res
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
### Calculate circular array index ###
|
||||
def index(i)
|
||||
# Use modulo operation to wrap the array head and tail together
|
||||
# When i passes the tail of the array, return to the head
|
||||
# When i passes the head of the array, return to the tail
|
||||
(i + capacity) % capacity
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Get the length of the double-ended queue
|
||||
deque = ArrayDeque.new(10)
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
puts "Deque deque = #{deque.to_array}"
|
||||
|
||||
# Update element
|
||||
peek_first = deque.peek_first
|
||||
puts "Front element peek_first = #{peek_first}"
|
||||
peek_last = deque.peek_last
|
||||
puts "Rear element peek_last = #{peek_last}"
|
||||
|
||||
# Elements enqueue
|
||||
deque.push_last(4)
|
||||
puts "After element 4 enqueues at rear, deque = #{deque.to_array}"
|
||||
deque.push_first(1)
|
||||
puts "After element 1 enqueues at rear, deque = #{deque.to_array}"
|
||||
|
||||
# Element dequeue
|
||||
pop_last = deque.pop_last
|
||||
puts "Dequeue rear element = #{pop_last}, after dequeue deque = #{deque.to_array}"
|
||||
pop_first = deque.pop_first
|
||||
puts "Dequeue front element = #{pop_first}, after dequeue deque = #{deque.to_array}"
|
||||
|
||||
# Get the length of the double-ended queue
|
||||
size = deque.size
|
||||
puts "Deque length size = #{size}"
|
||||
|
||||
# Check if the double-ended queue is empty
|
||||
is_empty = deque.is_empty?
|
||||
puts "Is deque empty = #{is_empty}"
|
||||
end
|
||||
107
en/codes/ruby/chapter_stack_and_queue/array_queue.rb
Normal file
107
en/codes/ruby/chapter_stack_and_queue/array_queue.rb
Normal file
@@ -0,0 +1,107 @@
|
||||
=begin
|
||||
File: array_queue.rb
|
||||
Created Time: 2024-04-05
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Queue based on circular array ###
|
||||
class ArrayQueue
|
||||
### Get queue length ###
|
||||
attr_reader :size
|
||||
|
||||
### Constructor ###
|
||||
def initialize(size)
|
||||
@nums = Array.new(size, 0) # Array for storing queue elements
|
||||
@front = 0 # Front pointer, points to the front of the queue element
|
||||
@size = 0 # Queue length
|
||||
end
|
||||
|
||||
### Get queue capacity ###
|
||||
def capacity
|
||||
@nums.length
|
||||
end
|
||||
|
||||
### Check if queue is empty ###
|
||||
def is_empty?
|
||||
size.zero?
|
||||
end
|
||||
|
||||
### Enqueue ###
|
||||
def push(num)
|
||||
raise IndexError, 'Queue is full' if size == capacity
|
||||
|
||||
# Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
# Add num to the rear of the queue
|
||||
rear = (@front + size) % capacity
|
||||
# Front pointer moves one position backward
|
||||
@nums[rear] = num
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### Dequeue ###
|
||||
def pop
|
||||
num = peek
|
||||
# Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
@front = (@front + 1) % capacity
|
||||
@size -= 1
|
||||
num
|
||||
end
|
||||
|
||||
### Access front element ###
|
||||
def peek
|
||||
raise IndexError, 'Queue is empty' if is_empty?
|
||||
|
||||
@nums[@front]
|
||||
end
|
||||
|
||||
### Return list for printing ###
|
||||
def to_array
|
||||
res = Array.new(size, 0)
|
||||
j = @front
|
||||
|
||||
for i in 0...size
|
||||
res[i] = @nums[j % capacity]
|
||||
j += 1
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Access front of the queue element
|
||||
queue = ArrayQueue.new(10)
|
||||
|
||||
# Elements enqueue
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "Queue queue = #{queue.to_array}"
|
||||
|
||||
# Return list for printing
|
||||
peek = queue.peek
|
||||
puts "Front element peek = #{peek}"
|
||||
|
||||
# Element dequeue
|
||||
pop = queue.pop
|
||||
puts "Dequeue element pop = #{pop}"
|
||||
puts "After dequeue, queue = #{queue.to_array}"
|
||||
|
||||
# Get the length of the queue
|
||||
size = queue.size
|
||||
puts "Queue length size = #{size}"
|
||||
|
||||
# Check if the queue is empty
|
||||
is_empty = queue.is_empty?
|
||||
puts "Is queue empty = #{is_empty}"
|
||||
|
||||
# Test circular array
|
||||
for i in 0...10
|
||||
queue.push(i)
|
||||
queue.pop
|
||||
puts "After round #{i} of enqueue + dequeue, queue = #{queue.to_array}"
|
||||
end
|
||||
end
|
||||
78
en/codes/ruby/chapter_stack_and_queue/array_stack.rb
Normal file
78
en/codes/ruby/chapter_stack_and_queue/array_stack.rb
Normal file
@@ -0,0 +1,78 @@
|
||||
=begin
|
||||
File: array_stack.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Stack based on array ###
|
||||
class ArrayStack
|
||||
### Constructor ###
|
||||
def initialize
|
||||
@stack = []
|
||||
end
|
||||
|
||||
### Get stack length ###
|
||||
def size
|
||||
@stack.length
|
||||
end
|
||||
|
||||
### Check if stack is empty ###
|
||||
def is_empty?
|
||||
@stack.empty?
|
||||
end
|
||||
|
||||
### Push ###
|
||||
def push(item)
|
||||
@stack << item
|
||||
end
|
||||
|
||||
### Pop ###
|
||||
def pop
|
||||
raise IndexError, 'Stack is empty' if is_empty?
|
||||
|
||||
@stack.pop
|
||||
end
|
||||
|
||||
### Access top element ###
|
||||
def peek
|
||||
raise IndexError, 'Stack is empty' if is_empty?
|
||||
|
||||
@stack.last
|
||||
end
|
||||
|
||||
### Return list for printing ###
|
||||
def to_array
|
||||
@stack
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Access top of the stack element
|
||||
stack = ArrayStack.new
|
||||
|
||||
# Elements push onto stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
puts "Stack stack = #{stack.to_array}"
|
||||
|
||||
# Return list for printing
|
||||
peek = stack.peek
|
||||
puts "Top element peek = #{peek}"
|
||||
|
||||
# Element pop from stack
|
||||
pop = stack.pop
|
||||
puts "Pop element pop = #{pop}"
|
||||
puts "After pop, stack = #{stack.to_array}"
|
||||
|
||||
# Get the length of the stack
|
||||
size = stack.size
|
||||
puts "Stack length size = #{size}"
|
||||
|
||||
# Check if empty
|
||||
is_empty = stack.is_empty?
|
||||
puts "Is stack empty = #{is_empty}"
|
||||
end
|
||||
42
en/codes/ruby/chapter_stack_and_queue/deque.rb
Normal file
42
en/codes/ruby/chapter_stack_and_queue/deque.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
=begin
|
||||
File: deque.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Get the length of the double-ended queue
|
||||
# Ruby has no built-in deque, can only use Array as deque
|
||||
deque = []
|
||||
|
||||
# Element enqueues
|
||||
deque << 2
|
||||
deque << 5
|
||||
deque << 4
|
||||
# Note: due to array, Array#unshift method has O(n) time complexity
|
||||
deque.unshift(3)
|
||||
deque.unshift(1)
|
||||
puts "Deque deque = #{deque}"
|
||||
|
||||
# Update element
|
||||
peek_first = deque.first
|
||||
puts "Front element peek_first = #{peek_first}"
|
||||
peek_last = deque.last
|
||||
puts "Rear element peek_last = #{peek_last}"
|
||||
|
||||
# Element dequeue
|
||||
# Note: due to array, Array#shift method has O(n) time complexity
|
||||
pop_front = deque.shift
|
||||
puts "Dequeue front element pop_front = #{pop_front}, after dequeue deque = #{deque}"
|
||||
pop_back = deque.pop
|
||||
puts "Dequeue rear element pop_back = #{pop_back}, after dequeue deque = #{deque}"
|
||||
|
||||
# Get the length of the double-ended queue
|
||||
size = deque.length
|
||||
puts "Deque length size = #{size}"
|
||||
|
||||
# Check if the double-ended queue is empty
|
||||
is_empty = size.zero?
|
||||
puts "Is deque empty = #{is_empty}"
|
||||
end
|
||||
168
en/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb
Normal file
168
en/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb
Normal file
@@ -0,0 +1,168 @@
|
||||
=begin
|
||||
File: linkedlist_deque.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Doubly linked list node
|
||||
class ListNode
|
||||
attr_accessor :val
|
||||
attr_accessor :next # Successor node reference
|
||||
attr_accessor :prev # Predecessor node reference
|
||||
|
||||
### Constructor ###
|
||||
def initialize(val)
|
||||
@val = val
|
||||
end
|
||||
end
|
||||
|
||||
### Deque based on doubly linked list ###
|
||||
class LinkedListDeque
|
||||
### Get deque length ###
|
||||
attr_reader :size
|
||||
|
||||
### Constructor ###
|
||||
def initialize
|
||||
@front = nil # Head node front
|
||||
@rear = nil # Tail node rear
|
||||
@size = 0 # Length of the double-ended queue
|
||||
end
|
||||
|
||||
### Check if deque is empty ###
|
||||
def is_empty?
|
||||
size.zero?
|
||||
end
|
||||
|
||||
### Enqueue operation ###
|
||||
def push(num, is_front)
|
||||
node = ListNode.new(num)
|
||||
# If list is empty, set both front and rear to node
|
||||
if is_empty?
|
||||
@front = @rear = node
|
||||
# Front of the queue enqueue operation
|
||||
elsif is_front
|
||||
# Add node to the head of the linked list
|
||||
@front.prev = node
|
||||
node.next = @front
|
||||
@front = node # Update head node
|
||||
# Rear of the queue enqueue operation
|
||||
else
|
||||
# Add node to the tail of the linked list
|
||||
@rear.next = node
|
||||
node.prev = @rear
|
||||
@rear = node # Update tail node
|
||||
end
|
||||
@size += 1 # Update queue length
|
||||
end
|
||||
|
||||
### Enqueue at front ###
|
||||
def push_first(num)
|
||||
push(num, true)
|
||||
end
|
||||
|
||||
### Enqueue at rear ###
|
||||
def push_last(num)
|
||||
push(num, false)
|
||||
end
|
||||
|
||||
### Dequeue operation ###
|
||||
def pop(is_front)
|
||||
raise IndexError, 'Deque is empty' if is_empty?
|
||||
|
||||
# Temporarily store head node value
|
||||
if is_front
|
||||
val = @front.val # Delete head node
|
||||
# Delete head node
|
||||
fnext = @front.next
|
||||
unless fnext.nil?
|
||||
fnext.prev = nil
|
||||
@front.next = nil
|
||||
end
|
||||
@front = fnext # Update head node
|
||||
# Temporarily store tail node value
|
||||
else
|
||||
val = @rear.val # Delete tail node
|
||||
# Update tail node
|
||||
rprev = @rear.prev
|
||||
unless rprev.nil?
|
||||
rprev.next = nil
|
||||
@rear.prev = nil
|
||||
end
|
||||
@rear = rprev # Update tail node
|
||||
end
|
||||
@size -= 1 # Update queue length
|
||||
|
||||
val
|
||||
end
|
||||
|
||||
### Dequeue from front ###
|
||||
def pop_first
|
||||
pop(true)
|
||||
end
|
||||
|
||||
### Dequeue from front ###
|
||||
def pop_last
|
||||
pop(false)
|
||||
end
|
||||
|
||||
### Access front element ###
|
||||
def peek_first
|
||||
raise IndexError, 'Deque is empty' if is_empty?
|
||||
|
||||
@front.val
|
||||
end
|
||||
|
||||
### Access rear element ###
|
||||
def peek_last
|
||||
raise IndexError, 'Deque is empty' if is_empty?
|
||||
|
||||
@rear.val
|
||||
end
|
||||
|
||||
### Return array for printing ###
|
||||
def to_array
|
||||
node = @front
|
||||
res = Array.new(size, 0)
|
||||
for i in 0...size
|
||||
res[i] = node.val
|
||||
node = node.next
|
||||
end
|
||||
res
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Get the length of the double-ended queue
|
||||
deque = LinkedListDeque.new
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
puts "Deque deque = #{deque.to_array}"
|
||||
|
||||
# Update element
|
||||
peek_first = deque.peek_first
|
||||
puts "Front element peek_first = #{peek_first}"
|
||||
peek_last = deque.peek_last
|
||||
puts "Rear element peek_last = #{peek_last}"
|
||||
|
||||
# Elements enqueue
|
||||
deque.push_last(4)
|
||||
puts "After element 4 enqueues at rear, deque = #{deque.to_array}"
|
||||
deque.push_first(1)
|
||||
puts "After element 1 enqueues at front, deque = #{deque.to_array}"
|
||||
|
||||
# Element dequeue
|
||||
pop_last = deque.pop_last
|
||||
puts "Dequeue rear element = #{pop_last}, after dequeue deque = #{deque.to_array}"
|
||||
pop_first = deque.pop_first
|
||||
puts "Dequeue front element = #{pop_first}, after dequeue deque = #{deque.to_array}"
|
||||
|
||||
# Get the length of the double-ended queue
|
||||
size = deque.size
|
||||
puts "Deque length size = #{size}"
|
||||
|
||||
# Check if the double-ended queue is empty
|
||||
is_empty = deque.is_empty?
|
||||
puts "Is deque empty = #{is_empty}"
|
||||
end
|
||||
101
en/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb
Normal file
101
en/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb
Normal file
@@ -0,0 +1,101 @@
|
||||
=begin
|
||||
File: linkedlist_queue.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative '../utils/list_node'
|
||||
|
||||
### Queue based on linked list ###
|
||||
class LinkedListQueue
|
||||
### Get queue length ###
|
||||
attr_reader :size
|
||||
|
||||
### Constructor ###
|
||||
def initialize
|
||||
@front = nil # Head node front
|
||||
@rear = nil # Tail node rear
|
||||
@size = 0
|
||||
end
|
||||
|
||||
### Check if queue is empty ###
|
||||
def is_empty?
|
||||
@front.nil?
|
||||
end
|
||||
|
||||
### Enqueue ###
|
||||
def push(num)
|
||||
# Add num after the tail node
|
||||
node = ListNode.new(num)
|
||||
|
||||
# If queue is empty, set both front and rear to this node
|
||||
if @front.nil?
|
||||
@front = node
|
||||
@rear = node
|
||||
# If queue is not empty, add this node after rear
|
||||
else
|
||||
@rear.next = node
|
||||
@rear = node
|
||||
end
|
||||
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### Dequeue ###
|
||||
def pop
|
||||
num = peek
|
||||
# Delete head node
|
||||
@front = @front.next
|
||||
@size -= 1
|
||||
num
|
||||
end
|
||||
|
||||
### Access front element ###
|
||||
def peek
|
||||
raise IndexError, 'Queue is empty' if is_empty?
|
||||
|
||||
@front.val
|
||||
end
|
||||
|
||||
### Convert linked list to Array and return ###
|
||||
def to_array
|
||||
queue = []
|
||||
temp = @front
|
||||
while temp
|
||||
queue << temp.val
|
||||
temp = temp.next
|
||||
end
|
||||
queue
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Access front of the queue element
|
||||
queue = LinkedListQueue.new
|
||||
|
||||
# Element enqueues
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "Queue queue = #{queue.to_array}"
|
||||
|
||||
# Return list for printing
|
||||
peek = queue.peek
|
||||
puts "Front element = #{peek}"
|
||||
|
||||
# Element dequeue
|
||||
pop_front = queue.pop
|
||||
puts "Dequeue element pop = #{pop_front}"
|
||||
puts "After dequeue, queue = #{queue.to_array}"
|
||||
|
||||
# Get the length of the queue
|
||||
size = queue.size
|
||||
puts "Queue length size = #{size}"
|
||||
|
||||
# Check if the queue is empty
|
||||
is_empty = queue.is_empty?
|
||||
puts "Is queue empty = #{is_empty}"
|
||||
end
|
||||
87
en/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb
Normal file
87
en/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb
Normal file
@@ -0,0 +1,87 @@
|
||||
=begin
|
||||
File: linkedlist_stack.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative '../utils/list_node'
|
||||
|
||||
### Stack based on linked list ###
|
||||
class LinkedListStack
|
||||
attr_reader :size
|
||||
|
||||
### Constructor ###
|
||||
def initialize
|
||||
@size = 0
|
||||
end
|
||||
|
||||
### Check if stack is empty ###
|
||||
def is_empty?
|
||||
@peek.nil?
|
||||
end
|
||||
|
||||
### Push ###
|
||||
def push(val)
|
||||
node = ListNode.new(val)
|
||||
node.next = @peek
|
||||
@peek = node
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### Pop ###
|
||||
def pop
|
||||
num = peek
|
||||
@peek = @peek.next
|
||||
@size -= 1
|
||||
num
|
||||
end
|
||||
|
||||
### Access top element ###
|
||||
def peek
|
||||
raise IndexError, 'Stack is empty' if is_empty?
|
||||
|
||||
@peek.val
|
||||
end
|
||||
|
||||
### Convert linked list to Array and return ###
|
||||
def to_array
|
||||
arr = []
|
||||
node = @peek
|
||||
while node
|
||||
arr << node.val
|
||||
node = node.next
|
||||
end
|
||||
arr.reverse
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Access top of the stack element
|
||||
stack = LinkedListStack.new
|
||||
|
||||
# Elements push onto stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
puts "Stack stack = #{stack.to_array}"
|
||||
|
||||
# Return list for printing
|
||||
peek = stack.peek
|
||||
puts "Top element peek = #{peek}"
|
||||
|
||||
# Element pop from stack
|
||||
pop = stack.pop
|
||||
puts "Pop element pop = #{pop}"
|
||||
puts "After pop, stack = #{stack.to_array}"
|
||||
|
||||
# Get the length of the stack
|
||||
size = stack.size
|
||||
puts "Stack length size = #{size}"
|
||||
|
||||
# Check if empty
|
||||
is_empty = stack.is_empty?
|
||||
puts "Is stack empty = #{is_empty}"
|
||||
end
|
||||
38
en/codes/ruby/chapter_stack_and_queue/queue.rb
Normal file
38
en/codes/ruby/chapter_stack_and_queue/queue.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
=begin
|
||||
File: queue.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Access front of the queue element
|
||||
# Ruby's built-in queue (Thread::Queue) has no peek and traversal methods, can use Array as queue
|
||||
queue = []
|
||||
|
||||
# Elements enqueue
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "Queue queue = #{queue}"
|
||||
|
||||
# Access queue elements
|
||||
peek = queue.first
|
||||
puts "Front element peek = #{peek}"
|
||||
|
||||
# Element dequeue
|
||||
# Note: due to array, Array#shift method has O(n) time complexity
|
||||
pop = queue.shift
|
||||
puts "Dequeue element pop = #{pop}"
|
||||
puts "After dequeue, queue = #{queue}"
|
||||
|
||||
# Get the length of the queue
|
||||
size = queue.length
|
||||
puts "Queue length size = #{size}"
|
||||
|
||||
# Check if the queue is empty
|
||||
is_empty = queue.empty?
|
||||
puts "Is queue empty = #{is_empty}"
|
||||
end
|
||||
37
en/codes/ruby/chapter_stack_and_queue/stack.rb
Normal file
37
en/codes/ruby/chapter_stack_and_queue/stack.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
=begin
|
||||
File: stack.rb
|
||||
Created Time: 2024-04-06
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Access top of the stack element
|
||||
# Ruby has no built-in stack class, can use Array as stack
|
||||
stack = []
|
||||
|
||||
# Elements push onto stack
|
||||
stack << 1
|
||||
stack << 3
|
||||
stack << 2
|
||||
stack << 5
|
||||
stack << 4
|
||||
puts "Stack stack = #{stack}"
|
||||
|
||||
# Return list for printing
|
||||
peek = stack.last
|
||||
puts "Top element peek = #{peek}"
|
||||
|
||||
# Element pop from stack
|
||||
pop = stack.pop
|
||||
puts "Pop element pop = #{pop}"
|
||||
puts "After pop, stack = #{stack}"
|
||||
|
||||
# Get the length of the stack
|
||||
size = stack.length
|
||||
puts "Stack length size = #{size}"
|
||||
|
||||
# Check if empty
|
||||
is_empty = stack.empty?
|
||||
puts "Is stack empty = #{is_empty}"
|
||||
end
|
||||
Reference in New Issue
Block a user