From 6404a53fc6422dfb285c6ec52c33a18ac16e7da7 Mon Sep 17 00:00:00 2001 From: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:38:48 +0700 Subject: [PATCH] chore: update driver code (#1265) --- .../chapter_array_and_linkedlist/array.rb | 59 +++++----- .../linked_list.rb | 57 +++++----- .../ruby/chapter_array_and_linkedlist/list.rb | 103 +++++++++--------- .../chapter_array_and_linkedlist/my_list.rb | 71 ++++++------ .../iteration.rb | 21 ++-- .../recursion.rb | 21 ++-- .../space_complexity.rb | 27 ++--- .../time_complexity.rb | 69 ++++++------ .../worst_best_time_complexity.rb | 15 +-- .../chapter_stack_and_queue/array_deque.rb | 67 ++++++------ .../chapter_stack_and_queue/array_queue.rb | 69 ++++++------ .../chapter_stack_and_queue/array_stack.rb | 57 +++++----- codes/ruby/chapter_stack_and_queue/deque.rb | 59 +++++----- .../linkedlist_deque.rb | 67 ++++++------ .../linkedlist_queue.rb | 57 +++++----- .../linkedlist_stack.rb | 57 +++++----- codes/ruby/chapter_stack_and_queue/queue.rb | 51 ++++----- codes/ruby/chapter_stack_and_queue/stack.rb | 49 +++++---- 18 files changed, 497 insertions(+), 479 deletions(-) diff --git a/codes/ruby/chapter_array_and_linkedlist/array.rb b/codes/ruby/chapter_array_and_linkedlist/array.rb index 3ad16b1c5..f9febd0d5 100644 --- a/codes/ruby/chapter_array_and_linkedlist/array.rb +++ b/codes/ruby/chapter_array_and_linkedlist/array.rb @@ -76,32 +76,33 @@ end ### Driver Code ### - -# 初始化数组 -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}" - -# 长度扩展 -nums = extend(nums, 3) -puts "将数组长度扩展至 8 ,得到 nums = #{nums}" - -# 插入元素 -insert(nums, 6, 3) -puts "在索引 3 处插入数字 6 ,得到 nums = #{nums}" - -# 删除元素 -remove(nums, 2) -puts "删除索引 2 处的元素,得到 nums = #{nums}" - -# 遍历数组 -traverse(nums) - -# 查找元素 -index = find(nums, 3) -puts "在 nums 中查找元素 3 ,得到索引 = #{index}" +if __FILE__ == $0 + # 初始化数组 + 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}" + + # 长度扩展 + nums = extend(nums, 3) + puts "将数组长度扩展至 8 ,得到 nums = #{nums}" + + # 插入元素 + insert(nums, 6, 3) + puts "在索引 3 处插入数字 6 ,得到 nums = #{nums}" + + # 删除元素 + remove(nums, 2) + puts "删除索引 2 处的元素,得到 nums = #{nums}" + + # 遍历数组 + traverse(nums) + + # 查找元素 + index = find(nums, 3) + puts "在 nums 中查找元素 3 ,得到索引 = #{index}" +end diff --git a/codes/ruby/chapter_array_and_linkedlist/linked_list.rb b/codes/ruby/chapter_array_and_linkedlist/linked_list.rb index 7b05f53cc..14a4f6cf2 100644 --- a/codes/ruby/chapter_array_and_linkedlist/linked_list.rb +++ b/codes/ruby/chapter_array_and_linkedlist/linked_list.rb @@ -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 diff --git a/codes/ruby/chapter_array_and_linkedlist/list.rb b/codes/ruby/chapter_array_and_linkedlist/list.rb index a7d0590e6..b22bba14e 100644 --- a/codes/ruby/chapter_array_and_linkedlist/list.rb +++ b/codes/ruby/chapter_array_and_linkedlist/list.rb @@ -5,55 +5,56 @@ Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end ### Driver Code ### - -# 初始化列表 -nums = [1, 3, 2, 5, 4] -puts "列表 nums = #{nums}" - -# 访问元素 -num = nums[1] -puts "访问索引 1 处的元素,得到 num = #{num}" - -# 更新元素 -nums[1] = 0 -puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums}" - -# 清空列表 -nums.clear -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.delete_at(3) -puts "删除索引 3 处的元素,得到 nums = #{nums}" - -# 通过索引遍历列表 -count = 0 -for i in 0...nums.length - count += nums[i] -end - -# 直接遍历列表元素 -count = 0 -nums.each do |x| - count += x +if __FILE__ == $0 + # 初始化列表 + nums = [1, 3, 2, 5, 4] + puts "列表 nums = #{nums}" + + # 访问元素 + num = nums[1] + puts "访问索引 1 处的元素,得到 num = #{num}" + + # 更新元素 + nums[1] = 0 + puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums}" + + # 清空列表 + nums.clear + 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.delete_at(3) + puts "删除索引 3 处的元素,得到 nums = #{nums}" + + # 通过索引遍历列表 + count = 0 + for i in 0...nums.length + count += nums[i] + 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}" end - -# 拼接两个列表 -nums1 = [6, 8, 7, 10, 9] -nums += nums1 -puts "将列表 nums1 拼接到 nums 之后,得到 nums = #{nums}" - -nums = nums.sort { |a, b| a <=> b } -puts "排序列表后 nums = #{nums}" diff --git a/codes/ruby/chapter_array_and_linkedlist/my_list.rb b/codes/ruby/chapter_array_and_linkedlist/my_list.rb index bb29646ac..c89f7cae3 100644 --- a/codes/ruby/chapter_array_and_linkedlist/my_list.rb +++ b/codes/ruby/chapter_array_and_linkedlist/my_list.rb @@ -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 ### - -# 初始化列表 -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.insert(3, 6) -puts "在索引 3 处插入数字 6 ,得到 nums = #{nums.to_array}" - -# 删除元素 -nums.remove(3) -puts "删除索引 3 的元素,得到 nums = #{nums.to_array}" - -# 访问元素 -num = nums.get(1) -puts "访问索引 1 处的元素,得到 num = #{num}" - -# 更新元素 -nums.set(1, 0) -puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums.to_array}" - -# 测试扩容机制 -for i in 0...10 - # 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 - nums.add(i) +if __FILE__ == $0 + # 初始化列表 + 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.insert(3, 6) + puts "在索引 3 处插入数字 6 ,得到 nums = #{nums.to_array}" + + # 删除元素 + nums.remove(3) + puts "删除索引 3 的元素,得到 nums = #{nums.to_array}" + + # 访问元素 + num = nums.get(1) + puts "访问索引 1 处的元素,得到 num = #{num}" + + # 更新元素 + nums.set(1, 0) + puts "将索引 1 处的元素更新为 0 ,得到 nums = #{nums.to_array}" + + # 测试扩容机制 + 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}" diff --git a/codes/ruby/chapter_computational_complexity/iteration.rb b/codes/ruby/chapter_computational_complexity/iteration.rb index cd926b9bd..5ce574325 100644 --- a/codes/ruby/chapter_computational_complexity/iteration.rb +++ b/codes/ruby/chapter_computational_complexity/iteration.rb @@ -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 diff --git a/codes/ruby/chapter_computational_complexity/recursion.rb b/codes/ruby/chapter_computational_complexity/recursion.rb index f0d68782f..4a83f9643 100644 --- a/codes/ruby/chapter_computational_complexity/recursion.rb +++ b/codes/ruby/chapter_computational_complexity/recursion.rb @@ -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 diff --git a/codes/ruby/chapter_computational_complexity/space_complexity.rb b/codes/ruby/chapter_computational_complexity/space_complexity.rb index 8172aca60..e5fba2913 100644 --- a/codes/ruby/chapter_computational_complexity/space_complexity.rb +++ b/codes/ruby/chapter_computational_complexity/space_complexity.rb @@ -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 diff --git a/codes/ruby/chapter_computational_complexity/time_complexity.rb b/codes/ruby/chapter_computational_complexity/time_complexity.rb index 4af0759fc..cf41db88b 100644 --- a/codes/ruby/chapter_computational_complexity/time_complexity.rb +++ b/codes/ruby/chapter_computational_complexity/time_complexity.rb @@ -128,37 +128,38 @@ def factorial_recur(n) end ### Driver Code ### - -# 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 -n = 8 -puts "输入数据大小 n = #{n}" - -count = constant(n) -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 = exponential(n) -puts "指数阶(循环实现)的操作数量 = #{count}" -count = exp_recur(n) -puts "指数阶(递归实现)的操作数量 = #{count}" - -count = logarithmic(n) -puts "对数阶(循环实现)的操作数量 = #{count}" -count = log_recur(n) -puts "对数阶(递归实现)的操作数量 = #{count}" - -count = linear_log_recur(n) -puts "线性对数阶(递归实现)的操作数量 = #{count}" - -count = factorial_recur(n) -puts "阶乘阶(递归实现)的操作数量 = #{count}" +if __FILE__ == $0 + # 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 + n = 8 + puts "输入数据大小 n = #{n}" + + count = constant(n) + 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 = exponential(n) + puts "指数阶(循环实现)的操作数量 = #{count}" + count = exp_recur(n) + puts "指数阶(递归实现)的操作数量 = #{count}" + + count = logarithmic(n) + puts "对数阶(循环实现)的操作数量 = #{count}" + count = log_recur(n) + puts "对数阶(递归实现)的操作数量 = #{count}" + + count = linear_log_recur(n) + puts "线性对数阶(递归实现)的操作数量 = #{count}" + + count = factorial_recur(n) + puts "阶乘阶(递归实现)的操作数量 = #{count}" +end diff --git a/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb b/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb index 4c0cd4776..206dc265e 100644 --- a/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb +++ b/codes/ruby/chapter_computational_complexity/worst_best_time_complexity.rb @@ -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 diff --git a/codes/ruby/chapter_stack_and_queue/array_deque.rb b/codes/ruby/chapter_stack_and_queue/array_deque.rb index ba4b78058..91e4ff71f 100644 --- a/codes/ruby/chapter_stack_and_queue/array_deque.rb +++ b/codes/ruby/chapter_stack_and_queue/array_deque.rb @@ -109,36 +109,37 @@ class ArrayDeque end ### Driver Code ### - -# 初始化双向队列 -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}" - -# 元素入队 -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}" - -# 获取双向队列的长度 -size = deque.size -puts "双向队列长度 size = #{size}" - -# 判断双向队列是否为空 -is_empty = deque.is_empty? -puts "双向队列是否为空 = #{is_empty}" +if __FILE__ == $0 + # 初始化双向队列 + 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}" + + # 元素入队 + 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}" + + # 获取双向队列的长度 + size = deque.size + puts "双向队列长度 size = #{size}" + + # 判断双向队列是否为空 + is_empty = deque.is_empty? + puts "双向队列是否为空 = #{is_empty}" +end diff --git a/codes/ruby/chapter_stack_and_queue/array_queue.rb b/codes/ruby/chapter_stack_and_queue/array_queue.rb index 28ec1f010..089d7a3fc 100644 --- a/codes/ruby/chapter_stack_and_queue/array_queue.rb +++ b/codes/ruby/chapter_stack_and_queue/array_queue.rb @@ -69,38 +69,39 @@ class ArrayQueue end ### Driver Code ### - -# 初始化队列 -queue = ArrayQueue.new(10) - -# 元素入队 -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}" - -# 元素出队 -pop = queue.pop -puts "出队元素 pop = #{pop}" -puts "出队后 queue = #{queue.to_array}" - -# 获取队列的长度 -size = queue.size -puts "队列长度 size = #{size}" - -# 判断队列是否为空 -is_empty = queue.is_empty? -puts "队列是否为空 = #{is_empty}" - -# 测试环形数组 -for i in 0...10 - queue.push(i) - queue.pop - puts "第 #{i} 轮入队 + 出队后 queue = #{queue.to_array}" +if __FILE__ == $0 + # 初始化队列 + queue = ArrayQueue.new(10) + + # 元素入队 + 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}" + + # 元素出队 + pop = queue.pop + puts "出队元素 pop = #{pop}" + puts "出队后 queue = #{queue.to_array}" + + # 获取队列的长度 + size = queue.size + puts "队列长度 size = #{size}" + + # 判断队列是否为空 + is_empty = queue.is_empty? + puts "队列是否为空 = #{is_empty}" + + # 测试环形数组 + for i in 0...10 + queue.push(i) + queue.pop + puts "第 #{i} 轮入队 + 出队后 queue = #{queue.to_array}" + end end diff --git a/codes/ruby/chapter_stack_and_queue/array_stack.rb b/codes/ruby/chapter_stack_and_queue/array_stack.rb index b580626ac..93a7f87da 100644 --- a/codes/ruby/chapter_stack_and_queue/array_stack.rb +++ b/codes/ruby/chapter_stack_and_queue/array_stack.rb @@ -47,31 +47,32 @@ class ArrayStack end ### Driver Code ### - -# 初始化栈 -stack = ArrayStack.new - -# 元素入栈 -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}" - -# 元素出栈 -pop = stack.pop -puts "出栈元素 pop = #{pop}" -puts "出栈后 stack = #{stack.to_array}" - -# 获取栈的长度 -size = stack.size -puts "栈的长度 size = #{size}" - -# 判断是否为空 -is_empty = stack.is_empty? -puts "栈是否为空 = #{is_empty}" +if __FILE__ == $0 + # 初始化栈 + stack = ArrayStack.new + + # 元素入栈 + 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}" + + # 元素出栈 + pop = stack.pop + puts "出栈元素 pop = #{pop}" + puts "出栈后 stack = #{stack.to_array}" + + # 获取栈的长度 + size = stack.size + puts "栈的长度 size = #{size}" + + # 判断是否为空 + is_empty = stack.is_empty? + puts "栈是否为空 = #{is_empty}" +end diff --git a/codes/ruby/chapter_stack_and_queue/deque.rb b/codes/ruby/chapter_stack_and_queue/deque.rb index cef705e03..21e63d294 100644 --- a/codes/ruby/chapter_stack_and_queue/deque.rb +++ b/codes/ruby/chapter_stack_and_queue/deque.rb @@ -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 diff --git a/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb b/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb index 0a9ae2e59..4b901a853 100644 --- a/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb +++ b/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb @@ -132,36 +132,37 @@ class LinkedListDeque end ### Driver Code ### - -# 初始化双向队列 -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}" - -# 元素入队 -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}" - -# 获取双向队列的长度 -size = deque.size -puts "双向队列长度 size = #{size}" - -# 判断双向队列是否为空 -is_empty = deque.is_empty? -puts "双向队列是否为空 = #{is_empty}" +if __FILE__ == $0 + # 初始化双向队列 + 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}" + + # 元素入队 + 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}" + + # 获取双向队列的长度 + size = deque.size + puts "双向队列长度 size = #{size}" + + # 判断双向队列是否为空 + is_empty = deque.is_empty? + puts "双向队列是否为空 = #{is_empty}" +end diff --git a/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb b/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb index a21e94ab2..fea3fc9cc 100644 --- a/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb +++ b/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb @@ -70,31 +70,32 @@ class LinkedListQueue end ### Driver Code ### - -# 初始化队列 -queue = LinkedListQueue.new - -# 元素如队 -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}" - -# 元素出队 -pop_front = queue.pop -puts "出队元素 pop = #{pop_front}" -puts "出队后 queue = #{queue.to_array}" - -# 获取队列的长度 -size = queue.size -puts "队列长度 size = #{size}" - -# 判断队列是否为空 -is_empty = queue.is_empty? -puts "队列是否为空 = #{is_empty}" +if __FILE__ == $0 + # 初始化队列 + queue = LinkedListQueue.new + + # 元素如队 + 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}" + + # 元素出队 + pop_front = queue.pop + puts "出队元素 pop = #{pop_front}" + puts "出队后 queue = #{queue.to_array}" + + # 获取队列的长度 + size = queue.size + puts "队列长度 size = #{size}" + + # 判断队列是否为空 + is_empty = queue.is_empty? + puts "队列是否为空 = #{is_empty}" +end diff --git a/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb b/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb index 36a04792e..0c3551b4b 100644 --- a/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb +++ b/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb @@ -56,31 +56,32 @@ class LinkedListStack end ### Driver Code ### - -# 初始化栈 -stack = LinkedListStack.new - -# 元素入栈 -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}" - -# 元素出栈 -pop = stack.pop -puts "出栈元素 pop = #{pop}" -puts "出栈后 stack = #{stack.to_array}" - -# 获取栈的长度 -size = stack.size -puts "栈的长度 size = #{size}" - -# 判断是否为空 -is_empty = stack.is_empty? -puts "栈是否为空 = #{is_empty}" +if __FILE__ == $0 + # 初始化栈 + stack = LinkedListStack.new + + # 元素入栈 + 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}" + + # 元素出栈 + pop = stack.pop + puts "出栈元素 pop = #{pop}" + puts "出栈后 stack = #{stack.to_array}" + + # 获取栈的长度 + size = stack.size + puts "栈的长度 size = #{size}" + + # 判断是否为空 + is_empty = stack.is_empty? + puts "栈是否为空 = #{is_empty}" +end diff --git a/codes/ruby/chapter_stack_and_queue/queue.rb b/codes/ruby/chapter_stack_and_queue/queue.rb index cda1983a7..919289d37 100644 --- a/codes/ruby/chapter_stack_and_queue/queue.rb +++ b/codes/ruby/chapter_stack_and_queue/queue.rb @@ -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 diff --git a/codes/ruby/chapter_stack_and_queue/stack.rb b/codes/ruby/chapter_stack_and_queue/stack.rb index 333ecf1d1..ff9e7ef9c 100644 --- a/codes/ruby/chapter_stack_and_queue/stack.rb +++ b/codes/ruby/chapter_stack_and_queue/stack.rb @@ -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