Merge pull request #240 from nuomi1/feature/queue-Swift

feat: add Swift codes for queue article
This commit is contained in:
Yudong Jin 2023-01-13 00:53:08 +08:00 committed by GitHub
commit 3e150ad9b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 410 additions and 27 deletions

View file

@ -16,6 +16,9 @@ let package = Package(
.executable(name: "stack", targets: ["stack"]),
.executable(name: "linkedlist_stack", targets: ["linkedlist_stack"]),
.executable(name: "array_stack", targets: ["array_stack"]),
.executable(name: "queue", targets: ["queue"]),
.executable(name: "linkedlist_queue", targets: ["linkedlist_queue"]),
.executable(name: "array_queue", targets: ["array_queue"]),
],
targets: [
.target(name: "utils", path: "utils"),
@ -30,5 +33,8 @@ let package = Package(
.executableTarget(name: "stack", path: "chapter_stack_and_queue", sources: ["stack.swift"]),
.executableTarget(name: "linkedlist_stack", dependencies: ["utils"], path: "chapter_stack_and_queue", sources: ["linkedlist_stack.swift"]),
.executableTarget(name: "array_stack", path: "chapter_stack_and_queue", sources: ["array_stack.swift"]),
.executableTarget(name: "queue", path: "chapter_stack_and_queue", sources: ["queue.swift"]),
.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"]),
]
)

View file

@ -0,0 +1,116 @@
/**
* File: array_queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
class ArrayQueue {
private var nums: [Int] //
private var front = 0 //
private var rear = 0 // + 1
init(capacity: Int) {
//
nums = Array(repeating: 0, count: capacity)
}
/* */
func capacity() -> Int {
nums.count
}
/* */
func size() -> Int {
let capacity = capacity()
// rear < front
return (capacity + rear - front) % capacity
}
/* */
func isEmpty() -> Bool {
rear - front == 0
}
/* */
func offer(num: Int) {
if size() == capacity() {
print("队列已满")
return
}
// num
nums[rear] = num
//
rear = (rear + 1) % capacity()
}
/* */
@discardableResult
func poll() -> Int {
let num = peek()
//
front = (front + 1) % capacity()
return num
}
/* 访 */
func peek() -> Int {
if isEmpty() {
fatalError("队列为空")
}
return nums[front]
}
/* */
func toArray() -> [Int] {
let size = size()
let capacity = capacity()
//
var res = Array(repeating: 0, count: size)
for (i, j) in sequence(first: (0, front), next: { $0 < size - 1 ? ($0 + 1, $1 + 1) : nil }) {
res[i] = nums[j % capacity]
}
return res
}
}
@main
enum _ArrayQueue {
/* Driver Code */
static func main() {
/* */
let capacity = 10
let queue = ArrayQueue(capacity: capacity)
/* */
queue.offer(num: 1)
queue.offer(num: 3)
queue.offer(num: 2)
queue.offer(num: 5)
queue.offer(num: 4)
print("队列 queue = \(queue.toArray())")
/* 访 */
let peek = queue.peek()
print("队首元素 peek = \(peek)")
/* */
let poll = queue.poll()
print("出队元素 poll = \(poll),出队后 queue = \(queue.toArray())")
/* */
let size = queue.size()
print("队列长度 size = \(size)")
/* */
let isEmpty = queue.isEmpty()
print("队列是否为空 = \(isEmpty)")
/* */
for i in 0 ..< 10 {
queue.offer(num: i)
queue.poll()
print("\(i) 轮入队 + 出队后 queue = \(queue.toArray())")
}
}
}

View file

@ -29,8 +29,9 @@ class ArrayStack {
}
/* */
@discardableResult
func pop() -> Int {
if stack.isEmpty {
if isEmpty() {
fatalError("栈为空")
}
return stack.removeLast()
@ -38,7 +39,7 @@ class ArrayStack {
/* 访 */
func peek() -> Int {
if stack.isEmpty {
if isEmpty() {
fatalError("栈为空")
}
return stack.last!

View file

@ -0,0 +1,105 @@
/**
* File: linkedlist_queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
class LinkedListQueue {
private var front: ListNode? //
private var rear: ListNode? //
private var _size = 0
init() {}
/* */
func size() -> Int {
_size
}
/* */
func isEmpty() -> Bool {
size() == 0
}
/* */
func offer(num: Int) {
// num
let node = ListNode(x: num)
//
if front == nil {
front = node
rear = node
}
//
else {
rear?.next = node
rear = node
}
_size += 1
}
/* */
@discardableResult
func poll() -> Int {
let num = peek()
//
front = front?.next
_size -= 1
return num
}
/* 访 */
func peek() -> Int {
if isEmpty() {
fatalError("队列为空")
}
return front!.val
}
/* Array */
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 _LinkedListQueue {
/* Driver Code */
static func main() {
/* */
let queue = LinkedListQueue()
/* */
queue.offer(num: 1)
queue.offer(num: 3)
queue.offer(num: 2)
queue.offer(num: 5)
queue.offer(num: 4)
print("队列 queue = \(queue.toArray())")
/* 访 */
let peek = queue.peek()
print("队首元素 peek = \(peek)")
/* */
let poll = queue.poll()
print("出队元素 poll = \(poll),出队后 queue = \(queue.toArray())")
/* */
let size = queue.size()
print("队列长度 size = \(size)")
/* */
let isEmpty = queue.isEmpty()
print("队列是否为空 = \(isEmpty)")
}
}

View file

@ -20,7 +20,7 @@ class LinkedListStack {
/* */
func isEmpty() -> Bool {
_size == 0
size() == 0
}
/* */
@ -32,6 +32,7 @@ class LinkedListStack {
}
/* */
@discardableResult
func pop() -> Int {
let num = peek()
_peek = _peek?.next
@ -41,7 +42,7 @@ class LinkedListStack {
/* 访 */
func peek() -> Int {
if _size == 0 {
if isEmpty() {
fatalError("栈为空")
}
return _peek!.val

View file

@ -0,0 +1,39 @@
/**
* File: queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Queue {
/* Driver Code */
static func main() {
/* */
// Swift Array 使
var queue: [Int] = []
/* */
queue.append(1)
queue.append(3)
queue.append(2)
queue.append(5)
queue.append(4)
print("队列 queue = \(queue)")
/* 访 */
let peek = queue.first!
print("队首元素 peek = \(peek)")
/* */
let pool = queue.removeFirst()
print("出队元素 poll = \(pool),出队后 queue = \(queue)")
/* */
let size = queue.count
print("队列长度 size = \(size)")
/* */
let isEmpty = queue.isEmpty
print("队列是否为空 = \(isEmpty)")
}
}

View file

@ -231,7 +231,28 @@ comments: true
=== "Swift"
```swift title="queue.swift"
/* 初始化队列 */
// Swift 没有内置的队列类,可以把 Array 当作队列来使用
var queue: [Int] = []
/* 元素入队 */
queue.append(1)
queue.append(3)
queue.append(2)
queue.append(5)
queue.append(4)
/* 访问队首元素 */
let peek = queue.first!
/* 元素出队 */
let pool = queue.removeFirst()
/* 获取队列的长度 */
let size = queue.count
/* 判断队列是否为空 */
let isEmpty = queue.isEmpty
```
## 队列实现
@ -627,7 +648,59 @@ comments: true
=== "Swift"
```swift title="linkedlist_queue.swift"
/* 基于链表实现的队列 */
class LinkedListQueue {
private var front: ListNode? // 头结点
private var rear: ListNode? // 尾结点
private var _size = 0
init() {}
/* 获取队列的长度 */
func size() -> Int {
_size
}
/* 判断队列是否为空 */
func isEmpty() -> Bool {
size() == 0
}
/* 入队 */
func offer(num: Int) {
// 尾结点后添加 num
let node = ListNode(x: num)
// 如果队列为空,则令头、尾结点都指向该结点
if front == nil {
front = node
rear = node
}
// 如果队列不为空,则将该结点添加到尾结点后
else {
rear?.next = node
rear = node
}
_size += 1
}
/* 出队 */
@discardableResult
func poll() -> Int {
let num = peek()
// 删除头结点
front = front?.next
_size -= 1
return num
}
/* 访问队首元素 */
func peek() -> Int {
if isEmpty() {
fatalError("队列为空")
}
return front!.val
}
}
```
### 基于数组的实现
@ -1042,7 +1115,63 @@ comments: true
=== "Swift"
```swift title="array_queue.swift"
/* 基于环形数组实现的队列 */
class ArrayQueue {
private var nums: [Int] // 用于存储队列元素的数组
private var front = 0 // 头指针,指向队首
private var rear = 0 // 尾指针,指向队尾 + 1
init(capacity: Int) {
// 初始化数组
nums = Array(repeating: 0, count: capacity)
}
/* 获取队列的容量 */
func capacity() -> Int {
nums.count
}
/* 获取队列的长度 */
func size() -> Int {
let capacity = capacity()
// 由于将数组看作为环形,可能 rear < front 因此需要取余数
return (capacity + rear - front) % capacity
}
/* 判断队列是否为空 */
func isEmpty() -> Bool {
rear - front == 0
}
/* 入队 */
func offer(num: Int) {
if size() == capacity() {
print("队列已满")
return
}
// 尾结点后添加 num
nums[rear] = num
// 尾指针向后移动一位,越过尾部后返回到数组头部
rear = (rear + 1) % capacity()
}
/* 出队 */
@discardableResult
func poll() -> Int {
let num = peek()
// 队头指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity()
return num
}
/* 访问队首元素 */
func peek() -> Int {
if isEmpty() {
fatalError("队列为空")
}
return nums[front]
}
}
```
## 队列典型应用

View file

@ -641,7 +641,7 @@ comments: true
/* 判断栈是否为空 */
func isEmpty() -> Bool {
_size == 0
size() == 0
}
/* 入栈 */
@ -653,6 +653,7 @@ comments: true
}
/* 出栈 */
@discardableResult
func pop() -> Int {
let num = peek()
_peek = _peek?.next
@ -662,22 +663,11 @@ comments: true
/* 访问栈顶元素 */
func peek() -> Int {
if _size == 0 {
if isEmpty() {
fatalError("栈为空")
}
return _peek!.val
}
/* 将 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 }) {
res[i] = node!.val
node = node?.next
}
return res
}
}
```
@ -994,8 +984,9 @@ comments: true
}
/* 出栈 */
@discardableResult
func pop() -> Int {
if stack.isEmpty {
if isEmpty() {
fatalError("栈为空")
}
return stack.removeLast()
@ -1003,16 +994,11 @@ comments: true
/* 访问栈顶元素 */
func peek() -> Int {
if stack.isEmpty {
if isEmpty() {
fatalError("栈为空")
}
return stack.last!
}
/* 将 List 转化为 Array 并返回 */
func toArray() -> [Int] {
stack
}
}
```