Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -4,11 +4,11 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Double-ended queue based on circular array implementation */
/* Deque implemented with a circular array */
class ArrayDeque {
private var nums: [Int] // Array for storing double-ended queue elements
private var front: Int // Front pointer, points to the front of the queue element
private var _size: Int // Double-ended queue length
private var nums: [Int] // Array that stores the deque elements
private var front: Int // Front index, identifying the first element
private var _size: Int // Deque size
/* Constructor */
init(capacity: Int) {
@@ -17,73 +17,73 @@ class ArrayDeque {
_size = 0
}
/* Get the capacity of the double-ended queue */
/* Get the deque capacity */
func capacity() -> Int {
nums.count
}
/* Get the length of the double-ended queue */
/* Get the deque size */
func size() -> Int {
_size
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Calculate circular array index */
/* Calculate an index in the circular array */
private func index(i: Int) -> Int {
// Use modulo operation to wrap the array head and tail together
// When i passes the tail of the array, return to the head
// When i passes the head of the array, return to the tail
// Use modulo to connect the end of the array to its beginning
// When i passes the end of the array, wrap around to the beginning
// When i passes the beginning of the array, wrap around to the end
(i + capacity()) % capacity()
}
/* Front of the queue enqueue */
/* Enqueue at the front */
func pushFirst(num: Int) {
if size() == capacity() {
print("Double-ended queue is full")
print("Deque is full")
return
}
// Use modulo operation to wrap front around to the tail after passing the head of the array
// Add num to the front of the queue
// Move the front index one position to the left
// Use modulo to wrap front to the end when it passes the beginning of the array
front = index(i: front - 1)
// Add num to front of queue
// Add num to the front
nums[front] = num
_size += 1
}
/* Rear of the queue enqueue */
/* Enqueue at the rear */
func pushLast(num: Int) {
if size() == capacity() {
print("Double-ended queue is full")
print("Deque is full")
return
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Calculate the rear index, one position past the last element
let rear = index(i: front + size())
// Front pointer moves one position backward
// Add num to the rear
nums[rear] = num
_size += 1
}
/* Rear of the queue dequeue */
/* Dequeue from the front */
func popFirst() -> Int {
let num = peekFirst()
// Move front pointer backward by one position
// Advance the front index by one position
front = index(i: front + 1)
_size -= 1
return num
}
/* Access rear of the queue element */
/* Dequeue from the rear */
func popLast() -> Int {
let num = peekLast()
_size -= 1
return num
}
/* Return list for printing */
/* Access the front element */
func peekFirst() -> Int {
if isEmpty() {
fatalError("Deque is empty")
@@ -91,58 +91,58 @@ class ArrayDeque {
return nums[front]
}
/* Driver Code */
/* Access the rear element */
func peekLast() -> Int {
if isEmpty() {
fatalError("Deque is empty")
}
// Initialize double-ended queue
// Calculate the index of the last element
let last = index(i: front + size() - 1)
return nums[last]
}
/* Return array for printing */
/* Return an array for printing */
func toArray() -> [Int] {
// Elements enqueue
// Include only the elements in the occupied range
(front ..< front + size()).map { nums[index(i: $0)] }
}
}
@main
enum _ArrayDeque {
/* Driver Code */
/* Driver code */
static func main() {
/* Get the length of the double-ended queue */
/* Initialize the deque */
let deque = ArrayDeque(capacity: 10)
deque.pushLast(num: 3)
deque.pushLast(num: 2)
deque.pushLast(num: 5)
print("Deque deque = \(deque.toArray())")
/* Update element */
/* Access an element */
let peekFirst = deque.peekFirst()
print("Front element peekFirst = \(peekFirst)")
let peekLast = deque.peekLast()
print("Rear element peekLast = \(peekLast)")
/* Elements enqueue */
/* Enqueue elements */
deque.pushLast(num: 4)
print("After element 4 enqueues at rear, deque = \(deque.toArray())")
print("After enqueueing 4 at the rear, deque = \(deque.toArray())")
deque.pushFirst(num: 1)
print("After element 1 enqueues at front, deque = \(deque.toArray())")
print("After enqueueing 1 at the front, deque = \(deque.toArray())")
/* Element dequeue */
/* Dequeue an element */
let popLast = deque.popLast()
print("Dequeue rear element = \(popLast), after rear dequeue deque = \(deque.toArray())")
print("Element dequeued from the rear = \(popLast); after dequeueing, deque = \(deque.toArray())")
let popFirst = deque.popFirst()
print("Dequeue front element = \(popFirst), after front dequeue deque = \(deque.toArray())")
print("Element dequeued from the front = \(popFirst); after dequeueing, deque = \(deque.toArray())")
/* Get the length of the double-ended queue */
/* Get the deque size */
let size = deque.size()
print("Deque length size = \(size)")
print("Deque size = \(size)")
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
let isEmpty = deque.isEmpty()
print("Is deque empty = \(isEmpty)")
print("Is the deque empty? \(isEmpty)")
}
}

View File

@@ -4,30 +4,30 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Queue based on circular array implementation */
/* Queue implemented with a circular array */
class ArrayQueue {
private var nums: [Int] // Array for storing queue elements
private var front: Int // Front pointer, points to the front of the queue element
private var _size: Int // Queue length
private var nums: [Int] // Array that stores the queue elements
private var front: Int // Front index, identifying the first element
private var _size: Int // Queue size
init(capacity: Int) {
// Initialize array
// Initialize the array
nums = Array(repeating: 0, count: capacity)
front = 0
_size = 0
}
/* Get the capacity of the queue */
/* Get the queue capacity */
func capacity() -> Int {
nums.count
}
/* Get the length of the queue */
/* Get the queue size */
func size() -> Int {
_size
}
/* Check if the queue is empty */
/* Check whether the queue is empty */
func isEmpty() -> Bool {
size() == 0
}
@@ -38,10 +38,10 @@ class ArrayQueue {
print("Queue is full")
return
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Add num to the rear of the queue
// Calculate the rear index, one position past the last element
// Use modulo to wrap rear to the beginning when it passes the end of the array
let rear = (front + size()) % capacity()
// Front pointer moves one position backward
// Add num to the rear
nums[rear] = num
_size += 1
}
@@ -50,13 +50,13 @@ class ArrayQueue {
@discardableResult
func pop() -> Int {
let num = peek()
// Move front pointer backward by one position, if it passes the tail, return to array head
// Advance the front index by one position, wrapping to the beginning if it passes the end
front = (front + 1) % capacity()
_size -= 1
return num
}
/* Return list for printing */
/* Access the front element */
func peek() -> Int {
if isEmpty() {
fatalError("Queue is empty")
@@ -64,22 +64,22 @@ class ArrayQueue {
return nums[front]
}
/* Return array */
/* Return an array */
func toArray() -> [Int] {
// Elements enqueue
// Include only the elements in the occupied range
(front ..< front + size()).map { nums[$0 % capacity()] }
}
}
@main
enum _ArrayQueue {
/* Driver Code */
/* Driver code */
static func main() {
/* Access front of the queue element */
/* Initialize the queue */
let capacity = 10
let queue = ArrayQueue(capacity: capacity)
/* Elements enqueue */
/* Enqueue elements */
queue.push(num: 1)
queue.push(num: 3)
queue.push(num: 2)
@@ -87,27 +87,27 @@ enum _ArrayQueue {
queue.push(num: 4)
print("Queue queue = \(queue.toArray())")
/* Return list for printing */
/* Access the front element */
let peek = queue.peek()
print("Front element peek = \(peek)")
/* Element dequeue */
/* Dequeue an element */
let pop = queue.pop()
print("Dequeue element pop = \(pop), after dequeue queue = \(queue.toArray())")
print("Dequeued element pop = \(pop); after dequeueing, queue = \(queue.toArray())")
/* Get the length of the queue */
/* Get the queue size */
let size = queue.size()
print("Queue length size = \(size)")
print("Queue size = \(size)")
/* Check if the queue is empty */
/* Check whether the queue is empty */
let isEmpty = queue.isEmpty()
print("Is queue empty = \(isEmpty)")
print("Is the queue empty? \(isEmpty)")
/* Test circular array */
/* Test the circular array */
for i in 0 ..< 10 {
queue.push(num: i)
queue.pop()
print("After round \(i) enqueue + dequeue, queue = \(queue.toArray())")
print("After enqueueing and dequeueing in round \(i), queue = \(queue.toArray())")
}
}
}

View File

@@ -4,21 +4,21 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Stack based on array implementation */
/* Stack implemented with an array */
class ArrayStack {
private var stack: [Int]
init() {
// Initialize list (dynamic array)
// Initialize the list (dynamic array)
stack = []
}
/* Get the length of the stack */
/* Get the stack size */
func size() -> Int {
stack.count
}
/* Check if the stack is empty */
/* Check whether the stack is empty */
func isEmpty() -> Bool {
stack.isEmpty
}
@@ -37,7 +37,7 @@ class ArrayStack {
return stack.removeLast()
}
/* Return list for printing */
/* Access the top element */
func peek() -> Int {
if isEmpty() {
fatalError("Stack is empty")
@@ -45,7 +45,7 @@ class ArrayStack {
return stack.last!
}
/* Convert List to Array and return */
/* Return the stack elements as an array */
func toArray() -> [Int] {
stack
}
@@ -53,12 +53,12 @@ class ArrayStack {
@main
enum _ArrayStack {
/* Driver Code */
/* Driver code */
static func main() {
/* Access top of the stack element */
/* Initialize the stack */
let stack = ArrayStack()
/* Elements push onto stack */
/* Push elements onto the stack */
stack.push(num: 1)
stack.push(num: 3)
stack.push(num: 2)
@@ -66,20 +66,20 @@ enum _ArrayStack {
stack.push(num: 4)
print("Stack stack = \(stack.toArray())")
/* Return list for printing */
/* Access the top element */
let peek = stack.peek()
print("Top element peek = \(peek)")
/* Element pop from stack */
/* Pop an element from the stack */
let pop = stack.pop()
print("Pop element pop = \(pop), after pop stack = \(stack.toArray())")
print("Popped element pop = \(pop); after popping, stack = \(stack.toArray())")
/* Get the length of the stack */
/* Get the stack size */
let size = stack.size()
print("Stack length size = \(size)")
print("Stack size = \(size)")
/* Check if empty */
/* Check whether it is empty */
let isEmpty = stack.isEmpty()
print("Is stack empty = \(isEmpty)")
print("Is the stack empty? \(isEmpty)")
}
}

View File

@@ -6,13 +6,13 @@
@main
enum Deque {
/* Driver Code */
/* Driver code */
static func main() {
/* Get the length of the double-ended queue */
// Swift has no built-in deque class, can use Array as deque
/* Initialize the deque */
// The Swift standard library has no built-in deque class; Array can be used as a deque
var deque: [Int] = []
/* Elements enqueue */
/* Enqueue elements */
deque.append(2)
deque.append(5)
deque.append(4)
@@ -20,25 +20,25 @@ enum Deque {
deque.insert(1, at: 0)
print("Deque deque = \(deque)")
/* Update element */
/* Access an element */
let peekFirst = deque.first!
print("Front element peekFirst = \(peekFirst)")
let peekLast = deque.last!
print("Rear element peekLast = \(peekLast)")
/* Element dequeue */
// When simulating with Array, popFirst complexity is O(n)
/* Dequeue an element */
// When using Array as a deque, removing the first element takes O(n) time
let popFirst = deque.removeFirst()
print("Dequeue front element popFirst = \(popFirst), after front dequeue deque = \(deque)")
print("Element dequeued from the front, popFirst = \(popFirst); after dequeueing, deque = \(deque)")
let popLast = deque.removeLast()
print("Dequeue rear element popLast = \(popLast), after rear dequeue deque = \(deque)")
print("Element dequeued from the rear, popLast = \(popLast); after dequeueing, deque = \(deque)")
/* Get the length of the double-ended queue */
/* Get the deque size */
let size = deque.count
print("Deque length size = \(size)")
print("Deque size = \(size)")
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
let isEmpty = deque.isEmpty
print("Is deque empty = \(isEmpty)")
print("Is the deque empty? \(isEmpty)")
}
}

View File

@@ -7,30 +7,30 @@
/* Doubly linked list node */
class ListNode {
var val: Int // Node value
var next: ListNode? // Successor node reference
weak var prev: ListNode? // Predecessor node reference
var next: ListNode? // Reference to the next node
weak var prev: ListNode? // Reference to the previous node
init(val: Int) {
self.val = val
}
}
/* Double-ended queue based on doubly linked list implementation */
/* Deque implemented with a doubly linked list */
class LinkedListDeque {
private var front: ListNode? // Head node front
private var rear: ListNode? // Tail node rear
private var _size: Int // Length of the double-ended queue
private var _size: Int // Deque size
init() {
_size = 0
}
/* Get the length of the double-ended queue */
/* Get the deque size */
func size() -> Int {
_size
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
func isEmpty() -> Bool {
size() == 0
}
@@ -43,29 +43,29 @@ class LinkedListDeque {
front = node
rear = node
}
// Front of the queue enqueue operation
// Enqueue at the front
else if isFront {
// Add node to the head of the linked list
front?.prev = node
node.next = front
front = node // Update head node
front = node // Update the head node
}
// Rear of the queue enqueue operation
// Enqueue at the rear
else {
// Add node to the tail of the linked list
rear?.next = node
node.prev = rear
rear = node // Update tail node
rear = node // Update the tail node
}
_size += 1 // Update queue length
_size += 1 // Update the deque size
}
/* Front of the queue enqueue */
/* Enqueue at the front */
func pushFirst(num: Int) {
push(num: num, isFront: true)
}
/* Rear of the queue enqueue */
/* Enqueue at the rear */
func pushLast(num: Int) {
push(num: num, isFront: false)
}
@@ -76,45 +76,45 @@ class LinkedListDeque {
fatalError("Deque is empty")
}
let val: Int
// When the deque has one element, front and rear point to the same node
// When the deque size is 1, the head and tail point to the same node
if _size == 1 {
val = front!.val
front = nil
rear = nil
}
// Temporarily store head node value
// Dequeue from the front
else if isFront {
val = front!.val // Delete head node
// Delete head node
val = front!.val // Save the head node value
// Remove the head node
let fNext = front?.next
fNext?.prev = nil
front?.next = nil
front = fNext // Update head node
front = fNext // Update the head node
}
// Temporarily store tail node value
// Dequeue from the rear
else {
val = rear!.val // Delete tail node
// Update tail node
val = rear!.val // Save the tail node value
// Remove the tail node
let rPrev = rear?.prev
rPrev?.next = nil
rear?.prev = nil
rear = rPrev // Update tail node
rear = rPrev // Update the tail node
}
_size -= 1 // Update queue length
_size -= 1 // Update the deque size
return val
}
/* Rear of the queue dequeue */
/* Dequeue from the front */
func popFirst() -> Int {
pop(isFront: true)
}
/* Access rear of the queue element */
/* Dequeue from the rear */
func popLast() -> Int {
pop(isFront: false)
}
/* Return list for printing */
/* Access the front element */
func peekFirst() -> Int {
if isEmpty() {
fatalError("Deque is empty")
@@ -122,7 +122,7 @@ class LinkedListDeque {
return front!.val
}
/* Driver Code */
/* Access the rear element */
func peekLast() -> Int {
if isEmpty() {
fatalError("Deque is empty")
@@ -130,7 +130,7 @@ class LinkedListDeque {
return rear!.val
}
/* Return array for printing */
/* Return an array for printing */
func toArray() -> [Int] {
var node = front
var res = Array(repeating: 0, count: size())
@@ -144,39 +144,39 @@ class LinkedListDeque {
@main
enum _LinkedListDeque {
/* Driver Code */
/* Driver code */
static func main() {
/* Get the length of the double-ended queue */
/* Initialize the deque */
let deque = LinkedListDeque()
deque.pushLast(num: 3)
deque.pushLast(num: 2)
deque.pushLast(num: 5)
print("Deque deque = \(deque.toArray())")
/* Update element */
/* Access an element */
let peekFirst = deque.peekFirst()
print("Front element peekFirst = \(peekFirst)")
let peekLast = deque.peekLast()
print("Rear element peekLast = \(peekLast)")
/* Elements enqueue */
/* Enqueue elements */
deque.pushLast(num: 4)
print("After element 4 enqueues at rear, deque = \(deque.toArray())")
print("After enqueueing 4 at the rear, deque = \(deque.toArray())")
deque.pushFirst(num: 1)
print("After element 1 enqueues at front, deque = \(deque.toArray())")
print("After enqueueing 1 at the front, deque = \(deque.toArray())")
/* Element dequeue */
/* Dequeue an element */
let popLast = deque.popLast()
print("Dequeue rear element = \(popLast), after rear dequeue deque = \(deque.toArray())")
print("Element dequeued from the rear = \(popLast); after dequeueing, deque = \(deque.toArray())")
let popFirst = deque.popFirst()
print("Dequeue front element = \(popFirst), after front dequeue deque = \(deque.toArray())")
print("Element dequeued from the front = \(popFirst); after dequeueing, deque = \(deque.toArray())")
/* Get the length of the double-ended queue */
/* Get the deque size */
let size = deque.size()
print("Deque length size = \(size)")
print("Deque size = \(size)")
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
let isEmpty = deque.isEmpty()
print("Is deque empty = \(isEmpty)")
print("Is the deque empty? \(isEmpty)")
}
}

View File

@@ -6,7 +6,7 @@
import utils
/* Queue based on linked list implementation */
/* Queue implemented with a linked list */
class LinkedListQueue {
private var front: ListNode? // Head node
private var rear: ListNode? // Tail node
@@ -16,12 +16,12 @@ class LinkedListQueue {
_size = 0
}
/* Get the length of the queue */
/* Get the queue size */
func size() -> Int {
_size
}
/* Check if the queue is empty */
/* Check whether the queue is empty */
func isEmpty() -> Bool {
size() == 0
}
@@ -30,12 +30,12 @@ class LinkedListQueue {
func push(num: Int) {
// Add num after the tail node
let node = ListNode(x: num)
// If the queue is empty, make both front and rear point to the node
// If the queue is empty, make both the head and tail point to this node
if front == nil {
front = node
rear = node
}
// If the queue is not empty, add the node after the tail node
// If the queue is not empty, add this node after the tail node
else {
rear?.next = node
rear = node
@@ -47,13 +47,13 @@ class LinkedListQueue {
@discardableResult
func pop() -> Int {
let num = peek()
// Delete head node
// Remove the head node
front = front?.next
_size -= 1
return num
}
/* Return list for printing */
/* Access the front element */
func peek() -> Int {
if isEmpty() {
fatalError("Queue is empty")
@@ -61,7 +61,7 @@ class LinkedListQueue {
return front!.val
}
/* Convert linked list to Array and return */
/* Convert the linked list to an array and return it */
func toArray() -> [Int] {
var node = front
var res = Array(repeating: 0, count: size())
@@ -75,12 +75,12 @@ class LinkedListQueue {
@main
enum _LinkedListQueue {
/* Driver Code */
/* Driver code */
static func main() {
/* Access front of the queue element */
/* Initialize the queue */
let queue = LinkedListQueue()
/* Elements enqueue */
/* Enqueue elements */
queue.push(num: 1)
queue.push(num: 3)
queue.push(num: 2)
@@ -88,20 +88,20 @@ enum _LinkedListQueue {
queue.push(num: 4)
print("Queue queue = \(queue.toArray())")
/* Return list for printing */
/* Access the front element */
let peek = queue.peek()
print("Front element peek = \(peek)")
/* Element dequeue */
/* Dequeue an element */
let pop = queue.pop()
print("Dequeue element pop = \(pop), after dequeue queue = \(queue.toArray())")
print("Dequeued element pop = \(pop); after dequeueing, queue = \(queue.toArray())")
/* Get the length of the queue */
/* Get the queue size */
let size = queue.size()
print("Queue length size = \(size)")
print("Queue size = \(size)")
/* Check if the queue is empty */
/* Check whether the queue is empty */
let isEmpty = queue.isEmpty()
print("Is queue empty = \(isEmpty)")
print("Is the queue empty? \(isEmpty)")
}
}

View File

@@ -6,21 +6,21 @@
import utils
/* Stack based on linked list implementation */
/* Stack implemented with a linked list */
class LinkedListStack {
private var _peek: ListNode? // Use head node as stack top
private var _size: Int // Stack length
private var _peek: ListNode? // Use the head node as the stack top
private var _size: Int // Stack size
init() {
_size = 0
}
/* Get the length of the stack */
/* Get the stack size */
func size() -> Int {
_size
}
/* Check if the stack is empty */
/* Check whether the stack is empty */
func isEmpty() -> Bool {
size() == 0
}
@@ -42,7 +42,7 @@ class LinkedListStack {
return num
}
/* Return list for printing */
/* Access the top element */
func peek() -> Int {
if isEmpty() {
fatalError("Stack is empty")
@@ -50,7 +50,7 @@ class LinkedListStack {
return _peek!.val
}
/* Convert List to Array and return */
/* Convert the linked list to an array and return it */
func toArray() -> [Int] {
var node = _peek
var res = Array(repeating: 0, count: size())
@@ -64,12 +64,12 @@ class LinkedListStack {
@main
enum _LinkedListStack {
/* Driver Code */
/* Driver code */
static func main() {
/* Access top of the stack element */
/* Initialize the stack */
let stack = LinkedListStack()
/* Elements push onto stack */
/* Push elements onto the stack */
stack.push(num: 1)
stack.push(num: 3)
stack.push(num: 2)
@@ -77,20 +77,20 @@ enum _LinkedListStack {
stack.push(num: 4)
print("Stack stack = \(stack.toArray())")
/* Return list for printing */
/* Access the top element */
let peek = stack.peek()
print("Top element peek = \(peek)")
/* Element pop from stack */
/* Pop an element from the stack */
let pop = stack.pop()
print("Pop element pop = \(pop), after pop stack = \(stack.toArray())")
print("Popped element pop = \(pop); after popping, stack = \(stack.toArray())")
/* Get the length of the stack */
/* Get the stack size */
let size = stack.size()
print("Stack length size = \(size)")
print("Stack size = \(size)")
/* Check if empty */
/* Check whether it is empty */
let isEmpty = stack.isEmpty()
print("Is stack empty = \(isEmpty)")
print("Is the stack empty? \(isEmpty)")
}
}

View File

@@ -6,13 +6,13 @@
@main
enum Queue {
/* Driver Code */
/* Driver code */
static func main() {
/* Access front of the queue element */
// Swift has no built-in queue class, can use Array as queue
/* Initialize the queue */
// The Swift standard library has no built-in queue class; Array can be used as a queue
var queue: [Int] = []
/* Elements enqueue */
/* Enqueue elements */
queue.append(1)
queue.append(3)
queue.append(2)
@@ -20,21 +20,21 @@ enum Queue {
queue.append(4)
print("Queue queue = \(queue)")
/* Return list for printing */
/* Access the front element */
let peek = queue.first!
print("Front element peek = \(peek)")
/* Element dequeue */
// When simulating with Array, pop complexity is O(n)
/* Dequeue an element */
// When using Array as a queue, dequeueing takes O(n) time
let pop = queue.removeFirst()
print("Dequeue element pop = \(pop), after dequeue queue = \(queue)")
print("Dequeued element pop = \(pop); after dequeueing, queue = \(queue)")
/* Get the length of the queue */
/* Get the queue size */
let size = queue.count
print("Queue length size = \(size)")
print("Queue size = \(size)")
/* Check if the queue is empty */
/* Check whether the queue is empty */
let isEmpty = queue.isEmpty
print("Is queue empty = \(isEmpty)")
print("Is the queue empty? \(isEmpty)")
}
}

View File

@@ -6,13 +6,13 @@
@main
enum Stack {
/* Driver Code */
/* Driver code */
static func main() {
/* Access top of the stack element */
// Swift has no built-in stack class, can use Array as stack
/* Initialize the stack */
// The Swift standard library has no built-in stack class; Array can be used as a stack
var stack: [Int] = []
/* Elements push onto stack */
/* Push elements onto the stack */
stack.append(1)
stack.append(3)
stack.append(2)
@@ -20,20 +20,20 @@ enum Stack {
stack.append(4)
print("Stack stack = \(stack)")
/* Return list for printing */
/* Access the top element */
let peek = stack.last!
print("Top element peek = \(peek)")
/* Element pop from stack */
/* Pop an element from the stack */
let pop = stack.removeLast()
print("Pop element pop = \(pop), after pop stack = \(stack)")
print("Popped element pop = \(pop); after popping, stack = \(stack)")
/* Get the length of the stack */
/* Get the stack size */
let size = stack.count
print("Stack length size = \(size)")
print("Stack size = \(size)")
/* Check if empty */
/* Check whether it is empty */
let isEmpty = stack.isEmpty
print("Is stack empty = \(isEmpty)")
print("Is the stack empty? \(isEmpty)")
}
}