forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request krahets#229 from nuomi1/feature/stack-Swift
feat: add Swift codes for stack article
- Loading branch information
Showing
5 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* File: array_stack.swift | ||
* Created Time: 2023-01-09 | ||
* Author: nuomi1 ([email protected]) | ||
*/ | ||
|
||
/* 基于数组实现的栈 */ | ||
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 changes: 93 additions & 0 deletions
93
codes/swift/chapter_stack_and_queue/linkedlist_stack.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* File: linkedlist_stack.swift | ||
* Created Time: 2023-01-09 | ||
* Author: nuomi1 ([email protected]) | ||
*/ | ||
|
||
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)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* File: stack.swift | ||
* Created Time: 2023-01-09 | ||
* Author: nuomi1 ([email protected]) | ||
*/ | ||
|
||
@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)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters