hello-algo/codes/swift/chapter_stack_and_queue/array_deque.swift
nuomi1 7359a7cb4b
Review Swift codes (#1150)
* feat(swift): review for chapter_computational_complexity

* feat(swift): review for chapter_data_structure

* feat(swift): review for chapter_array_and_linkedlist

* feat(swift): review for chapter_stack_and_queue

* feat(swift): review for chapter_hashing

* feat(swift): review for chapter_tree

* feat(swift): add codes for heap article

* feat(swift): review for chapter_heap

* feat(swift): review for chapter_graph

* feat(swift): review for chapter_searching

* feat(swift): review for chapter_sorting

* feat(swift): review for chapter_divide_and_conquer

* feat(swift): review for chapter_backtracking

* feat(swift): review for chapter_dynamic_programming

* feat(swift): review for chapter_greedy

* feat(swift): review for utils

* feat(swift): update ci tool

* feat(swift): trailing closure

* feat(swift): array init

* feat(swift): map index
2024-03-20 21:15:39 +08:00

148 lines
4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: array_deque.swift
* Created Time: 2023-02-22
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
class ArrayDeque {
private var nums: [Int] //
private var front: Int //
private var _size: Int //
/* */
init(capacity: Int) {
nums = Array(repeating: 0, count: capacity)
front = 0
_size = 0
}
/* */
func capacity() -> Int {
nums.count
}
/* */
func size() -> Int {
_size
}
/* */
func isEmpty() -> Bool {
size() == 0
}
/* */
private func index(i: Int) -> Int {
//
// i
// i
(i + capacity()) % capacity()
}
/* */
func pushFirst(num: Int) {
if size() == capacity() {
print("双向队列已满")
return
}
//
// front
front = index(i: front - 1)
// num
nums[front] = num
_size += 1
}
/* */
func pushLast(num: Int) {
if size() == capacity() {
print("双向队列已满")
return
}
// + 1
let rear = index(i: front + size())
// num
nums[rear] = num
_size += 1
}
/* */
func popFirst() -> Int {
let num = peekFirst()
//
front = index(i: front + 1)
_size -= 1
return num
}
/* */
func popLast() -> Int {
let num = peekLast()
_size -= 1
return num
}
/* 访 */
func peekFirst() -> Int {
if isEmpty() {
fatalError("双向队列为空")
}
return nums[front]
}
/* 访 */
func peekLast() -> Int {
if isEmpty() {
fatalError("双向队列为空")
}
//
let last = index(i: front + size() - 1)
return nums[last]
}
/* */
func toArray() -> [Int] {
//
(front ..< front + size()).map { nums[index(i: $0)] }
}
}
@main
enum _ArrayDeque {
/* Driver Code */
static func main() {
/* */
let deque = ArrayDeque(capacity: 10)
deque.pushLast(num: 3)
deque.pushLast(num: 2)
deque.pushLast(num: 5)
print("双向队列 deque = \(deque.toArray())")
/* 访 */
let peekFirst = deque.peekFirst()
print("队首元素 peekFirst = \(peekFirst)")
let peekLast = deque.peekLast()
print("队尾元素 peekLast = \(peekLast)")
/* */
deque.pushLast(num: 4)
print("元素 4 队尾入队后 deque = \(deque.toArray())")
deque.pushFirst(num: 1)
print("元素 1 队首入队后 deque = \(deque.toArray())")
/* */
let popLast = deque.popLast()
print("队尾出队元素 = \(popLast),队尾出队后 deque = \(deque.toArray())")
let popFirst = deque.popFirst()
print("队首出队元素 = \(popFirst),队首出队后 deque = \(deque.toArray())")
/* */
let size = deque.size()
print("双向队列长度 size = \(size)")
/* */
let isEmpty = deque.isEmpty()
print("双向队列是否为空 = \(isEmpty)")
}
}