feat: add Swift codes for stack article

pull/229/head
nuomi1 2 years ago
parent a86bdeb7cd
commit 47f017177b
No known key found for this signature in database
GPG Key ID: E410D5FF602FBF25

@ -13,6 +13,9 @@ let package = Package(
.executable(name: "linked_list", targets: ["linked_list"]), .executable(name: "linked_list", targets: ["linked_list"]),
.executable(name: "list", targets: ["list"]), .executable(name: "list", targets: ["list"]),
.executable(name: "my_list", targets: ["my_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: [ targets: [
.target(name: "utils", path: "utils"), .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: "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: "list", path: "chapter_array_and_linkedlist", sources: ["list.swift"]),
.executableTarget(name: "my_list", path: "chapter_array_and_linkedlist", sources: ["my_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"]),
] ]
) )

@ -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)")
}
}

@ -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)")
}
}

@ -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)")
}
}

@ -231,7 +231,28 @@ comments: true
=== "Swift" === "Swift"
```swift title="stack.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"
```swift title="linkedlist_stack.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"
```swift title="array_stack.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 !!! tip

Loading…
Cancel
Save