diff --git a/codes/swift/Package.swift b/codes/swift/Package.swift index 3a5a18ed3..6d6eaa8a5 100644 --- a/codes/swift/Package.swift +++ b/codes/swift/Package.swift @@ -13,6 +13,9 @@ let package = Package( .executable(name: "linked_list", targets: ["linked_list"]), .executable(name: "list", targets: ["list"]), .executable(name: "my_list", targets: ["my_list"]), + .executable(name: "stack", targets: ["stack"]), + .executable(name: "linkedlist_stack", targets: ["linkedlist_stack"]), + .executable(name: "array_stack", targets: ["array_stack"]), ], targets: [ .target(name: "utils", path: "utils"), @@ -24,5 +27,8 @@ let package = Package( .executableTarget(name: "linked_list", dependencies: ["utils"], path: "chapter_array_and_linkedlist", sources: ["linked_list.swift"]), .executableTarget(name: "list", path: "chapter_array_and_linkedlist", sources: ["list.swift"]), .executableTarget(name: "my_list", path: "chapter_array_and_linkedlist", sources: ["my_list.swift"]), + .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"]), ] ) diff --git a/codes/swift/chapter_stack_and_queue/array_stack.swift b/codes/swift/chapter_stack_and_queue/array_stack.swift new file mode 100644 index 000000000..faeeaa173 --- /dev/null +++ b/codes/swift/chapter_stack_and_queue/array_stack.swift @@ -0,0 +1,84 @@ +/** + * File: array_stack.swift + * Created Time: 2023-01-09 + * Author: nuomi1 (nuomi1@qq.com) + */ + +/* 基于数组实现的栈 */ +class ArrayStack { + private var stack: [Int] + + init() { + // 初始化列表(动态数组) + stack = [] + } + + /* 获取栈的长度 */ + func size() -> Int { + stack.count + } + + /* 判断栈是否为空 */ + func isEmpty() -> Bool { + stack.isEmpty + } + + /* 入栈 */ + func push(num: Int) { + stack.append(num) + } + + /* 出栈 */ + func pop() -> Int { + if stack.isEmpty { + fatalError("栈为空") + } + return stack.removeLast() + } + + /* 访问栈顶元素 */ + func peek() -> Int { + if stack.isEmpty { + fatalError("栈为空") + } + return stack.last! + } + + /* 将 List 转化为 Array 并返回 */ + func toArray() -> [Int] { + stack + } +} + +@main +enum _ArrayStack { + /* Driver Code */ + static func main() { + /* 初始化栈 */ + let stack = ArrayStack() + + /* 元素入栈 */ + stack.push(num: 1) + stack.push(num: 3) + stack.push(num: 2) + stack.push(num: 5) + stack.push(num: 4) + print("栈 stack = \(stack.toArray())") + + /* 访问栈顶元素 */ + let peek = stack.peek() + print("栈顶元素 peek = \(peek)") + + /* 元素出栈 */ + let pop = stack.pop() + print("出栈元素 pop = \(pop),出栈后 stack = \(stack.toArray())") + + /* 获取栈的长度 */ + let size = stack.size() + print("栈的长度 size = \(size)") + + /* 判断是否为空 */ + let isEmpty = stack.isEmpty() + print("栈是否为空 = \(isEmpty)") + } +} diff --git a/codes/swift/chapter_stack_and_queue/linkedlist_stack.swift b/codes/swift/chapter_stack_and_queue/linkedlist_stack.swift new file mode 100644 index 000000000..9d3c98b01 --- /dev/null +++ b/codes/swift/chapter_stack_and_queue/linkedlist_stack.swift @@ -0,0 +1,93 @@ +/** + * File: linkedlist_stack.swift + * Created Time: 2023-01-09 + * Author: nuomi1 (nuomi1@qq.com) + */ + +import utils + +/* 基于链表实现的栈 */ +class LinkedListStack { + private var _peek: ListNode? // 将头结点作为栈顶 + private var _size = 0 // 栈的长度 + + init() {} + + /* 获取栈的长度 */ + func size() -> Int { + _size + } + + /* 判断栈是否为空 */ + func isEmpty() -> Bool { + _size == 0 + } + + /* 入栈 */ + func push(num: Int) { + let node = ListNode(x: num) + node.next = _peek + _peek = node + _size += 1 + } + + /* 出栈 */ + func pop() -> Int { + let num = peek() + _peek = _peek?.next + _size -= 1 + return num + } + + /* 访问栈顶元素 */ + func peek() -> Int { + if _size == 0 { + 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 + } +} + +@main +enum _LinkedListStack { + /* Driver Code */ + static func main() { + /* 初始化栈 */ + let stack = LinkedListStack() + + /* 元素入栈 */ + stack.push(num: 1) + stack.push(num: 3) + stack.push(num: 2) + stack.push(num: 5) + stack.push(num: 4) + print("栈 stack = \(stack.toArray())") + + /* 访问栈顶元素 */ + let peek = stack.peek() + print("栈顶元素 peek = \(peek)") + + /* 元素出栈 */ + let pop = stack.pop() + print("出栈元素 pop = \(pop),出栈后 stack = \(stack.toArray())") + + /* 获取栈的长度 */ + let size = stack.size() + print("栈的长度 size = \(size)") + + /* 判断是否为空 */ + let isEmpty = stack.isEmpty() + print("栈是否为空 = \(isEmpty)") + } +} diff --git a/codes/swift/chapter_stack_and_queue/stack.swift b/codes/swift/chapter_stack_and_queue/stack.swift new file mode 100644 index 000000000..c6b8bc184 --- /dev/null +++ b/codes/swift/chapter_stack_and_queue/stack.swift @@ -0,0 +1,39 @@ +/** + * File: stack.swift + * Created Time: 2023-01-09 + * Author: nuomi1 (nuomi1@qq.com) + */ + +@main +enum Stack { + /* Driver Code */ + static func main() { + /* 初始化栈 */ + // Swift 没有内置的栈类,可以把 Array 当作栈来使用 + var stack: [Int] = [] + + /* 元素入栈 */ + stack.append(1) + stack.append(3) + stack.append(2) + stack.append(5) + stack.append(4) + print("栈 stack = \(stack)") + + /* 访问栈顶元素 */ + let peek = stack.last! + print("栈顶元素 peek = \(peek)") + + /* 元素出栈 */ + let pop = stack.removeLast() + print("出栈元素 pop = \(pop),出栈后 stack = \(stack)") + + /* 获取栈的长度 */ + let size = stack.count + print("栈的长度 size = \(size)") + + /* 判断是否为空 */ + let isEmpty = stack.isEmpty + print("栈是否为空 = \(isEmpty)") + } +} diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index d5a3f9164..04825695c 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -231,7 +231,28 @@ comments: true === "Swift" ```swift title="stack.swift" + /* 初始化栈 */ + // Swift 没有内置的栈类,可以把 Array 当作栈来使用 + var stack: [Int] = [] + /* 元素入栈 */ + stack.append(1) + stack.append(3) + stack.append(2) + stack.append(5) + stack.append(4) + + /* 访问栈顶元素 */ + let peek = stack.last! + + /* 元素出栈 */ + let pop = stack.removeLast() + + /* 获取栈的长度 */ + let size = stack.count + + /* 判断是否为空 */ + let isEmpty = stack.isEmpty ``` ## 栈的实现 @@ -606,7 +627,58 @@ comments: true === "Swift" ```swift title="linkedlist_stack.swift" + /* 基于链表实现的栈 */ + class LinkedListStack { + private var _peek: ListNode? // 将头结点作为栈顶 + private var _size = 0 // 栈的长度 + init() {} + + /* 获取栈的长度 */ + func size() -> Int { + _size + } + + /* 判断栈是否为空 */ + func isEmpty() -> Bool { + _size == 0 + } + + /* 入栈 */ + func push(num: Int) { + let node = ListNode(x: num) + node.next = _peek + _peek = node + _size += 1 + } + + /* 出栈 */ + func pop() -> Int { + let num = peek() + _peek = _peek?.next + _size -= 1 + return num + } + + /* 访问栈顶元素 */ + func peek() -> Int { + if _size == 0 { + 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 + } + } ``` ### 基于数组的实现 @@ -897,7 +969,51 @@ comments: true === "Swift" ```swift title="array_stack.swift" + /* 基于数组实现的栈 */ + class ArrayStack { + private var stack: [Int] + init() { + // 初始化列表(动态数组) + stack = [] + } + + /* 获取栈的长度 */ + func size() -> Int { + stack.count + } + + /* 判断栈是否为空 */ + func isEmpty() -> Bool { + stack.isEmpty + } + + /* 入栈 */ + func push(num: Int) { + stack.append(num) + } + + /* 出栈 */ + func pop() -> Int { + if stack.isEmpty { + fatalError("栈为空") + } + return stack.removeLast() + } + + /* 访问栈顶元素 */ + func peek() -> Int { + if stack.isEmpty { + fatalError("栈为空") + } + return stack.last! + } + + /* 将 List 转化为 Array 并返回 */ + func toArray() -> [Int] { + stack + } + } ``` !!! tip