mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 09:36:28 +08:00
feat: add Swift codes for stack article
This commit is contained in:
parent
a86bdeb7cd
commit
47f017177b
5 changed files with 338 additions and 0 deletions
|
@ -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"]),
|
||||
]
|
||||
)
|
||||
|
|
84
codes/swift/chapter_stack_and_queue/array_stack.swift
Normal file
84
codes/swift/chapter_stack_and_queue/array_stack.swift
Normal file
|
@ -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)")
|
||||
}
|
||||
}
|
93
codes/swift/chapter_stack_and_queue/linkedlist_stack.swift
Normal file
93
codes/swift/chapter_stack_and_queue/linkedlist_stack.swift
Normal file
|
@ -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)")
|
||||
}
|
||||
}
|
39
codes/swift/chapter_stack_and_queue/stack.swift
Normal file
39
codes/swift/chapter_stack_and_queue/stack.swift
Normal file
|
@ -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 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
|
||||
|
|
Loading…
Reference in a new issue