mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:06:28 +08:00
chore: update driver code (#1265)
This commit is contained in:
parent
a3ee733e7b
commit
6404a53fc6
18 changed files with 439 additions and 421 deletions
|
@ -76,32 +76,33 @@ end
|
|||
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化数组
|
||||
arr = Array.new(5, 0)
|
||||
puts "数组 arr = #{arr}"
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
puts "数组 nums = #{nums}"
|
||||
|
||||
# 初始化数组
|
||||
arr = Array.new(5, 0)
|
||||
puts "数组 arr = #{arr}"
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
puts "数组 nums = #{nums}"
|
||||
# 随机访问
|
||||
random_num = random_access(nums)
|
||||
puts "在 nums 中获取随机元素 #{random_num}"
|
||||
|
||||
# 随机访问
|
||||
random_num = random_access(nums)
|
||||
puts "在 nums 中获取随机元素 #{random_num}"
|
||||
# 长度扩展
|
||||
nums = extend(nums, 3)
|
||||
puts "将数组长度扩展至 8 ,得到 nums = #{nums}"
|
||||
|
||||
# 长度扩展
|
||||
nums = extend(nums, 3)
|
||||
puts "将数组长度扩展至 8 ,得到 nums = #{nums}"
|
||||
# 插入元素
|
||||
insert(nums, 6, 3)
|
||||
puts "在索引 3 处插入数字 6 ,得到 nums = #{nums}"
|
||||
|
||||
# 插入元素
|
||||
insert(nums, 6, 3)
|
||||
puts "在索引 3 处插入数字 6 ,得到 nums = #{nums}"
|
||||
# 删除元素
|
||||
remove(nums, 2)
|
||||
puts "删除索引 2 处的元素,得到 nums = #{nums}"
|
||||
|
||||
# 删除元素
|
||||
remove(nums, 2)
|
||||
puts "删除索引 2 处的元素,得到 nums = #{nums}"
|
||||
# 遍历数组
|
||||
traverse(nums)
|
||||
|
||||
# 遍历数组
|
||||
traverse(nums)
|
||||
|
||||
# 查找元素
|
||||
index = find(nums, 3)
|
||||
puts "在 nums 中查找元素 3 ,得到索引 = #{index}"
|
||||
# 查找元素
|
||||
index = find(nums, 3)
|
||||
puts "在 nums 中查找元素 3 ,得到索引 = #{index}"
|
||||
end
|
||||
|
|
|
@ -48,35 +48,36 @@ def find(head, target)
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化链表
|
||||
# 初始化各个节点
|
||||
n0 = ListNode.new(1)
|
||||
n1 = ListNode.new(3)
|
||||
n2 = ListNode.new(2)
|
||||
n3 = ListNode.new(5)
|
||||
n4 = ListNode.new(4)
|
||||
# 构建节点之间的引用
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
puts "初始化的链表为"
|
||||
print_linked_list(n0)
|
||||
|
||||
# 初始化链表
|
||||
# 初始化各个节点
|
||||
n0 = ListNode.new(1)
|
||||
n1 = ListNode.new(3)
|
||||
n2 = ListNode.new(2)
|
||||
n3 = ListNode.new(5)
|
||||
n4 = ListNode.new(4)
|
||||
# 构建节点之间的引用
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
puts "初始化的链表为"
|
||||
print_linked_list(n0)
|
||||
# 插入节点
|
||||
insert(n0, ListNode.new(0))
|
||||
print_linked_list n0
|
||||
|
||||
# 插入节点
|
||||
insert(n0, ListNode.new(0))
|
||||
print_linked_list n0
|
||||
# 删除节点
|
||||
remove(n0)
|
||||
puts "删除节点后的链表为"
|
||||
print_linked_list(n0)
|
||||
|
||||
# 删除节点
|
||||
remove(n0)
|
||||
puts "删除节点后的链表为"
|
||||
print_linked_list(n0)
|
||||
# 访问节点
|
||||
node = access(n0, 3)
|
||||
puts "链表中索引 3 处的节点的值 = #{node.val}"
|
||||
|
||||
# 访问节点
|
||||
node = access(n0, 3)
|
||||
puts "链表中索引 3 处的节点的值 = #{node.val}"
|
||||
|
||||
# 查找节点
|
||||
index = find(n0, 2)
|
||||
puts "链表中值为 2 的节点的索引 = #{index}"
|
||||
# 查找节点
|
||||
index = find(n0, 2)
|
||||
puts "链表中值为 2 的节点的索引 = #{index}"
|
||||
end
|
||||
|
|
|
@ -5,55 +5,56 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化列表
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
puts "列表 nums = #{nums}"
|
||||
|
||||
# 初始化列表
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
puts "列表 nums = #{nums}"
|
||||
# 访问元素
|
||||
num = nums[1]
|
||||
puts "访问索引 1 处的元素,得到 num = #{num}"
|
||||
|
||||
# 访问元素
|
||||
num = nums[1]
|
||||
puts "访问索引 1 处的元素,得到 num = #{num}"
|
||||
# 更新元素
|
||||
nums[1] = 0
|
||||
puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums}"
|
||||
|
||||
# 更新元素
|
||||
nums[1] = 0
|
||||
puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums}"
|
||||
# 清空列表
|
||||
nums.clear
|
||||
puts "清空列表后 nums = #{nums}"
|
||||
|
||||
# 清空列表
|
||||
nums.clear
|
||||
puts "清空列表后 nums = #{nums}"
|
||||
# 在尾部添加元素
|
||||
nums << 1
|
||||
nums << 3
|
||||
nums << 2
|
||||
nums << 5
|
||||
nums << 4
|
||||
puts "添加元素后 nums = #{nums}"
|
||||
|
||||
# 在尾部添加元素
|
||||
nums << 1
|
||||
nums << 3
|
||||
nums << 2
|
||||
nums << 5
|
||||
nums << 4
|
||||
puts "添加元素后 nums = #{nums}"
|
||||
# 在中间插入元素
|
||||
nums.insert(3, 6)
|
||||
puts "在索引 3 处插入元素 6 ,得到 nums = #{nums}"
|
||||
|
||||
# 在中间插入元素
|
||||
nums.insert(3, 6)
|
||||
puts "在索引 3 处插入元素 6 ,得到 nums = #{nums}"
|
||||
# 删除元素
|
||||
nums.delete_at(3)
|
||||
puts "删除索引 3 处的元素,得到 nums = #{nums}"
|
||||
|
||||
# 删除元素
|
||||
nums.delete_at(3)
|
||||
puts "删除索引 3 处的元素,得到 nums = #{nums}"
|
||||
# 通过索引遍历列表
|
||||
count = 0
|
||||
for i in 0...nums.length
|
||||
count += nums[i]
|
||||
end
|
||||
|
||||
# 通过索引遍历列表
|
||||
count = 0
|
||||
for i in 0...nums.length
|
||||
count += nums[i]
|
||||
# 直接遍历列表元素
|
||||
count = 0
|
||||
nums.each do |x|
|
||||
count += x
|
||||
end
|
||||
|
||||
# 拼接两个列表
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
puts "将列表 nums1 拼接到 nums 之后,得到 nums = #{nums}"
|
||||
|
||||
nums = nums.sort { |a, b| a <=> b }
|
||||
puts "排序列表后 nums = #{nums}"
|
||||
end
|
||||
|
||||
# 直接遍历列表元素
|
||||
count = 0
|
||||
nums.each do |x|
|
||||
count += x
|
||||
end
|
||||
|
||||
# 拼接两个列表
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
puts "将列表 nums1 拼接到 nums 之后,得到 nums = #{nums}"
|
||||
|
||||
nums = nums.sort { |a, b| a <=> b }
|
||||
puts "排序列表后 nums = #{nums}"
|
||||
|
|
|
@ -35,7 +35,7 @@ class MyList
|
|||
# 元素数量超出容量时,触发扩容机制
|
||||
extend_capacity if size == capacity
|
||||
@arr[size] = num
|
||||
|
||||
|
||||
# 更新元素数量
|
||||
@size += 1
|
||||
end
|
||||
|
@ -49,7 +49,7 @@ class MyList
|
|||
|
||||
# 将索引 index 以及之后的元素都向后移动一位
|
||||
for j in (size - 1).downto(index)
|
||||
@arr[j + 1] = @arr[j]
|
||||
@arr[j + 1] = @arr[j]
|
||||
end
|
||||
@arr[index] = num
|
||||
|
||||
|
@ -95,37 +95,38 @@ class MyList
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化列表
|
||||
nums = MyList.new
|
||||
|
||||
# 初始化列表
|
||||
nums = MyList.new
|
||||
# 在尾部添加元素
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
puts "列表 nums = #{nums.to_array} ,容量 = #{nums.capacity} ,长度 = #{nums.size}"
|
||||
|
||||
# 在尾部添加元素
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
puts "列表 nums = #{nums.to_array} ,容量 = #{nums.capacity} ,长度 = #{nums.size}"
|
||||
# 在中间插入元素
|
||||
nums.insert(3, 6)
|
||||
puts "在索引 3 处插入数字 6 ,得到 nums = #{nums.to_array}"
|
||||
|
||||
# 在中间插入元素
|
||||
nums.insert(3, 6)
|
||||
puts "在索引 3 处插入数字 6 ,得到 nums = #{nums.to_array}"
|
||||
# 删除元素
|
||||
nums.remove(3)
|
||||
puts "删除索引 3 的元素,得到 nums = #{nums.to_array}"
|
||||
|
||||
# 删除元素
|
||||
nums.remove(3)
|
||||
puts "删除索引 3 的元素,得到 nums = #{nums.to_array}"
|
||||
# 访问元素
|
||||
num = nums.get(1)
|
||||
puts "访问索引 1 处的元素,得到 num = #{num}"
|
||||
|
||||
# 访问元素
|
||||
num = nums.get(1)
|
||||
puts "访问索引 1 处的元素,得到 num = #{num}"
|
||||
# 更新元素
|
||||
nums.set(1, 0)
|
||||
puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums.to_array}"
|
||||
|
||||
# 更新元素
|
||||
nums.set(1, 0)
|
||||
puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums.to_array}"
|
||||
|
||||
# 测试扩容机制
|
||||
for i in 0...10
|
||||
# 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
nums.add(i)
|
||||
# 测试扩容机制
|
||||
for i in 0...10
|
||||
# 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
nums.add(i)
|
||||
end
|
||||
puts "扩容后的列表 nums = #{nums.to_array} ,容量 = #{nums.capacity} ,长度 = #{nums.size}"
|
||||
end
|
||||
puts "扩容后的列表 nums = #{nums.to_array} ,容量 = #{nums.capacity} ,长度 = #{nums.size}"
|
||||
|
|
|
@ -62,17 +62,18 @@ def nested_for_loop(n)
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
n = 5
|
||||
res = for_loop(n)
|
||||
puts "\nfor 循环的求和结果 res = #{res}"
|
||||
|
||||
res = for_loop(n)
|
||||
puts "\nfor 循环的求和结果 res = #{res}"
|
||||
res = while_loop(n)
|
||||
puts "\nwhile 循环的求和结果 res = #{res}"
|
||||
|
||||
res = while_loop(n)
|
||||
puts "\nwhile 循环的求和结果 res = #{res}"
|
||||
res = while_loop_ii(n)
|
||||
puts "\nwhile 循环(两次更新)求和结果 res = #{res}"
|
||||
|
||||
res = while_loop_ii(n)
|
||||
puts "\nwhile 循环(两次更新)求和结果 res = #{res}"
|
||||
|
||||
res = nested_for_loop(n)
|
||||
puts "\n双层 for 循环的遍历结果 #{res}"
|
||||
res = nested_for_loop(n)
|
||||
puts "\n双层 for 循环的遍历结果 #{res}"
|
||||
end
|
||||
|
|
|
@ -53,17 +53,18 @@ def fib(n)
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
n = 5
|
||||
res = recur(n)
|
||||
puts "\n递归函数的求和结果 res = #{res}"
|
||||
|
||||
res = recur(n)
|
||||
puts "\n递归函数的求和结果 res = #{res}"
|
||||
res = for_loop_recur(n)
|
||||
puts "\n使用迭代模拟递归求和结果 res = #{res}"
|
||||
|
||||
res = for_loop_recur(n)
|
||||
puts "\n使用迭代模拟递归求和结果 res = #{res}"
|
||||
res = tail_recur(n, 0)
|
||||
puts "\n尾递归函数的求和结果 res = #{res}"
|
||||
|
||||
res = tail_recur(n, 0)
|
||||
puts "\n尾递归函数的求和结果 res = #{res}"
|
||||
|
||||
res = fib(n)
|
||||
puts "\n斐波那契数列的第 #{n} 项为 #{res}"
|
||||
res = fib(n)
|
||||
puts "\n斐波那契数列的第 #{n} 项为 #{res}"
|
||||
end
|
||||
|
|
|
@ -72,20 +72,21 @@ def build_tree(n)
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
n = 5
|
||||
# 常数阶
|
||||
constant(n)
|
||||
|
||||
# 常数阶
|
||||
constant(n)
|
||||
# 线性阶
|
||||
linear(n)
|
||||
linear_recur(n)
|
||||
|
||||
# 线性阶
|
||||
linear(n)
|
||||
linear_recur(n)
|
||||
# 平方阶
|
||||
quadratic(n)
|
||||
quadratic_recur(n)
|
||||
|
||||
# 平方阶
|
||||
quadratic(n)
|
||||
quadratic_recur(n)
|
||||
|
||||
# 指数阶
|
||||
root = build_tree(n)
|
||||
print_tree(root)
|
||||
# 指数阶
|
||||
root = build_tree(n)
|
||||
print_tree(root)
|
||||
end
|
||||
|
|
|
@ -128,37 +128,38 @@ def factorial_recur(n)
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
n = 8
|
||||
puts "输入数据大小 n = #{n}"
|
||||
|
||||
# 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
n = 8
|
||||
puts "输入数据大小 n = #{n}"
|
||||
count = constant(n)
|
||||
puts "常数阶的操作数量 = #{count}"
|
||||
|
||||
count = constant(n)
|
||||
puts "常数阶的操作数量 = #{count}"
|
||||
count = linear(n)
|
||||
puts "线性阶的操作数量 = #{count}"
|
||||
count = array_traversal(Array.new(n, 0))
|
||||
puts "线性阶(遍历数组)的操作数量 = #{count}"
|
||||
|
||||
count = linear(n)
|
||||
puts "线性阶的操作数量 = #{count}"
|
||||
count = array_traversal(Array.new(n, 0))
|
||||
puts "线性阶(遍历数组)的操作数量 = #{count}"
|
||||
count = quadratic(n)
|
||||
puts "平方阶的操作数量 = #{count}"
|
||||
nums = Array.new(n) { |i| n - i } # [n, n-1, ..., 2, 1]
|
||||
count = bubble_sort(nums)
|
||||
puts "平方阶(冒泡排序)的操作数量 = #{count}"
|
||||
|
||||
count = quadratic(n)
|
||||
puts "平方阶的操作数量 = #{count}"
|
||||
nums = Array.new(n) { |i| n - i } # [n, n-1, ..., 2, 1]
|
||||
count = bubble_sort(nums)
|
||||
puts "平方阶(冒泡排序)的操作数量 = #{count}"
|
||||
count = exponential(n)
|
||||
puts "指数阶(循环实现)的操作数量 = #{count}"
|
||||
count = exp_recur(n)
|
||||
puts "指数阶(递归实现)的操作数量 = #{count}"
|
||||
|
||||
count = exponential(n)
|
||||
puts "指数阶(循环实现)的操作数量 = #{count}"
|
||||
count = exp_recur(n)
|
||||
puts "指数阶(递归实现)的操作数量 = #{count}"
|
||||
count = logarithmic(n)
|
||||
puts "对数阶(循环实现)的操作数量 = #{count}"
|
||||
count = log_recur(n)
|
||||
puts "对数阶(递归实现)的操作数量 = #{count}"
|
||||
|
||||
count = logarithmic(n)
|
||||
puts "对数阶(循环实现)的操作数量 = #{count}"
|
||||
count = log_recur(n)
|
||||
puts "对数阶(递归实现)的操作数量 = #{count}"
|
||||
count = linear_log_recur(n)
|
||||
puts "线性对数阶(递归实现)的操作数量 = #{count}"
|
||||
|
||||
count = linear_log_recur(n)
|
||||
puts "线性对数阶(递归实现)的操作数量 = #{count}"
|
||||
|
||||
count = factorial_recur(n)
|
||||
puts "阶乘阶(递归实现)的操作数量 = #{count}"
|
||||
count = factorial_recur(n)
|
||||
puts "阶乘阶(递归实现)的操作数量 = #{count}"
|
||||
end
|
||||
|
|
|
@ -24,11 +24,12 @@ def find_one(nums)
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
|
||||
for i in 0...10
|
||||
n = 100
|
||||
nums = random_numbers(n)
|
||||
index = find_one(nums)
|
||||
puts "\n数组 [ 1, 2, ..., n ] 被打乱后 = #{nums}"
|
||||
puts "数字 1 的索引为 #{index}"
|
||||
if __FILE__ == $0
|
||||
for i in 0...10
|
||||
n = 100
|
||||
nums = random_numbers(n)
|
||||
index = find_one(nums)
|
||||
puts "\n数组 [ 1, 2, ..., n ] 被打乱后 = #{nums}"
|
||||
puts "数字 1 的索引为 #{index}"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -109,36 +109,37 @@ class ArrayDeque
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化双向队列
|
||||
deque = ArrayDeque.new(10)
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
puts "双向队列 deque = #{deque.to_array}"
|
||||
|
||||
# 初始化双向队列
|
||||
deque = ArrayDeque.new(10)
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
puts "双向队列 deque = #{deque.to_array}"
|
||||
# 访问元素
|
||||
peek_first = deque.peek_first
|
||||
puts "队首元素 peek_first = #{peek_first}"
|
||||
peek_last = deque.peek_last
|
||||
puts "队尾元素 peek_last = #{peek_last}"
|
||||
|
||||
# 访问元素
|
||||
peek_first = deque.peek_first
|
||||
puts "队首元素 peek_first = #{peek_first}"
|
||||
peek_last = deque.peek_last
|
||||
puts "队尾元素 peek_last = #{peek_last}"
|
||||
# 元素入队
|
||||
deque.push_last(4)
|
||||
puts "元素 4 队尾入队后 deque = #{deque.to_array}"
|
||||
deque.push_first(1)
|
||||
puts "元素 1 队尾入队后 deque = #{deque.to_array}"
|
||||
|
||||
# 元素入队
|
||||
deque.push_last(4)
|
||||
puts "元素 4 队尾入队后 deque = #{deque.to_array}"
|
||||
deque.push_first(1)
|
||||
puts "元素 1 队尾入队后 deque = #{deque.to_array}"
|
||||
# 元素出队
|
||||
pop_last = deque.pop_last
|
||||
puts "队尾出队元素 = #{pop_last},队尾出队后 deque = #{deque.to_array}"
|
||||
pop_first = deque.pop_first
|
||||
puts "队尾出队元素 = #{pop_first},队尾出队后 deque = #{deque.to_array}"
|
||||
|
||||
# 元素出队
|
||||
pop_last = deque.pop_last
|
||||
puts "队尾出队元素 = #{pop_last},队尾出队后 deque = #{deque.to_array}"
|
||||
pop_first = deque.pop_first
|
||||
puts "队尾出队元素 = #{pop_first},队尾出队后 deque = #{deque.to_array}"
|
||||
# 获取双向队列的长度
|
||||
size = deque.size
|
||||
puts "双向队列长度 size = #{size}"
|
||||
|
||||
# 获取双向队列的长度
|
||||
size = deque.size
|
||||
puts "双向队列长度 size = #{size}"
|
||||
|
||||
# 判断双向队列是否为空
|
||||
is_empty = deque.is_empty?
|
||||
puts "双向队列是否为空 = #{is_empty}"
|
||||
# 判断双向队列是否为空
|
||||
is_empty = deque.is_empty?
|
||||
puts "双向队列是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -69,38 +69,39 @@ class ArrayQueue
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化队列
|
||||
queue = ArrayQueue.new(10)
|
||||
|
||||
# 初始化队列
|
||||
queue = ArrayQueue.new(10)
|
||||
# 元素入队
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "队列 queue = #{queue.to_array}"
|
||||
|
||||
# 元素入队
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "队列 queue = #{queue.to_array}"
|
||||
# 访问队首元素
|
||||
peek = queue.peek
|
||||
puts "队首元素 peek = #{peek}"
|
||||
|
||||
# 访问队首元素
|
||||
peek = queue.peek
|
||||
puts "队首元素 peek = #{peek}"
|
||||
# 元素出队
|
||||
pop = queue.pop
|
||||
puts "出队元素 pop = #{pop}"
|
||||
puts "出队后 queue = #{queue.to_array}"
|
||||
|
||||
# 元素出队
|
||||
pop = queue.pop
|
||||
puts "出队元素 pop = #{pop}"
|
||||
puts "出队后 queue = #{queue.to_array}"
|
||||
# 获取队列的长度
|
||||
size = queue.size
|
||||
puts "队列长度 size = #{size}"
|
||||
|
||||
# 获取队列的长度
|
||||
size = queue.size
|
||||
puts "队列长度 size = #{size}"
|
||||
# 判断队列是否为空
|
||||
is_empty = queue.is_empty?
|
||||
puts "队列是否为空 = #{is_empty}"
|
||||
|
||||
# 判断队列是否为空
|
||||
is_empty = queue.is_empty?
|
||||
puts "队列是否为空 = #{is_empty}"
|
||||
|
||||
# 测试环形数组
|
||||
for i in 0...10
|
||||
queue.push(i)
|
||||
queue.pop
|
||||
puts "第 #{i} 轮入队 + 出队后 queue = #{queue.to_array}"
|
||||
# 测试环形数组
|
||||
for i in 0...10
|
||||
queue.push(i)
|
||||
queue.pop
|
||||
puts "第 #{i} 轮入队 + 出队后 queue = #{queue.to_array}"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -47,31 +47,32 @@ class ArrayStack
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化栈
|
||||
stack = ArrayStack.new
|
||||
|
||||
# 初始化栈
|
||||
stack = ArrayStack.new
|
||||
# 元素入栈
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
puts "栈 stack = #{stack.to_array}"
|
||||
|
||||
# 元素入栈
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
puts "栈 stack = #{stack.to_array}"
|
||||
# 访问栈顶元素
|
||||
peek = stack.peek
|
||||
puts "栈顶元素 peek = #{peek}"
|
||||
|
||||
# 访问栈顶元素
|
||||
peek = stack.peek
|
||||
puts "栈顶元素 peek = #{peek}"
|
||||
# 元素出栈
|
||||
pop = stack.pop
|
||||
puts "出栈元素 pop = #{pop}"
|
||||
puts "出栈后 stack = #{stack.to_array}"
|
||||
|
||||
# 元素出栈
|
||||
pop = stack.pop
|
||||
puts "出栈元素 pop = #{pop}"
|
||||
puts "出栈后 stack = #{stack.to_array}"
|
||||
# 获取栈的长度
|
||||
size = stack.size
|
||||
puts "栈的长度 size = #{size}"
|
||||
|
||||
# 获取栈的长度
|
||||
size = stack.size
|
||||
puts "栈的长度 size = #{size}"
|
||||
|
||||
# 判断是否为空
|
||||
is_empty = stack.is_empty?
|
||||
puts "栈是否为空 = #{is_empty}"
|
||||
# 判断是否为空
|
||||
is_empty = stack.is_empty?
|
||||
puts "栈是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -5,37 +5,38 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化双向队列
|
||||
# Ruby 没有内直的双端队列,只能把 Array 当作双端队列来使用
|
||||
deque = []
|
||||
|
||||
# 初始化双向队列
|
||||
# Ruby 没有内直的双端队列,只能把 Array 当作双端队列来使用
|
||||
deque = []
|
||||
# 元素如队
|
||||
deque << 2
|
||||
deque << 5
|
||||
deque << 4
|
||||
# 请注意,由于是数组,Array#unshift 方法的时间复杂度为 O(n)
|
||||
deque.unshift(3)
|
||||
deque.unshift(1)
|
||||
puts "双向队列 deque = #{deque}"
|
||||
|
||||
# 元素如队
|
||||
deque << 2
|
||||
deque << 5
|
||||
deque << 4
|
||||
# 请注意,由于是数组,Array#unshift 方法的时间复杂度为 O(n)
|
||||
deque.unshift(3)
|
||||
deque.unshift(1)
|
||||
puts "双向队列 deque = #{deque}"
|
||||
# 访问元素
|
||||
peek_first = deque.first
|
||||
puts "队首元素 peek_first = #{peek_first}"
|
||||
peek_last = deque.last
|
||||
puts "队尾元素 peek_last = #{peek_last}"
|
||||
|
||||
# 访问元素
|
||||
peek_first = deque.first
|
||||
puts "队首元素 peek_first = #{peek_first}"
|
||||
peek_last = deque.last
|
||||
puts "队尾元素 peek_last = #{peek_last}"
|
||||
# 元素出队
|
||||
# 请注意,由于是数组, Array#shift 方法的时间复杂度为 O(n)
|
||||
pop_front = deque.shift
|
||||
puts "队首出队元素 pop_front = #{pop_front},队首出队后 deque = #{deque}"
|
||||
pop_back = deque.pop
|
||||
puts "队尾出队元素 pop_back = #{pop_back}, 队尾出队后 deque = #{deque}"
|
||||
|
||||
# 元素出队
|
||||
# 请注意,由于是数组, Array#shift 方法的时间复杂度为 O(n)
|
||||
pop_front = deque.shift
|
||||
puts "队首出队元素 pop_front = #{pop_front},队首出队后 deque = #{deque}"
|
||||
pop_back = deque.pop
|
||||
puts "队尾出队元素 pop_back = #{pop_back}, 队尾出队后 deque = #{deque}"
|
||||
# 获取双向队列的长度
|
||||
size = deque.length
|
||||
puts "双向队列长度 size = #{size}"
|
||||
|
||||
# 获取双向队列的长度
|
||||
size = deque.length
|
||||
puts "双向队列长度 size = #{size}"
|
||||
|
||||
# 判断双向队列是否为空
|
||||
is_empty = size.zero?
|
||||
puts "双向队列是否为空 = #{is_empty}"
|
||||
# 判断双向队列是否为空
|
||||
is_empty = size.zero?
|
||||
puts "双向队列是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -132,36 +132,37 @@ class LinkedListDeque
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化双向队列
|
||||
deque = LinkedListDeque.new
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
puts "双向队列 deque = #{deque.to_array}"
|
||||
|
||||
# 初始化双向队列
|
||||
deque = LinkedListDeque.new
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
puts "双向队列 deque = #{deque.to_array}"
|
||||
# 访问元素
|
||||
peek_first = deque.peek_first
|
||||
puts "队首元素 peek_first = #{peek_first}"
|
||||
peek_last = deque.peek_last
|
||||
puts "队首元素 peek_last = #{peek_last}"
|
||||
|
||||
# 访问元素
|
||||
peek_first = deque.peek_first
|
||||
puts "队首元素 peek_first = #{peek_first}"
|
||||
peek_last = deque.peek_last
|
||||
puts "队首元素 peek_last = #{peek_last}"
|
||||
# 元素入队
|
||||
deque.push_last(4)
|
||||
puts "元素 4 队尾入队后 deque = #{deque.to_array}"
|
||||
deque.push_first(1)
|
||||
puts "元素 1 队首入队后 deque = #{deque.to_array}"
|
||||
|
||||
# 元素入队
|
||||
deque.push_last(4)
|
||||
puts "元素 4 队尾入队后 deque = #{deque.to_array}"
|
||||
deque.push_first(1)
|
||||
puts "元素 1 队首入队后 deque = #{deque.to_array}"
|
||||
# 元素出队
|
||||
pop_last = deque.pop_last
|
||||
puts "队尾出队元素 = #{pop_last}, 队尾出队后 deque = #{deque.to_array}"
|
||||
pop_first = deque.pop_first
|
||||
puts "队首出队元素 = #{pop_first},队首出队后 deque = #{deque.to_array}"
|
||||
|
||||
# 元素出队
|
||||
pop_last = deque.pop_last
|
||||
puts "队尾出队元素 = #{pop_last}, 队尾出队后 deque = #{deque.to_array}"
|
||||
pop_first = deque.pop_first
|
||||
puts "队首出队元素 = #{pop_first},队首出队后 deque = #{deque.to_array}"
|
||||
# 获取双向队列的长度
|
||||
size = deque.size
|
||||
puts "双向队列长度 size = #{size}"
|
||||
|
||||
# 获取双向队列的长度
|
||||
size = deque.size
|
||||
puts "双向队列长度 size = #{size}"
|
||||
|
||||
# 判断双向队列是否为空
|
||||
is_empty = deque.is_empty?
|
||||
puts "双向队列是否为空 = #{is_empty}"
|
||||
# 判断双向队列是否为空
|
||||
is_empty = deque.is_empty?
|
||||
puts "双向队列是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -70,31 +70,32 @@ class LinkedListQueue
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化队列
|
||||
queue = LinkedListQueue.new
|
||||
|
||||
# 初始化队列
|
||||
queue = LinkedListQueue.new
|
||||
# 元素如队
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "队列 queue = #{queue.to_array}"
|
||||
|
||||
# 元素如队
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "队列 queue = #{queue.to_array}"
|
||||
# 访问队首元素
|
||||
peek = queue.peek
|
||||
puts "队首元素 front = #{peek}"
|
||||
|
||||
# 访问队首元素
|
||||
peek = queue.peek
|
||||
puts "队首元素 front = #{peek}"
|
||||
# 元素出队
|
||||
pop_front = queue.pop
|
||||
puts "出队元素 pop = #{pop_front}"
|
||||
puts "出队后 queue = #{queue.to_array}"
|
||||
|
||||
# 元素出队
|
||||
pop_front = queue.pop
|
||||
puts "出队元素 pop = #{pop_front}"
|
||||
puts "出队后 queue = #{queue.to_array}"
|
||||
# 获取队列的长度
|
||||
size = queue.size
|
||||
puts "队列长度 size = #{size}"
|
||||
|
||||
# 获取队列的长度
|
||||
size = queue.size
|
||||
puts "队列长度 size = #{size}"
|
||||
|
||||
# 判断队列是否为空
|
||||
is_empty = queue.is_empty?
|
||||
puts "队列是否为空 = #{is_empty}"
|
||||
# 判断队列是否为空
|
||||
is_empty = queue.is_empty?
|
||||
puts "队列是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -56,31 +56,32 @@ class LinkedListStack
|
|||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化栈
|
||||
stack = LinkedListStack.new
|
||||
|
||||
# 初始化栈
|
||||
stack = LinkedListStack.new
|
||||
# 元素入栈
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
puts "栈 stack = #{stack.to_array}"
|
||||
|
||||
# 元素入栈
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
puts "栈 stack = #{stack.to_array}"
|
||||
# 访问栈顶元素
|
||||
peek = stack.peek
|
||||
puts "栈顶元素 peek = #{peek}"
|
||||
|
||||
# 访问栈顶元素
|
||||
peek = stack.peek
|
||||
puts "栈顶元素 peek = #{peek}"
|
||||
# 元素出栈
|
||||
pop = stack.pop
|
||||
puts "出栈元素 pop = #{pop}"
|
||||
puts "出栈后 stack = #{stack.to_array}"
|
||||
|
||||
# 元素出栈
|
||||
pop = stack.pop
|
||||
puts "出栈元素 pop = #{pop}"
|
||||
puts "出栈后 stack = #{stack.to_array}"
|
||||
# 获取栈的长度
|
||||
size = stack.size
|
||||
puts "栈的长度 size = #{size}"
|
||||
|
||||
# 获取栈的长度
|
||||
size = stack.size
|
||||
puts "栈的长度 size = #{size}"
|
||||
|
||||
# 判断是否为空
|
||||
is_empty = stack.is_empty?
|
||||
puts "栈是否为空 = #{is_empty}"
|
||||
# 判断是否为空
|
||||
is_empty = stack.is_empty?
|
||||
puts "栈是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -5,33 +5,34 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化队列
|
||||
# Ruby 内置的队列(Thread::Queue) 没有 peek 和遍历方法,可以把 Array 当作队列来使用
|
||||
queue = []
|
||||
|
||||
# 初始化队列
|
||||
# Ruby 内置的队列(Thread::Queue) 没有 peek 和遍历方法,可以把 Array 当作队列来使用
|
||||
queue = []
|
||||
# 元素入队
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "队列 queue = #{queue}"
|
||||
|
||||
# 元素入队
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
puts "队列 queue = #{queue}"
|
||||
# 访问队列元素
|
||||
peek = queue.first
|
||||
puts "队首元素 peek = #{peek}"
|
||||
|
||||
# 访问队列元素
|
||||
peek = queue.first
|
||||
puts "队首元素 peek = #{peek}"
|
||||
# 元素出队
|
||||
# 清注意,由于是数组,Array#shift 方法时间复杂度为 O(n)
|
||||
pop = queue.shift
|
||||
puts "出队元素 pop = #{pop}"
|
||||
puts "出队后 queue = #{queue}"
|
||||
|
||||
# 元素出队
|
||||
# 清注意,由于是数组,Array#shift 方法时间复杂度为 O(n)
|
||||
pop = queue.shift
|
||||
puts "出队元素 pop = #{pop}"
|
||||
puts "出队后 queue = #{queue}"
|
||||
# 获取队列的长度
|
||||
size = queue.length
|
||||
puts "队列长度 size = #{size}"
|
||||
|
||||
# 获取队列的长度
|
||||
size = queue.length
|
||||
puts "队列长度 size = #{size}"
|
||||
|
||||
# 判断队列是否为空
|
||||
is_empty = queue.empty?
|
||||
puts "队列是否为空 = #{is_empty}"
|
||||
# 判断队列是否为空
|
||||
is_empty = queue.empty?
|
||||
puts "队列是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
|
@ -5,32 +5,33 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|||
=end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# 初始化栈
|
||||
# Ruby 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
stack = []
|
||||
|
||||
# 初始化栈
|
||||
# Ruby 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
stack = []
|
||||
# 元素入栈
|
||||
stack << 1
|
||||
stack << 3
|
||||
stack << 2
|
||||
stack << 5
|
||||
stack << 4
|
||||
puts "栈 stack = #{stack}"
|
||||
|
||||
# 元素入栈
|
||||
stack << 1
|
||||
stack << 3
|
||||
stack << 2
|
||||
stack << 5
|
||||
stack << 4
|
||||
puts "栈 stack = #{stack}"
|
||||
# 访问栈顶元素
|
||||
peek = stack.last
|
||||
puts "栈顶元素 peek = #{peek}"
|
||||
|
||||
# 访问栈顶元素
|
||||
peek = stack.last
|
||||
puts "栈顶元素 peek = #{peek}"
|
||||
# 元素出栈
|
||||
pop = stack.pop
|
||||
puts "出栈元素 pop = #{pop}"
|
||||
puts "出栈后 stack = #{stack}"
|
||||
|
||||
# 元素出栈
|
||||
pop = stack.pop
|
||||
puts "出栈元素 pop = #{pop}"
|
||||
puts "出栈后 stack = #{stack}"
|
||||
# 获取栈的长度
|
||||
size = stack.length
|
||||
puts "栈的长度 size = #{size}"
|
||||
|
||||
# 获取栈的长度
|
||||
size = stack.length
|
||||
puts "栈的长度 size = #{size}"
|
||||
|
||||
# 判断是否为空
|
||||
is_empty = stack.empty?
|
||||
puts "栈是否为空 = #{is_empty}"
|
||||
# 判断是否为空
|
||||
is_empty = stack.empty?
|
||||
puts "栈是否为空 = #{is_empty}"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue