mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 00:04:44 +08:00
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:
@@ -6,96 +6,96 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Deque implemented with a circular array */
|
||||
/* Constructor */
|
||||
class ArrayDeque(capacity: Int) {
|
||||
private var nums: IntArray = IntArray(capacity) // Array for storing double-ended queue elements
|
||||
private var front: Int = 0 // Front pointer, points to the front of the queue element
|
||||
private var queSize: Int = 0 // Double-ended queue length
|
||||
private var nums: IntArray = IntArray(capacity) // Array that stores the deque elements
|
||||
private var front: Int = 0 // Front pointer, pointing to the first element
|
||||
private var queSize: Int = 0 // Deque size
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
/* Get the deque capacity */
|
||||
fun capacity(): Int {
|
||||
return nums.size
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return queSize == 0
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
/* Calculate an index in the circular array */
|
||||
private fun 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
|
||||
return (i + capacity()) % capacity()
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
fun pushFirst(num: Int) {
|
||||
if (queSize == capacity()) {
|
||||
println("Double-ended queue is full")
|
||||
println("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 pointer one position to the left
|
||||
// Use modulo to wrap front to the end when it passes the beginning of the array
|
||||
front = index(front - 1)
|
||||
// Add num to front of queue
|
||||
// Add num to the front
|
||||
nums[front] = num
|
||||
queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
fun pushLast(num: Int) {
|
||||
if (queSize == capacity()) {
|
||||
println("Double-ended queue is full")
|
||||
println("Deque is full")
|
||||
return
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Calculate the rear pointer, which points one position past the last element
|
||||
val rear = index(front + queSize)
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
nums[rear] = num
|
||||
queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
fun popFirst(): Int {
|
||||
val num = peekFirst()
|
||||
// Move front pointer backward by one position
|
||||
// Advance the front pointer by one position
|
||||
front = index(front + 1)
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
fun popLast(): Int {
|
||||
val num = peekLast()
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
fun peekFirst(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return nums[front]
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
fun peekLast(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
// Initialize double-ended queue
|
||||
// Calculate the index of the last element
|
||||
val last = index(front + queSize - 1)
|
||||
return nums[last]
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
fun toArray(): IntArray {
|
||||
// Elements enqueue
|
||||
// Copy only the elements within the occupied range
|
||||
val res = IntArray(queSize)
|
||||
var i = 0
|
||||
var j = front
|
||||
@@ -108,38 +108,38 @@ class ArrayDeque(capacity: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
val deque = ArrayDeque(10)
|
||||
deque.pushLast(3)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
println("Deque deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
val peekFirst = deque.peekFirst()
|
||||
println("Front element peekFirst = $peekFirst")
|
||||
println("Front element: peekFirst = $peekFirst")
|
||||
val peekLast = deque.peekLast()
|
||||
println("Rear element peekLast = $peekLast")
|
||||
println("Rear element: peekLast = $peekLast")
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.pushLast(4)
|
||||
println("After element 4 enqueues at rear, deque = ${deque.toArray().contentToString()}")
|
||||
println("After enqueuing 4 at the rear: deque = ${deque.toArray().contentToString()}")
|
||||
deque.pushFirst(1)
|
||||
println("After element 1 enqueues at front, deque = ${deque.toArray().contentToString()}")
|
||||
println("After enqueuing 1 at the front: deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
val popLast = deque.popLast()
|
||||
println("Dequeue rear element = ${popLast}, after rear dequeue deque = ${deque.toArray().contentToString()}")
|
||||
println("Element dequeued from the rear = ${popLast}; deque after removal = ${deque.toArray().contentToString()}")
|
||||
val popFirst = deque.popFirst()
|
||||
println("Dequeue front element = ${popFirst}, after front dequeue deque = ${deque.toArray().contentToString()}")
|
||||
println("Element dequeued from the front = ${popFirst}; deque after removal = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
val size = deque.size()
|
||||
println("Deque length size = $size")
|
||||
println("Deque size = $size")
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("Is deque empty = $isEmpty")
|
||||
println("Is the deque empty? $isEmpty")
|
||||
}
|
||||
@@ -6,23 +6,23 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
class ArrayQueue(capacity: Int) {
|
||||
private val nums: IntArray = IntArray(capacity) // Array for storing queue elements
|
||||
private var front: Int = 0 // Front pointer, points to the front of the queue element
|
||||
private var queSize: Int = 0 // Queue length
|
||||
private val nums: IntArray = IntArray(capacity) // Array that stores the queue elements
|
||||
private var front: Int = 0 // Front pointer, pointing to the first element
|
||||
private var queSize: Int = 0 // Queue size
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
/* Get the queue capacity */
|
||||
fun capacity(): Int {
|
||||
return nums.size
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return queSize == 0
|
||||
}
|
||||
@@ -33,10 +33,10 @@ class ArrayQueue(capacity: Int) {
|
||||
println("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 pointer, which points one position past the last element
|
||||
// Use modulo to wrap rear to the beginning when it passes the end of the array
|
||||
val rear = (front + queSize) % capacity()
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
nums[rear] = num
|
||||
queSize++
|
||||
}
|
||||
@@ -44,21 +44,21 @@ class ArrayQueue(capacity: Int) {
|
||||
/* Dequeue */
|
||||
fun pop(): Int {
|
||||
val num = peek()
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front pointer by one position, wrapping to the beginning if it passes the end
|
||||
front = (front + 1) % capacity()
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return nums[front]
|
||||
}
|
||||
|
||||
/* Return array */
|
||||
/* Return an array */
|
||||
fun toArray(): IntArray {
|
||||
// Elements enqueue
|
||||
// Copy only the elements within the occupied range
|
||||
val res = IntArray(queSize)
|
||||
var i = 0
|
||||
var j = front
|
||||
@@ -71,13 +71,13 @@ class ArrayQueue(capacity: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
val capacity = 10
|
||||
val queue = ArrayQueue(capacity)
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
@@ -85,26 +85,26 @@ fun main() {
|
||||
queue.push(4)
|
||||
println("Queue queue = ${queue.toArray().contentToString()}")
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
val peek = queue.peek()
|
||||
println("Front element peek = $peek")
|
||||
println("Front element: peek = $peek")
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
val pop = queue.pop()
|
||||
println("Dequeue element pop = ${pop}, after dequeue queue = ${queue.toArray().contentToString()}")
|
||||
println("Dequeued element: pop = ${pop}; queue after removal = ${queue.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
val size = queue.size()
|
||||
println("Queue length size = $size")
|
||||
println("Queue size = $size")
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
val isEmpty = queue.isEmpty()
|
||||
println("Is queue empty = $isEmpty")
|
||||
println("Is the queue empty? $isEmpty")
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (i in 0..9) {
|
||||
queue.push(i)
|
||||
queue.pop()
|
||||
println("After round $i enqueue + dequeue, queue = ${queue.toArray().contentToString()}")
|
||||
println("After enqueue and dequeue round $i: queue = ${queue.toArray().contentToString()}")
|
||||
}
|
||||
}
|
||||
@@ -6,17 +6,17 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
class ArrayStack {
|
||||
// Initialize list (dynamic array)
|
||||
// Initialize the list (dynamic array)
|
||||
private val stack = mutableListOf<Int>()
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
fun size(): Int {
|
||||
return stack.size
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
@@ -32,24 +32,24 @@ class ArrayStack {
|
||||
return stack.removeAt(size() - 1)
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stack[size() - 1]
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the list to an array and return it */
|
||||
fun toArray(): Array<Any> {
|
||||
return stack.toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
val stack = ArrayStack()
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
@@ -57,19 +57,19 @@ fun main() {
|
||||
stack.push(4)
|
||||
println("Stack stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
val peek = stack.peek()
|
||||
println("Top element peek = $peek")
|
||||
println("Top element: peek = $peek")
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
val pop = stack.pop()
|
||||
println("Pop element pop = $pop, after pop stack = ${stack.toArray().contentToString()}")
|
||||
println("Popped element: pop = $pop; stack after removal = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
val size = stack.size()
|
||||
println("Stack length size = $size")
|
||||
println("Stack size = $size")
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("Is stack empty = $isEmpty")
|
||||
println("Is the stack empty? $isEmpty")
|
||||
}
|
||||
@@ -8,38 +8,38 @@ package chapter_stack_and_queue
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
val deque = LinkedList<Int>()
|
||||
deque.offerLast(3)
|
||||
deque.offerLast(2)
|
||||
deque.offerLast(5)
|
||||
println("Deque deque = $deque")
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
val peekFirst = deque.peekFirst()
|
||||
println("Front element peekFirst = $peekFirst")
|
||||
println("Front element: peekFirst = $peekFirst")
|
||||
val peekLast = deque.peekLast()
|
||||
println("Rear element peekLast = $peekLast")
|
||||
println("Rear element: peekLast = $peekLast")
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.offerLast(4)
|
||||
println("After element 4 enqueues at rear, deque = $deque")
|
||||
println("After enqueuing 4 at the rear: deque = $deque")
|
||||
deque.offerFirst(1)
|
||||
println("After element 1 enqueues at front, deque = $deque")
|
||||
println("After enqueuing 1 at the front: deque = $deque")
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
val popLast = deque.pollLast()
|
||||
println("Dequeue rear element = $popLast, after rear dequeue deque = $deque")
|
||||
println("Element dequeued from the rear = $popLast; deque after removal = $deque")
|
||||
val popFirst = deque.pollFirst()
|
||||
println("Dequeue front element = $popFirst, after front dequeue deque = $deque")
|
||||
println("Element dequeued from the front = $popFirst; deque after removal = $deque")
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
val size = deque.size
|
||||
println("Deque length size = $size")
|
||||
println("Deque size = $size")
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("Is deque empty = $isEmpty")
|
||||
println("Is the deque empty? $isEmpty")
|
||||
}
|
||||
@@ -8,23 +8,23 @@ package chapter_stack_and_queue
|
||||
|
||||
/* Doubly linked list node */
|
||||
class ListNode(var _val: Int) {
|
||||
// Node value
|
||||
var next: ListNode? = null // Successor node reference
|
||||
var prev: ListNode? = null // Predecessor node reference
|
||||
// The node value is stored in _val
|
||||
var next: ListNode? = null // Reference to the next node
|
||||
var prev: ListNode? = null // Reference to the previous node
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
/* Deque implemented with a doubly linked list */
|
||||
class LinkedListDeque {
|
||||
private var front: ListNode? = null // Head node front
|
||||
private var rear: ListNode? = null // Tail node rear
|
||||
private var queSize: Int = 0 // Length of the double-ended queue
|
||||
private var queSize: Int = 0 // Deque size
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
@@ -36,28 +36,28 @@ class LinkedListDeque {
|
||||
if (isEmpty()) {
|
||||
rear = node
|
||||
front = rear
|
||||
// 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
|
||||
// Rear of the queue enqueue operation
|
||||
front = node // Update the head node
|
||||
// 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
|
||||
}
|
||||
queSize++ // Update queue length
|
||||
queSize++ // Update the queue size
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
fun pushFirst(num: Int) {
|
||||
push(num, true)
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
fun pushLast(num: Int) {
|
||||
push(num, false)
|
||||
}
|
||||
@@ -67,56 +67,56 @@ class LinkedListDeque {
|
||||
if (isEmpty())
|
||||
throw IndexOutOfBoundsException()
|
||||
val _val: Int
|
||||
// When the deque has one element, front and rear point to the same node
|
||||
// When the queue size is 1, the head and tail point to the same node
|
||||
if (queSize == 1) {
|
||||
_val = front!!._val
|
||||
front = null
|
||||
rear = null
|
||||
}
|
||||
// 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
|
||||
val fNext = front!!.next
|
||||
fNext!!.prev = null
|
||||
front!!.next = null
|
||||
front = fNext // Update head node
|
||||
// Temporarily store tail node value
|
||||
front = fNext // Update the head node
|
||||
// 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
|
||||
val rPrev = rear!!.prev
|
||||
rPrev!!.next = null
|
||||
rear!!.prev = null
|
||||
rear = rPrev // Update tail node
|
||||
rear = rPrev // Update the tail node
|
||||
}
|
||||
queSize-- // Update queue length
|
||||
queSize-- // Update the queue size
|
||||
return _val
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
fun popFirst(): Int {
|
||||
return pop(true)
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
fun popLast(): Int {
|
||||
return pop(false)
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
fun peekFirst(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
fun peekLast(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return rear!!._val
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
fun toArray(): IntArray {
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
@@ -128,38 +128,38 @@ class LinkedListDeque {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
val deque = LinkedListDeque()
|
||||
deque.pushLast(3)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
println("Deque deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
val peekFirst = deque.peekFirst()
|
||||
println("Front element peekFirst = $peekFirst")
|
||||
println("Front element: peekFirst = $peekFirst")
|
||||
val peekLast = deque.peekLast()
|
||||
println("Rear element peekLast = $peekLast")
|
||||
println("Rear element: peekLast = $peekLast")
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.pushLast(4)
|
||||
println("After element 4 enqueues at rear, deque = ${deque.toArray().contentToString()}")
|
||||
println("After enqueuing 4 at the rear: deque = ${deque.toArray().contentToString()}")
|
||||
deque.pushFirst(1)
|
||||
println("After element 1 enqueues at front, deque = ${deque.toArray().contentToString()}")
|
||||
println("After enqueuing 1 at the front: deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
val popLast = deque.popLast()
|
||||
println("Dequeue rear element = ${popLast}, after rear dequeue deque = ${deque.toArray().contentToString()}")
|
||||
println("Element dequeued from the rear = ${popLast}; deque after removal = ${deque.toArray().contentToString()}")
|
||||
val popFirst = deque.popFirst()
|
||||
println("Dequeue front element = ${popFirst}, after front dequeue deque = ${deque.toArray().contentToString()}")
|
||||
println("Element dequeued from the front = ${popFirst}; deque after removal = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
val size = deque.size()
|
||||
println("Deque length size = $size")
|
||||
println("Deque size = $size")
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("Is deque empty = $isEmpty")
|
||||
println("Is the deque empty? $isEmpty")
|
||||
}
|
||||
@@ -6,20 +6,20 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
/* Queue implemented with a linked list */
|
||||
class LinkedListQueue(
|
||||
// Head node front, tail node rear
|
||||
// Head node front and tail node rear
|
||||
private var front: ListNode? = null,
|
||||
private var rear: ListNode? = null,
|
||||
private var queSize: Int = 0
|
||||
) {
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
@@ -28,11 +28,11 @@ class LinkedListQueue(
|
||||
fun push(num: Int) {
|
||||
// Add num after the tail node
|
||||
val node = ListNode(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 == null) {
|
||||
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
|
||||
@@ -43,19 +43,19 @@ class LinkedListQueue(
|
||||
/* Dequeue */
|
||||
fun pop(): Int {
|
||||
val num = peek()
|
||||
// Delete head node
|
||||
// Remove the head node
|
||||
front = front?.next
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
/* Convert the linked list to an array and return it */
|
||||
fun toArray(): IntArray {
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
@@ -67,12 +67,12 @@ class LinkedListQueue(
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
val queue = LinkedListQueue()
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
@@ -80,19 +80,19 @@ fun main() {
|
||||
queue.push(4)
|
||||
println("Queue queue = ${queue.toArray().contentToString()}")
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
val peek = queue.peek()
|
||||
println("Front element peek = $peek")
|
||||
println("Front element: peek = $peek")
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
val pop = queue.pop()
|
||||
println("Dequeue element pop = $pop, after dequeue queue = ${queue.toArray().contentToString()}")
|
||||
println("Dequeued element: pop = $pop; queue after removal = ${queue.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
val size = queue.size()
|
||||
println("Queue length size = $size")
|
||||
println("Queue size = $size")
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
val isEmpty = queue.isEmpty()
|
||||
println("Is queue empty = $isEmpty")
|
||||
println("Is the queue empty? $isEmpty")
|
||||
}
|
||||
@@ -6,18 +6,18 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
/* Stack implemented with a linked list */
|
||||
class LinkedListStack(
|
||||
private var stackPeek: ListNode? = null, // Use head node as stack top
|
||||
private var stkSize: Int = 0 // Stack length
|
||||
private var stackPeek: ListNode? = null, // Use the head node as the stack top
|
||||
private var stkSize: Int = 0 // Stack size
|
||||
) {
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
fun size(): Int {
|
||||
return stkSize
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
@@ -38,13 +38,13 @@ class LinkedListStack(
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
fun peek(): Int? {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stackPeek?._val
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the list to an array and return it */
|
||||
fun toArray(): IntArray {
|
||||
var node = stackPeek
|
||||
val res = IntArray(size())
|
||||
@@ -56,12 +56,12 @@ class LinkedListStack(
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
val stack = LinkedListStack()
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
@@ -69,19 +69,19 @@ fun main() {
|
||||
stack.push(4)
|
||||
println("Stack stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
val peek = stack.peek()!!
|
||||
println("Top element peek = $peek")
|
||||
println("Top element: peek = $peek")
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
val pop = stack.pop()!!
|
||||
println("Pop element pop = $pop, after pop stack = ${stack.toArray().contentToString()}")
|
||||
println("Popped element: pop = $pop; stack after removal = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
val size = stack.size()
|
||||
println("Stack length size = $size")
|
||||
println("Stack size = $size")
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("Is stack empty = $isEmpty")
|
||||
println("Is the stack empty? $isEmpty")
|
||||
}
|
||||
@@ -8,12 +8,12 @@ package chapter_stack_and_queue
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
val queue = LinkedList<Int>()
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.offer(1)
|
||||
queue.offer(3)
|
||||
queue.offer(2)
|
||||
@@ -21,19 +21,19 @@ fun main() {
|
||||
queue.offer(4)
|
||||
println("Queue queue = $queue")
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
val peek = queue.peek()
|
||||
println("Front element peek = $peek")
|
||||
println("Front element: peek = $peek")
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
val pop = queue.poll()
|
||||
println("Dequeue element pop = $pop, after dequeue queue = $queue")
|
||||
println("Dequeued element: pop = $pop; queue after removal = $queue")
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
val size = queue.size
|
||||
println("Queue length size = $size")
|
||||
println("Queue size = $size")
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
val isEmpty = queue.isEmpty()
|
||||
println("Is queue empty = $isEmpty")
|
||||
println("Is the queue empty? $isEmpty")
|
||||
}
|
||||
@@ -8,12 +8,12 @@ package chapter_stack_and_queue
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
fun main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
val stack = Stack<Int>()
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
@@ -21,19 +21,19 @@ fun main() {
|
||||
stack.push(4)
|
||||
println("Stack stack = $stack")
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
val peek = stack.peek()
|
||||
println("Top element peek = $peek")
|
||||
println("Top element: peek = $peek")
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
val pop = stack.pop()
|
||||
println("Pop element pop = $pop, after pop stack = $stack")
|
||||
println("Popped element: pop = $pop; stack after removal = $stack")
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
val size = stack.size
|
||||
println("Stack length size = $size")
|
||||
println("Stack size = $size")
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("Is stack empty = $isEmpty")
|
||||
println("Is the stack empty? $isEmpty")
|
||||
}
|
||||
Reference in New Issue
Block a user