This commit is contained in:
krahets
2024-03-21 04:22:07 +08:00
parent 35a07170c0
commit cfdb743939
52 changed files with 292 additions and 290 deletions

View File

@@ -999,15 +999,15 @@ The implementation code is as follows:
class LinkedListDeque {
private var front: ListNode? // 头节点 front
private var rear: ListNode? // 尾节点 rear
private var queSize: Int // 双向队列的长度
private var _size: Int // 双向队列的长度
init() {
queSize = 0
_size = 0
}
/* 获取双向队列的长度 */
func size() -> Int {
queSize
_size
}
/* 判断双向队列是否为空 */
@@ -1037,7 +1037,7 @@ The implementation code is as follows:
node.prev = rear
rear = node // 更新尾节点
}
queSize += 1 // 更新队列长度
_size += 1 // 更新队列长度
}
/* 队首入队 */
@@ -1078,7 +1078,7 @@ The implementation code is as follows:
}
rear = rPrev // 更新尾节点
}
queSize -= 1 // 更新队列长度
_size -= 1 // 更新队列长度
return val
}
@@ -1093,13 +1093,19 @@ The implementation code is as follows:
}
/* 访问队首元素 */
func peekFirst() -> Int? {
isEmpty() ? nil : front?.val
func peekFirst() -> Int {
if isEmpty() {
fatalError("双向队列为空")
}
return front!.val
}
/* 访问队尾元素 */
func peekLast() -> Int? {
isEmpty() ? nil : rear?.val
func peekLast() -> Int {
if isEmpty() {
fatalError("双向队列为空")
}
return rear!.val
}
/* 返回数组用于打印 */

View File

@@ -684,9 +684,11 @@ Below is the code for implementing a queue using a linked list:
class LinkedListQueue {
private var front: ListNode? // 头节点
private var rear: ListNode? // 尾节点
private var _size = 0
private var _size: Int
init() {}
init() {
_size = 0
}
/* 获取队列的长度 */
func size() -> Int {
@@ -1605,12 +1607,14 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
/* 基于环形数组实现的队列 */
class ArrayQueue {
private var nums: [Int] // 用于存储队列元素的数组
private var front = 0 // 队首指针,指向队首元素
private var queSize = 0 // 队列长度
private var front: Int // 队首指针,指向队首元素
private var _size: Int // 队列长度
init(capacity: Int) {
// 初始化数组
nums = Array(repeating: 0, count: capacity)
front = 0
_size = 0
}
/* 获取队列的容量 */
@@ -1620,12 +1624,12 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
/* 获取队列的长度 */
func size() -> Int {
queSize
_size
}
/* 判断队列是否为空 */
func isEmpty() -> Bool {
queSize == 0
size() == 0
}
/* 入队 */
@@ -1636,10 +1640,10 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
}
// 计算队尾指针,指向队尾索引 + 1
// 通过取余操作实现 rear 越过数组尾部后回到头部
let rear = (front + queSize) % capacity()
let rear = (front + size()) % capacity()
// 将 num 添加至队尾
nums[rear] = num
queSize += 1
_size += 1
}
/* 出队 */
@@ -1648,7 +1652,7 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
let num = peek()
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
front = (front + 1) % capacity()
queSize -= 1
_size -= 1
return num
}
@@ -1663,11 +1667,7 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
/* 返回数组 */
func toArray() -> [Int] {
// 仅转换有效长度范围内的列表元素
var res = Array(repeating: 0, count: queSize)
for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {
res[i] = nums[j % capacity()]
}
return res
(front ..< front + size()).map { nums[$0 % capacity()] }
}
}
```

View File

@@ -643,9 +643,11 @@ Below is an example code for implementing a stack based on a linked list:
/* 基于链表实现的栈 */
class LinkedListStack {
private var _peek: ListNode? // 将头节点作为栈顶
private var _size = 0 // 栈的长度
private var _size: Int // 栈的长度
init() {}
init() {
_size = 0
}
/* 获取栈的长度 */
func size() -> Int {
@@ -685,8 +687,8 @@ Below is an example code for implementing a stack based on a linked list:
/* 将 List 转化为 Array 并返回 */
func toArray() -> [Int] {
var node = _peek
var res = Array(repeating: 0, count: _size)
for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {
var res = Array(repeating: 0, count: size())
for i in res.indices.reversed() {
res[i] = node!.val
node = node?.next
}