This commit is contained in:
krahets
2024-04-09 20:43:40 +08:00
parent d8caf02e9e
commit a6adc8e20a
48 changed files with 1599 additions and 571 deletions

View File

@ -1845,7 +1845,7 @@ The implementation code is as follows:
```kotlin title="linkedlist_deque.kt"
/* 双向链表节点 */
class ListNode(var value: Int) {
class ListNode(var _val: Int) {
// 节点值
var next: ListNode? = null // 后继节点引用
var prev: ListNode? = null // 前驱节点引用
@ -1853,9 +1853,9 @@ The implementation code is as follows:
/* 基于双向链表实现的双向队列 */
class LinkedListDeque {
private var front: ListNode? = null // 头节点 front ,尾节点 rear
private var rear: ListNode? = null
private var queSize = 0 // 双向队列的长度
private var front: ListNode? = null // 头节点 front
private var rear: ListNode? = null // 尾节点 rear
private var queSize: Int = 0 // 双向队列的长度
/* 获取双向队列的长度 */
fun size(): Int {
@ -1902,12 +1902,12 @@ The implementation code is as follows:
/* 出队操作 */
fun pop(isFront: Boolean): Int {
if (isEmpty()) throw IndexOutOfBoundsException()
if (isEmpty())
throw IndexOutOfBoundsException()
val value: Int
// 队首出队操作
if (isFront) {
value = front!!.value // 暂存头节点值
value = front!!._val // 暂存头节点值
// 删除头节点
val fNext = front!!.next
if (fNext != null) {
@ -1917,7 +1917,7 @@ The implementation code is as follows:
front = fNext // 更新头节点
// 队尾出队操作
} else {
value = rear!!.value // 暂存尾节点值
value = rear!!._val // 暂存尾节点值
// 删除尾节点
val rPrev = rear!!.prev
if (rPrev != null) {
@ -1942,17 +1942,14 @@ The implementation code is as follows:
/* 访问队首元素 */
fun peekFirst(): Int {
if (isEmpty()) {
throw IndexOutOfBoundsException()
}
return front!!.value
if (isEmpty()) throw IndexOutOfBoundsException()
return front!!._val
}
/* 访问队尾元素 */
fun peekLast(): Int {
if (isEmpty()) throw IndexOutOfBoundsException()
return rear!!.value
return rear!!._val
}
/* 返回数组用于打印 */
@ -1960,7 +1957,7 @@ The implementation code is as follows:
var node = front
val res = IntArray(size())
for (i in res.indices) {
res[i] = node!!.value
res[i] = node!!._val
node = node.next
}
return res
@ -1971,9 +1968,138 @@ The implementation code is as follows:
=== "Ruby"
```ruby title="linkedlist_deque.rb"
[class]{ListNode}-[func]{}
=begin
File: linkedlist_deque.rb
Created Time: 2024-04-06
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
[class]{LinkedListDeque}-[func]{}
### 双向链表节点
class ListNode
attr_accessor :val
attr_accessor :next # 后继节点引用
attr_accessor :prev # 前躯节点引用
### 构造方法 ###
def initialize(val)
@val = val
end
end
### 基于双向链表实现的双向队列 ###
class LinkedListDeque
### 获取双向队列的长度 ###
attr_reader :size
### 构造方法 ###
def initialize
@front = nil # 头节点 front
@rear = nil # 尾节点 rear
@size = 0 # 双向队列的长度
end
### 判断双向队列是否为空 ###
def is_empty?
size.zero?
end
### 入队操作 ###
def push(num, is_front)
node = ListNode.new(num)
# 若链表为空, 则令 front 和 rear 都指向 node
if is_empty?
@front = @rear = node
# 队首入队操作
elsif is_front
# 将 node 添加至链表头部
@front.prev = node
node.next = @front
@front = node # 更新头节点
# 队尾入队操作
else
# 将 node 添加至链表尾部
@rear.next = node
node.prev = @rear
@rear = node # 更新尾节点
end
@size += 1 # 更新队列长度
end
### 队首入队 ###
def push_first(num)
push(num, true)
end
### 队尾入队 ###
def push_last(num)
push(num, false)
end
### 出队操作 ###
def pop(is_front)
raise IndexError, '双向队列为空' if is_empty?
# 队首出队操作
if is_front
val = @front.val # 暂存头节点值
# 删除头节点
fnext = @front.next
unless fnext.nil?
fnext.prev = nil
@front.next = nil
end
@front = fnext # 更新头节点
# 队尾出队操作
else
val = @rear.val # 暂存尾节点值
# 删除尾节点
rprev = @rear.prev
unless rprev.nil?
rprev.next = nil
@rear.prev = nil
end
@rear = rprev # 更新尾节点
end
@size -= 1 # 更新队列长度
val
end
### 队首出队 ###
def pop_first
pop(true)
end
### 队首出队 ###
def pop_last
pop(false)
end
### 访问队首元素 ###
def peek_first
raise IndexError, '双向队列为空' if is_empty?
@front.val
end
### 访问队尾元素 ###
def peek_last
raise IndexError, '双向队列为空' if is_empty?
@rear.val
end
### 返回数组用于打印 ###
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
```
=== "Zig"
@ -3356,11 +3482,11 @@ The implementation only needs to add methods for "front enqueue" and "rear deque
=== "Kotlin"
```kotlin title="array_deque.kt"
/* 基于环形数组实现的双向队列 */
/* 构造方法 */
class ArrayDeque(capacity: Int) {
private var nums = IntArray(capacity) // 用于存储双向队列元素的数组
private var front = 0 // 队首指针,指向队首元素
private var queSize = 0 // 双向队列长度
private var nums: IntArray = IntArray(capacity) // 用于存储双向队列元素的数组
private var front: Int = 0 // 队首指针,指向队首元素
private var queSize: Int = 0 // 双向队列长度
/* 获取双向队列的容量 */
fun capacity(): Int {
@ -3421,7 +3547,7 @@ The implementation only needs to add methods for "front enqueue" and "rear deque
return num
}
/* 访问队尾元素 */
/* 队尾出队 */
fun popLast(): Int {
val num = peekLast()
queSize--
@ -3461,7 +3587,109 @@ The implementation only needs to add methods for "front enqueue" and "rear deque
=== "Ruby"
```ruby title="array_deque.rb"
[class]{ArrayDeque}-[func]{}
### 基于环形数组实现的双向队列 ###
class ArrayDeque
### 获取双向队列的长度 ###
attr_reader :size
### 构造方法 ###
def initialize(capacity)
@nums = Array.new(capacity, 0)
@front = 0
@size = 0
end
### 获取双向队列的容量 ###
def capacity
@nums.length
end
### 判断双向队列是否为空 ###
def is_empty?
size.zero?
end
### 队首入队 ###
def push_first(num)
if size == capacity
puts '双向队列已满'
return
end
# 队首指针向左移动一位
# 通过取余操作实现 front 越过数组头部后回到尾部
@front = index(@front - 1)
# 将 num 添加至队首
@nums[@front] = num
@size += 1
end
### 队尾入队 ###
def push_last(num)
if size == capacity
puts '双向队列已满'
return
end
# 计算队尾指针,指向队尾索引 + 1
rear = index(@front + size)
# 将 num 添加至队尾
@nums[rear] = num
@size += 1
end
### 队首出队 ###
def pop_first
num = peek_first
# 队首指针向后移动一位
@front = index(@front + 1)
@size -= 1
num
end
### 队尾出队 ###
def pop_last
num = peek_last
@size -= 1
num
end
### 访问队首元素 ###
def peek_first
raise IndexError, '双向队列为空' if is_empty?
@nums[@front]
end
### 访问队尾元素 ###
def peek_last
raise IndexError, '双向队列为空' if is_empty?
# 计算尾元素索引
last = index(@front + size - 1)
@nums[last]
end
### 返回数组用于打印 ###
def to_array
# 仅转换有效长度范围内的列表元素
res = []
for i in 0...size
res << @nums[index(@front + i)]
end
res
end
private
### 计算环形数组索引 ###
def index(i)
# 通过取余操作实现数组首尾相连
# 当 i 越过数组尾部后,回到头部
# 当 i 越过数组头部后,回到尾部
(i + capacity) % capacity
end
end
```
=== "Zig"

View File

@ -1180,7 +1180,7 @@ Below is the code for implementing a queue using a linked list:
/* 访问队首元素 */
fun peek(): Int {
if (isEmpty()) throw IndexOutOfBoundsException()
return front!!.value
return front!!._val
}
/* 将链表转化为 Array 并返回 */
@ -1188,7 +1188,7 @@ Below is the code for implementing a queue using a linked list:
var node = front
val res = IntArray(size())
for (i in res.indices) {
res[i] = node!!.value
res[i] = node!!._val
node = node.next
}
return res
@ -1199,7 +1199,68 @@ Below is the code for implementing a queue using a linked list:
=== "Ruby"
```ruby title="linkedlist_queue.rb"
[class]{LinkedListQueue}-[func]{}
### 基于链表头现的队列 ###
class LinkedListQueue
### 获取队列的长度 ###
attr_reader :size
### 构造方法 ###
def initialize
@front = nil # 头节点 front
@rear = nil # 尾节点 rear
@size = 0
end
### 判断队列是否为空 ###
def is_empty?
@front.nil?
end
### 入队 ###
def push(num)
# 在尾节点后添加 num
node = ListNode.new(num)
# 如果队列为空,则令头,尾节点都指向该节点
if @front.nil?
@front = node
@rear = node
# 如果队列不为空,则令该节点添加到尾节点后
else
@rear.next = node
@rear = node
end
@size += 1
end
### 出队 ###
def pop
num = peek
# 删除头节点
@front = @front.next
@size -= 1
num
end
### 访问队首元素 ###
def peek
raise IndexError, '队列为空' if is_empty?
@front.val
end
### 将链表为 Array 并返回 ###
def to_array
queue = []
temp = @front
while temp
queue << temp.val
temp = temp.next
end
queue
end
end
```
=== "Zig"
@ -2118,9 +2179,9 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
```kotlin title="array_queue.kt"
/* 基于环形数组实现的队列 */
class ArrayQueue(capacity: Int) {
private val nums = IntArray(capacity) // 用于存储队列元素的数组
private var front = 0 // 队首指针,指向队首元素
private var queSize = 0 // 队列长度
private val nums: IntArray = IntArray(capacity) // 用于存储队列元素的数组
private var front: Int = 0 // 队首指针,指向队首元素
private var queSize: Int = 0 // 队列长度
/* 获取队列的容量 */
fun capacity(): Int {
@ -2185,7 +2246,69 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
=== "Ruby"
```ruby title="array_queue.rb"
[class]{ArrayQueue}-[func]{}
### 基于环形数组实现的队列 ###
class ArrayQueue
### 获取队列的长度 ###
attr_reader :size
### 构造方法 ###
def initialize(size)
@nums = Array.new(size, 0) # 用于存储队列元素的数组
@front = 0 # 队首指针,指向队首元素
@size = 0 # 队列长度
end
### 获取队列的容量 ###
def capacity
@nums.length
end
### 判断队列是否为空 ###
def is_empty?
size.zero?
end
### 入队 ###
def push(num)
raise IndexError, '队列已满' if size == capacity
# 计算队尾指针,指向队尾索引 + 1
# 通过取余操作实现 rear 越过数组尾部后回到头部
rear = (@front + size) % capacity
# 将 num 添加至队尾
@nums[rear] = num
@size += 1
end
### 出队 ###
def pop
num = peek
# 队首指针向后移动一位,若越过尾部,则返回到数组头部
@front = (@front + 1) % capacity
@size -= 1
num
end
### 访问队首元素 ###
def peek
raise IndexError, '队列为空' if is_empty?
@nums[@front]
end
### 返回列表用于打印 ###
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
```
=== "Zig"

View File

@ -1052,7 +1052,7 @@ Below is an example code for implementing a stack based on a linked list:
/* 访问栈顶元素 */
fun peek(): Int? {
if (isEmpty()) throw IndexOutOfBoundsException()
return stackPeek?.value
return stackPeek?._val
}
/* 将 List 转化为 Array 并返回 */
@ -1060,7 +1060,7 @@ Below is an example code for implementing a stack based on a linked list:
var node = stackPeek
val res = IntArray(size())
for (i in res.size - 1 downTo 0) {
res[i] = node?.value!!
res[i] = node?._val!!
node = node.next
}
return res
@ -1071,7 +1071,54 @@ Below is an example code for implementing a stack based on a linked list:
=== "Ruby"
```ruby title="linkedlist_stack.rb"
[class]{LinkedListStack}-[func]{}
### 基于链表实现的栈 ###
class LinkedListStack
attr_reader :size
### 构造方法 ###
def initialize
@size = 0
end
### 判断栈是否为空 ###
def is_empty?
@peek.nil?
end
### 入栈 ###
def push(val)
node = ListNode.new(val)
node.next = @peek
@peek = node
@size += 1
end
### 出栈 ###
def pop
num = peek
@peek = @peek.next
@size -= 1
num
end
### 访问栈顶元素 ###
def peek
raise IndexError, '栈为空' if is_empty?
@peek.val
end
### 将链表转化为 Array 并反回 ###
def to_array
arr = []
node = @peek
while node
arr << node.val
node = node.next
end
arr.reverse
end
end
```
=== "Zig"
@ -1712,7 +1759,7 @@ Since the elements to be pushed onto the stack may continuously increase, we can
/* 基于数组实现的栈 */
class ArrayStack {
// 初始化列表(动态数组)
private val stack = ArrayList<Int>()
private val stack = mutableListOf<Int>()
/* 获取栈的长度 */
fun size(): Int {
@ -1743,7 +1790,7 @@ Since the elements to be pushed onto the stack may continuously increase, we can
/* 将 List 转化为 Array 并返回 */
fun toArray(): Array<Any> {
return stack.toArray()
return stack.toTypedArray()
}
}
```
@ -1751,7 +1798,47 @@ Since the elements to be pushed onto the stack may continuously increase, we can
=== "Ruby"
```ruby title="array_stack.rb"
[class]{ArrayStack}-[func]{}
### 基于数组实现的栈 ###
class ArrayStack
### 构造方法 ###
def initialize
@stack = []
end
### 获取栈的长度 ###
def size
@stack.length
end
### 判断栈是否为空 ###
def is_empty?
@stack.empty?
end
### 入栈 ###
def push(item)
@stack << item
end
### 出栈 ###
def pop
raise IndexError, '栈为空' if is_empty?
@stack.pop
end
### 访问栈顶元素 ###
def peek
raise IndexError, '栈为空' if is_empty?
@stack.last
end
### 返回列表用于打印 ###
def to_array
@stack
end
end
```
=== "Zig"