Add Ruby code blocks to the documents (#1200)

* Add Ruby code blocks to documents

* Remove Ruby code from en/docs
This commit is contained in:
Yudong Jin 2024-03-31 03:57:11 +08:00 committed by GitHub
parent 034ee65e9a
commit 57bdfd6284
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 259 additions and 255 deletions

View file

@ -113,14 +113,6 @@
var nums = intArrayOf(1, 3, 2, 5, 4)
```
=== "Zig"
```zig title="array.zig"
// 初始化数组
var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }
var nums = [_]i32{ 1, 3, 2, 5, 4 };
```
=== "Ruby"
```ruby title="array.rb"
@ -129,6 +121,14 @@
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
```zig title="array.zig"
// 初始化数组
var arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }
var nums = [_]i32{ 1, 3, 2, 5, 4 };
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0Aarr%20%3D%20%5B0%5D%20*%205%20%20%23%20%5B%200,%200,%200,%200,%200%20%5D%0Anums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View file

@ -170,6 +170,21 @@
}
```
=== "Ruby"
```ruby title=""
# 链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向下一节点的引用
def initialize(val=nil, next_node=nil)
@val = val || 0
@next = next_node
end
end
```
=== "Zig"
```zig title=""
@ -190,21 +205,6 @@
}
```
=== "Ruby"
```ruby title=""
# 链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向下一节点的引用
def initialize(val=nil, next_node=nil)
@val = val || 0
@next = next_node
end
end
```
## 链表常用操作
### 初始化链表
@ -416,6 +416,23 @@
n3.next = n4;
```
=== "Ruby"
```ruby title="linked_list.rb"
# 初始化链表 1 -> 3 -> 2 -> 5 -> 4
# 初始化各个节点
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
```
=== "Zig"
```zig title="linked_list.zig"
@ -433,23 +450,6 @@
n3.next = &n4;
```
=== "Ruby"
```ruby title=linked_list.rb
# 初始化链表 1 -> 3 -> 2 -> 5 -> 4
# 初始化各个节点
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
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%201%20-%3E%203%20-%3E%202%20-%3E%205%20-%3E%204%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@ -700,6 +700,23 @@
}
```
=== "Ruby"
```ruby title=""
# 双向链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向后继节点的引用
attr_accessor :prev # 指向前驱节点的引用
def initialize(val=nil, next_node=nil, prev_node=nil)
@val = val || 0
@next = next_node
@prev = prev_node
end
end
```
=== "Zig"
```zig title=""
@ -722,23 +739,6 @@
}
```
=== "Ruby"
```ruby title=""
# 双向链表节点类
class ListNode
attr_accessor :val # 节点值
attr_accessor :next # 指向后继节点的引用
attr_accessor :prev # 指向前驱节点的引用
def initialize(val=nil, next_node=nil, prev_node=nil)
@val = val || 0
@next = next_node
@prev = prev_node
end
end
```
![常见链表种类](linked_list.assets/linkedlist_common_types.png)
## 链表典型应用

View file

@ -137,15 +137,6 @@
var nums = numbers.toMutableList()
```
=== "Zig"
```zig title="list.zig"
// 初始化列表
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
```
=== "Ruby"
```ruby title="list.rb"
@ -156,6 +147,15 @@
nums = [1, 3, 2, 5, 4]
```
=== "Zig"
```zig title="list.zig"
// 初始化列表
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20%23%20%E6%97%A0%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D&cumulative=false&curInstr=4&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@ -278,6 +278,15 @@
nums[1] = 0 // 将索引 1 处的元素更新为 0
```
=== "Ruby"
```ruby title="list.rb"
# 访问元素
num = nums[1]
# 更新元素
nums[1] = 0
```
=== "Zig"
```zig title="list.zig"
@ -288,15 +297,6 @@
nums.items[1] = 0; // 将索引 1 处的元素更新为 0
```
=== "Ruby"
```ruby title="list.rb"
# 访问元素
num = nums[1]
# 更新元素
nums[1] = 0
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%E8%AE%BF%E9%97%AE%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%0A%0A%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%E5%B0%86%E7%B4%A2%E5%BC%95%201%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0%E6%9B%B4%E6%96%B0%E4%B8%BA%200&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@ -531,26 +531,6 @@
nums.remove(3); // 删除索引 3 处的元素
```
=== "Zig"
```zig title="list.zig"
// 清空列表
nums.clearRetainingCapacity();
// 在尾部添加元素
try nums.append(1);
try nums.append(3);
try nums.append(2);
try nums.append(5);
try nums.append(4);
// 在中间插入元素
try nums.insert(3, 6); // 在索引 3 处插入数字 6
// 删除元素
_ = nums.orderedRemove(3); // 删除索引 3 处的元素
```
=== "Ruby"
```ruby title="list.rb"
@ -571,6 +551,26 @@
nums.delete_at 3
```
=== "Zig"
```zig title="list.zig"
// 清空列表
nums.clearRetainingCapacity();
// 在尾部添加元素
try nums.append(1);
try nums.append(3);
try nums.append(2);
try nums.append(5);
try nums.append(4);
// 在中间插入元素
try nums.insert(3, 6); // 在索引 3 处插入数字 6
// 删除元素
_ = nums.orderedRemove(3); // 删除索引 3 处的元素
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E6%9C%89%E5%88%9D%E5%A7%8B%E5%80%BC%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B8%85%E7%A9%BA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E5%B0%BE%E9%83%A8%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.insert%283,%206%29%20%20%23%20%E5%9C%A8%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E6%8F%92%E5%85%A5%E6%95%B0%E5%AD%97%206%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E5%85%83%E7%B4%A0&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@ -756,6 +756,22 @@
}
```
=== "Ruby"
```ruby title="list.rb"
# 通过索引遍历列表
count = 0
for i in 0...nums.length
count += nums[i]
end
# 直接遍历列表元素
count = 0
for num in nums
count += num
end
```
=== "Zig"
```zig title="list.zig"
@ -773,22 +789,6 @@
}
```
=== "Ruby"
```ruby title="list.rb"
# 通过索引遍历列表
count = 0
for i in 0...nums.length
count += nums[i]
end
# 直接遍历列表元素
count = 0
for num in nums
count += num
end
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%E7%9B%B4%E6%8E%A5%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@ -892,6 +892,14 @@
nums.addAll(nums1) // 将列表 nums1 拼接到 nums 之后
```
=== "Ruby"
```ruby title="list.rb"
# 拼接两个列表
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
=== "Zig"
```zig title="list.zig"
@ -902,14 +910,6 @@
try nums.insertSlice(nums.items.len, nums1.items); // 将列表 nums1 拼接到 nums 之后
```
=== "Ruby"
```ruby title="list.rb"
# 拼接两个列表
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8B%BC%E6%8E%A5%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%0A%20%20%20%20nums1%20%3D%20%5B6,%208,%207,%2010,%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%E5%B0%86%E5%88%97%E8%A1%A8%20nums1%20%E6%8B%BC%E6%8E%A5%E5%88%B0%20nums%20%E4%B9%8B%E5%90%8E&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
@ -1001,13 +1001,6 @@
nums.sort() // 排序后,列表元素从小到大排列
```
=== "Zig"
```zig title="list.zig"
// 排序列表
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
```
=== "Ruby"
```ruby title="list.rb"
@ -1015,6 +1008,13 @@
nums = nums.sort { |a, b| a <=> b }
```
=== "Zig"
```zig title="list.zig"
// 排序列表
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%0A%20%20%20%20nums%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%8E%92%E5%BA%8F%E5%88%97%E8%A1%A8%0A%20%20%20%20nums.sort%28%29%20%20%23%20%E6%8E%92%E5%BA%8F%E5%90%8E%EF%BC%8C%E5%88%97%E8%A1%A8%E5%85%83%E7%B4%A0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%88%97&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

View file

@ -403,6 +403,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -332,6 +332,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@ -490,6 +496,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@ -748,6 +760,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -186,6 +186,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@ -465,6 +471,12 @@ $$
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@ -673,6 +685,12 @@ $$
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@ -949,6 +967,12 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -163,6 +163,12 @@
val bools = BooleanArray(5)
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -371,6 +371,12 @@ $$
// 节点对象 utils.ListNode@1d81eb93 的哈希值为 495053715
```
=== "Ruby"
```ruby title="built_in_hash.rb"
```
=== "Zig"
```zig title="built_in_hash.zig"

View file

@ -290,6 +290,12 @@
map.remove(10583)
```
=== "Ruby"
```ruby title="hash_map.rb"
```
=== "Zig"
```zig title="hash_map.zig"
@ -503,6 +509,12 @@
}
```
=== "Ruby"
```ruby title="hash_map.rb"
```
=== "Zig"
```zig title="hash_map.zig"

View file

@ -402,6 +402,12 @@
minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))
```
=== "Ruby"
```ruby title="heap.rb"
```
=== "Zig"
```zig title="heap.zig"

View file

@ -169,6 +169,12 @@
*/
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -352,6 +352,12 @@
val isEmpty = deque.isEmpty()
```
=== "Ruby"
```ruby title="deque.rb"
```
=== "Zig"
```zig title="deque.zig"

View file

@ -328,6 +328,12 @@
val isEmpty = queue.isEmpty()
```
=== "Ruby"
```ruby title="queue.rb"
```
=== "Zig"
```zig title="queue.zig"

View file

@ -322,6 +322,12 @@
val isEmpty = stack.isEmpty()
```
=== "Ruby"
```ruby title="stack.rb"
```
=== "Zig"
```zig title="stack.zig"

View file

@ -120,6 +120,12 @@
val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -211,6 +211,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""

View file

@ -186,6 +186,12 @@
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
@ -423,6 +429,12 @@
n2.right = n5
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
=== "Zig"
```zig title="binary_tree.zig"
@ -579,6 +591,12 @@
n1.left = n2
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
=== "Zig"
```zig title="binary_tree.zig"

View file

@ -119,14 +119,6 @@ Arrays can be initialized in two ways depending on the needs: either without ini
var nums = [_]i32{ 1, 3, 2, 5, 4 };
```
=== "Ruby"
```ruby title="array.rb"
# Initialize array
arr = Array.new(5, 0) # [ 0, 0, 0, 0, 0 ]
nums = [1, 3, 2, 5, 4]
```
### Accessing Elements
Elements in an array are stored in contiguous memory spaces, making it simpler to compute each element's memory address. The formula shown in the Figure below aids in determining an element's memory address, utilizing the array's memory address (specifically, the first element's address) and the element's index. This computation streamlines direct access to the desired element.

View file

@ -185,22 +185,6 @@ As the code below illustrates, a `ListNode` in a linked list, besides holding a
}
```
=== "Ruby"
```ruby title=""
# Linked List Node Class
class ListNode
attr_accessor :val # Node value
attr_accessor :next # Reference to the next node
def initialize(val=nil, next_node=nil)
@val = val || 0
@next = next_node
end
end
```
## Common Operations on Linked Lists
### Initializing a Linked List
@ -418,23 +402,6 @@ Constructing a linked list is a two-step process: first, initializing each node
n3.next = &n4;
```
=== "Ruby"
```ruby title=linked_list.rb
# Initialize linked list 1 -> 3 -> 2 -> 5 -> 4
# Initialize each node
n0 = ListNode.new 1
n1 = ListNode.new 3
n2 = ListNode.new 2
n3 = ListNode.new 5
n4 = ListNode.new 4
# Build references between nodes
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
```
The array as a whole is a variable, for instance, the array `nums` includes elements like `nums[0]`, `nums[1]`, and so on, whereas a linked list is made up of several distinct node objects. **We typically refer to a linked list by its head node**, for example, the linked list in the previous code snippet is referred to as `n0`.
### Inserting a Node
@ -697,23 +664,6 @@ As shown in the figure, there are three common types of linked lists.
}
```
=== "Ruby"
```ruby title=""
# Bidirectional linked list node class
class ListNode
attr_accessor :val # Node value
attr_accessor :next # Reference to the successor node
attr_accessor :prev # Reference to the predecessor node
def initialize(val=nil, next_node=nil, prev_node=nil)
@val = val || 0
@next = next_node
@prev = prev_node
end
end
```
![Common Types of Linked Lists](linked_list.assets/linkedlist_common_types.png)
## Typical Applications of Linked Lists

View file

@ -141,16 +141,6 @@ We typically use two initialization methods: "without initial values" and "with
try nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
```
=== "Ruby"
```ruby title="list.rb"
# Initialize list
# Without initial values
nums1 = []
# With initial values
nums = [1, 3, 2, 5, 4]
```
### Accessing Elements
Lists are essentially arrays, thus they can access and update elements in $O(1)$ time, which is very efficient.
@ -276,15 +266,6 @@ Lists are essentially arrays, thus they can access and update elements in $O(1)$
nums.items[1] = 0; // Update the element at index 1 to 0
```
=== "Ruby"
```ruby title="list.rb"
# Access elements
num = nums[1]
# Update elements
nums[1] = 0
```
### Inserting and Removing Elements
Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an $O(1)$ operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of $O(n)$.
@ -521,26 +502,6 @@ Compared to arrays, lists offer more flexibility in adding and removing elements
_ = nums.orderedRemove(3); // Remove the element at index 3
```
=== "Ruby"
```ruby title="list.rb"
# Clear list
nums.clear
# Append elements at the end
nums << 1
nums << 3
nums << 2
nums << 5
nums << 4
# Insert element in the middle
nums.insert 3, 6 # Insert number 6 at index 3
# Remove elements
nums.delete_at 3 # Remove the element at index 3
```
### Iterating the List
Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element.
@ -730,22 +691,6 @@ Similar to arrays, lists can be iterated either by using indices or by directly
}
```
=== "Ruby"
```ruby title="list.rb"
# Iterate through the list by index
count = 0
for i in 0...nums.length
count += nums[i]
end
# Iterate directly though list elements
count = 0
for num in nums
count += num
end
```
### Concatenating Lists
Given a new list `nums1`, we can append it to the end of the original list.
@ -853,14 +798,6 @@ Given a new list `nums1`, we can append it to the end of the original list.
try nums.insertSlice(nums.items.len, nums1.items); // Concatenate nums1 to the end of nums
```
=== "Ruby"
```ruby title="list.rb"
# Concatenate two lists
nums1 = [6, 8, 7, 10, 9]
nums += nums1
```
### Sorting the List
Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as "binary search" and "two-pointer" algorithms.
@ -954,13 +891,6 @@ Once the list is sorted, we can employ algorithms commonly used in array-related
std.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));
```
=== "Ruby"
```ruby title="list.rb"
# Sort the list
nums = nums.sort { |a, b| a <=> b }
```
## List Implementation
Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning.

View file

@ -209,7 +209,7 @@ body {
/* code block tabs */
.md-typeset .tabbed-labels>label {
font-size: 0.59rem;
font-size: 0.545rem;
}
/* header banner */