feat: add Swift codes for deque article (#380)

pull/383/head
nuomi1 2 years ago committed by GitHub
parent 533cb7bbbe
commit 037769d94d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,8 @@ let package = Package(
.executable(name: "linkedlist_queue", targets: ["linkedlist_queue"]),
.executable(name: "array_queue", targets: ["array_queue"]),
.executable(name: "deque", targets: ["deque"]),
.executable(name: "linkedlist_deque", targets: ["linkedlist_deque"]),
.executable(name: "array_deque", targets: ["array_deque"]),
// chapter_hashing
.executable(name: "hash_map", targets: ["hash_map"]),
.executable(name: "array_hash_map", targets: ["array_hash_map"]),
@ -72,6 +74,8 @@ let package = Package(
.executableTarget(name: "linkedlist_queue", dependencies: ["utils"], path: "chapter_stack_and_queue", sources: ["linkedlist_queue.swift"]),
.executableTarget(name: "array_queue", path: "chapter_stack_and_queue", sources: ["array_queue.swift"]),
.executableTarget(name: "deque", path: "chapter_stack_and_queue", sources: ["deque.swift"]),
.executableTarget(name: "linkedlist_deque", path: "chapter_stack_and_queue", sources: ["linkedlist_deque.swift"]),
.executableTarget(name: "array_deque", path: "chapter_stack_and_queue", sources: ["array_deque.swift"]),
// chapter_hashing
.executableTarget(name: "hash_map", dependencies: ["utils"], path: "chapter_hashing", sources: ["hash_map.swift"]),
.executableTarget(name: "array_hash_map", path: "chapter_hashing", sources: ["array_hash_map.swift"]),

@ -0,0 +1,152 @@
/**
* 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 queSize: Int //
/* */
init(capacity: Int) {
nums = Array(repeating: 0, count: capacity)
front = 0
queSize = 0
}
/* */
func capacity() -> Int {
nums.count
}
/* */
func size() -> Int {
queSize
}
/* */
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
queSize += 1
}
/* */
func pushLast(num: Int) {
if size() == capacity() {
print("双向队列已满")
return
}
// + 1
let rear = index(i: front + size())
// num
nums[rear] = num
queSize += 1
}
/* */
func pollFirst() -> Int {
let num = peekFirst()
//
front = index(i: front + 1)
queSize -= 1
return num
}
/* */
func pollLast() -> Int {
let num = peekLast()
queSize -= 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] {
//
var res = Array(repeating: 0, count: size())
for (i, j) in sequence(first: (0, front), next: { $0 < self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {
res[i] = nums[index(i: j)]
}
return res
}
}
@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 pollLast = deque.pollLast()
print("队尾出队元素 = \(pollLast),队尾出队后 deque = \(deque.toArray())")
let pollFirst = deque.pollFirst()
print("队首出队元素 = \(pollFirst),队首出队后 deque = \(deque.toArray())")
/* */
let size = deque.size()
print("双向队列长度 size = \(size)")
/* */
let isEmpty = deque.isEmpty()
print("双向队列是否为空 = \(isEmpty)")
}
}

@ -0,0 +1,174 @@
/**
* File: linkedlist_deque.swift
* Created Time: 2023-02-22
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
class ListNode {
var val: Int //
var next: ListNode? //
var prev: ListNode? //
init(val: Int) {
self.val = val
}
}
/* */
class LinkedListDeque {
private var front: ListNode? // front
private var rear: ListNode? // rear
private var queSize: Int //
init() {
queSize = 0
}
/* */
func size() -> Int {
queSize
}
/* */
func isEmpty() -> Bool {
size() == 0
}
/* */
private func push(num: Int, isFront: Bool) {
let node = ListNode(val: num)
// front, rear node
if isEmpty() {
front = node
rear = node
}
//
else if isFront {
// node
front?.prev = node
node.next = front
front = node //
}
//
else {
// node
rear?.next = node
node.prev = rear
rear = node //
}
queSize += 1 //
}
/* */
func pushFirst(num: Int) {
push(num: num, isFront: true)
}
/* */
func pushLast(num: Int) {
push(num: num, isFront: false)
}
/* */
private func poll(isFront: Bool) -> Int {
if isEmpty() {
fatalError("双向队列为空")
}
let val: Int
//
if isFront {
val = front!.val //
//
let fNext = front?.next
if fNext != nil {
fNext?.prev = nil
front?.next = nil
}
front = fNext //
}
//
else {
val = rear!.val //
//
let rPrev = rear?.prev
if rPrev != nil {
rPrev?.next = nil
rear?.prev = nil
}
rear = rPrev //
}
queSize -= 1 //
return val
}
/* */
func pollFirst() -> Int {
poll(isFront: true)
}
/* */
func pollLast() -> Int {
poll(isFront: false)
}
/* 访 */
func peekFirst() -> Int? {
isEmpty() ? nil : front?.val
}
/* 访 */
func peekLast() -> Int? {
isEmpty() ? nil : rear?.val
}
/* */
func toArray() -> [Int] {
var node = front
var res = Array(repeating: 0, count: size())
for i in res.indices {
res[i] = node!.val
node = node?.next
}
return res
}
}
@main
enum _LinkedListDeque {
/* Driver Code */
static func main() {
/* */
let deque = LinkedListDeque()
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 pollLast = deque.pollLast()
print("队尾出队元素 = \(pollLast),队尾出队后 deque = \(deque.toArray())")
let pollFirst = deque.pollFirst()
print("队首出队元素 = \(pollFirst),队首出队后 deque = \(deque.toArray())")
/* */
let size = deque.size()
print("双向队列长度 size = \(size)")
/* */
let isEmpty = deque.isEmpty()
print("双向队列是否为空 = \(isEmpty)")
}
}

@ -381,7 +381,9 @@ comments: true
=== "Swift"
```swift title="linkedlist_deque.swift"
[class]{ListNode}-[func]{}
[class]{LinkedListDeque}-[func]{}
```
=== "Zig"
@ -462,7 +464,7 @@ comments: true
=== "Swift"
```swift title="array_deque.swift"
[class]{ArrayDeque}-[func]{}
```
=== "Zig"

Loading…
Cancel
Save