Add Swift language blocks to the docs.

This commit is contained in:
Yudong Jin
2023-01-08 19:41:05 +08:00
parent 3ba37dba3a
commit 73e3452838
22 changed files with 414 additions and 70 deletions

View File

@@ -228,6 +228,12 @@ comments: true
bool isEmpty = stack.Count()==0;
```
=== "Swift"
```swift title="stack.swift"
```
## 栈的实现
为了更加清晰地了解栈的运行机制,接下来我们来自己动手实现一个栈类。
@@ -591,6 +597,12 @@ comments: true
}
```
=== "Swift"
```swift title="linkedlist_stack.swift"
```
### 基于数组的实现
使用「数组」实现栈时,将数组的尾部当作栈顶,这样可以保证入栈与出栈操作的时间复杂度都为 $O(1)$ 。准确地说,由于入栈的元素可能是源源不断的,我们需要使用可以动态扩容的「列表」。
@@ -870,6 +882,12 @@ comments: true
}
```
=== "Swift"
```swift title="array_stack.swift"
```
!!! tip
某些语言并未专门提供栈类,但我们可以直接把该语言的「数组」或「链表」看作栈来使用,并通过“脑补”来屏蔽无关操作,而无需像上述代码去特意包装一层。