mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
refactor: use internal method and remove toArray() method
This commit is contained in:
@ -29,8 +29,9 @@ class ArrayStack {
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
@discardableResult
|
||||
func pop() -> Int {
|
||||
if stack.isEmpty {
|
||||
if isEmpty() {
|
||||
fatalError("栈为空")
|
||||
}
|
||||
return stack.removeLast()
|
||||
@ -38,7 +39,7 @@ class ArrayStack {
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
func peek() -> Int {
|
||||
if stack.isEmpty {
|
||||
if isEmpty() {
|
||||
fatalError("栈为空")
|
||||
}
|
||||
return stack.last!
|
||||
|
||||
@ -20,7 +20,7 @@ class LinkedListStack {
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
func isEmpty() -> Bool {
|
||||
_size == 0
|
||||
size() == 0
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
@ -32,6 +32,7 @@ class LinkedListStack {
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
@discardableResult
|
||||
func pop() -> Int {
|
||||
let num = peek()
|
||||
_peek = _peek?.next
|
||||
@ -41,7 +42,7 @@ class LinkedListStack {
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
func peek() -> Int {
|
||||
if _size == 0 {
|
||||
if isEmpty() {
|
||||
fatalError("栈为空")
|
||||
}
|
||||
return _peek!.val
|
||||
|
||||
Reference in New Issue
Block a user