mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-13 15:45:41 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -6,7 +6,7 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
class ArrayDeque:
|
||||
"""Double-ended queue class based on circular array"""
|
||||
"""Double-ended queue based on circular array implementation"""
|
||||
|
||||
def __init__(self, capacity: int):
|
||||
"""Constructor"""
|
||||
@@ -23,70 +23,70 @@ class ArrayDeque:
|
||||
return self._size
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if the double-ended queue is empty"""
|
||||
"""Check if the double-ended queue is empty"""
|
||||
return self._size == 0
|
||||
|
||||
def index(self, i: int) -> int:
|
||||
"""Calculate circular array index"""
|
||||
# Implement circular array by modulo operation
|
||||
# When i exceeds the tail of the array, return to the head
|
||||
# When i exceeds the head of the array, return to the tail
|
||||
# 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
|
||||
return (i + self.capacity()) % self.capacity()
|
||||
|
||||
def push_first(self, num: int):
|
||||
"""Front enqueue"""
|
||||
"""Front of the queue enqueue"""
|
||||
if self._size == self.capacity():
|
||||
print("Double-ended queue is full")
|
||||
return
|
||||
# Move the front pointer one position to the left
|
||||
# Implement front crossing the head of the array to return to the tail by modulo operation
|
||||
# Front pointer moves one position to the left
|
||||
# Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
self._front = self.index(self._front - 1)
|
||||
# Add num to the front
|
||||
# Add num to the front of the queue
|
||||
self._nums[self._front] = num
|
||||
self._size += 1
|
||||
|
||||
def push_last(self, num: int):
|
||||
"""Rear enqueue"""
|
||||
"""Rear of the queue enqueue"""
|
||||
if self._size == self.capacity():
|
||||
print("Double-ended queue is full")
|
||||
return
|
||||
# Calculate rear pointer, pointing to rear index + 1
|
||||
# Calculate rear pointer, points to rear index + 1
|
||||
rear = self.index(self._front + self._size)
|
||||
# Add num to the rear
|
||||
# Add num to the rear of the queue
|
||||
self._nums[rear] = num
|
||||
self._size += 1
|
||||
|
||||
def pop_first(self) -> int:
|
||||
"""Front dequeue"""
|
||||
"""Front of the queue dequeue"""
|
||||
num = self.peek_first()
|
||||
# Move front pointer one position backward
|
||||
# Front pointer moves one position backward
|
||||
self._front = self.index(self._front + 1)
|
||||
self._size -= 1
|
||||
return num
|
||||
|
||||
def pop_last(self) -> int:
|
||||
"""Rear dequeue"""
|
||||
"""Rear of the queue dequeue"""
|
||||
num = self.peek_last()
|
||||
self._size -= 1
|
||||
return num
|
||||
|
||||
def peek_first(self) -> int:
|
||||
"""Access front element"""
|
||||
"""Access front of the queue element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Double-ended queue is empty")
|
||||
return self._nums[self._front]
|
||||
|
||||
def peek_last(self) -> int:
|
||||
"""Access rear element"""
|
||||
"""Access rear of the queue element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Double-ended queue is empty")
|
||||
# Calculate rear element index
|
||||
# Calculate tail element index
|
||||
last = self.index(self._front + self._size - 1)
|
||||
return self._nums[last]
|
||||
|
||||
def to_array(self) -> list[int]:
|
||||
"""Return array for printing"""
|
||||
# Only convert elements within valid length range
|
||||
# Only convert list elements within the valid length range
|
||||
res = []
|
||||
for i in range(self._size):
|
||||
res.append(self._nums[self.index(self._front + i)])
|
||||
@@ -100,30 +100,30 @@ if __name__ == "__main__":
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
print("Double-ended queue deque =", deque.to_array())
|
||||
print("double-ended queue deque =", deque.to_array())
|
||||
|
||||
# Access element
|
||||
# Access elements
|
||||
peek_first: int = deque.peek_first()
|
||||
print("Front element peek_first =", peek_first)
|
||||
print("Front of the queue element peek_first =", peek_first)
|
||||
peek_last: int = deque.peek_last()
|
||||
print("Rear element peek_last =", peek_last)
|
||||
print("Rear of the queue element peek_last =", peek_last)
|
||||
|
||||
# Element enqueue
|
||||
# Elements enqueue
|
||||
deque.push_last(4)
|
||||
print("Element 4 rear enqueued, deque =", deque.to_array())
|
||||
print("Element 4 rear enqueue after deque =", deque.to_array())
|
||||
deque.push_first(1)
|
||||
print("Element 1 front enqueued, deque =", deque.to_array())
|
||||
print("Element 1 front enqueue after deque =", deque.to_array())
|
||||
|
||||
# Element dequeue
|
||||
# Elements dequeue
|
||||
pop_last: int = deque.pop_last()
|
||||
print("Rear dequeued element =", pop_last, ", deque after rear dequeue =", deque.to_array())
|
||||
print("Rear dequeued element =", pop_last, ", rear dequeue after deque =", deque.to_array())
|
||||
pop_first: int = deque.pop_first()
|
||||
print("Front dequeued element =", pop_first, ", deque after front dequeue =", deque.to_array())
|
||||
print("Front dequeued element =", pop_first, ", front dequeue after deque =", deque.to_array())
|
||||
|
||||
# Get the length of the double-ended queue
|
||||
size: int = deque.size()
|
||||
print("Double-ended queue length size =", size)
|
||||
print("Length of the double-ended queue size =", size)
|
||||
|
||||
# Determine if the double-ended queue is empty
|
||||
# Check if the double-ended queue is empty
|
||||
is_empty: bool = deque.is_empty()
|
||||
print("Is the double-ended queue empty =", is_empty)
|
||||
|
||||
@@ -6,12 +6,12 @@ Author: Peng Chen (pengchzn@gmail.com)
|
||||
|
||||
|
||||
class ArrayQueue:
|
||||
"""Queue class based on circular array"""
|
||||
"""Queue based on circular array implementation"""
|
||||
|
||||
def __init__(self, size: int):
|
||||
"""Constructor"""
|
||||
self._nums: list[int] = [0] * size # Array for storing queue elements
|
||||
self._front: int = 0 # Front pointer, pointing to the front element
|
||||
self._front: int = 0 # Front pointer, points to the front of the queue element
|
||||
self._size: int = 0 # Queue length
|
||||
|
||||
def capacity(self) -> int:
|
||||
@@ -23,36 +23,36 @@ class ArrayQueue:
|
||||
return self._size
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if the queue is empty"""
|
||||
"""Check if the queue is empty"""
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int):
|
||||
"""Enqueue"""
|
||||
if self._size == self.capacity():
|
||||
raise IndexError("Queue is full")
|
||||
# Calculate rear pointer, pointing to rear index + 1
|
||||
# Use modulo operation to wrap the rear pointer from the end of the array back to the start
|
||||
# Calculate rear pointer, points to rear index + 1
|
||||
# Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
rear: int = (self._front + self._size) % self.capacity()
|
||||
# Add num to the rear
|
||||
# Add num to the rear of the queue
|
||||
self._nums[rear] = num
|
||||
self._size += 1
|
||||
|
||||
def pop(self) -> int:
|
||||
"""Dequeue"""
|
||||
num: int = self.peek()
|
||||
# Move front pointer one position backward, returning to the head of the array if it exceeds the tail
|
||||
# Front pointer moves one position backward, if it passes the tail, return to the head of the array
|
||||
self._front = (self._front + 1) % self.capacity()
|
||||
self._size -= 1
|
||||
return num
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access front element"""
|
||||
"""Access front of the queue element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Queue is empty")
|
||||
return self._nums[self._front]
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
"""Return array for printing"""
|
||||
"""Return list for printing"""
|
||||
res = [0] * self.size()
|
||||
j: int = self._front
|
||||
for i in range(self.size()):
|
||||
@@ -66,28 +66,28 @@ if __name__ == "__main__":
|
||||
# Initialize queue
|
||||
queue = ArrayQueue(10)
|
||||
|
||||
# Element enqueue
|
||||
# Elements enqueue
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
print("Queue queue =", queue.to_list())
|
||||
print("queue =", queue.to_list())
|
||||
|
||||
# Access front element
|
||||
# Access front of the queue element
|
||||
peek: int = queue.peek()
|
||||
print("Front element peek =", peek)
|
||||
print("Front of the queue element peek =", peek)
|
||||
|
||||
# Element dequeue
|
||||
pop: int = queue.pop()
|
||||
print("Dequeued element pop =", pop)
|
||||
print("Queue after dequeue =", queue.to_list())
|
||||
print("After dequeue queue =", queue.to_list())
|
||||
|
||||
# Get the length of the queue
|
||||
size: int = queue.size()
|
||||
print("Queue length size =", size)
|
||||
print("Length of the queue size =", size)
|
||||
|
||||
# Determine if the queue is empty
|
||||
# Check if the queue is empty
|
||||
is_empty: bool = queue.is_empty()
|
||||
print("Is the queue empty =", is_empty)
|
||||
|
||||
@@ -95,4 +95,4 @@ if __name__ == "__main__":
|
||||
for i in range(10):
|
||||
queue.push(i)
|
||||
queue.pop()
|
||||
print("In the ", i, "th round of enqueue + dequeue, queue = ", queue.to_list())
|
||||
print("Round", i, "enqueue + dequeue after queue = ", queue.to_list())
|
||||
|
||||
@@ -6,7 +6,7 @@ Author: Peng Chen (pengchzn@gmail.com)
|
||||
|
||||
|
||||
class ArrayStack:
|
||||
"""Stack class based on array"""
|
||||
"""Stack based on array implementation"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
@@ -17,7 +17,7 @@ class ArrayStack:
|
||||
return len(self._stack)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if the stack is empty"""
|
||||
"""Check if the stack is empty"""
|
||||
return self.size() == 0
|
||||
|
||||
def push(self, item: int):
|
||||
@@ -31,13 +31,13 @@ class ArrayStack:
|
||||
return self._stack.pop()
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access stack top element"""
|
||||
"""Access top of the stack element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Stack is empty")
|
||||
return self._stack[-1]
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
"""Return array for printing"""
|
||||
"""Return list for printing"""
|
||||
return self._stack
|
||||
|
||||
|
||||
@@ -46,27 +46,27 @@ if __name__ == "__main__":
|
||||
# Initialize stack
|
||||
stack = ArrayStack()
|
||||
|
||||
# Element push
|
||||
# Elements push onto stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
print("Stack stack =", stack.to_list())
|
||||
print("stack =", stack.to_list())
|
||||
|
||||
# Access stack top element
|
||||
# Access top of the stack element
|
||||
peek: int = stack.peek()
|
||||
print("Stack top element peek =", peek)
|
||||
print("Top of the stack element peek =", peek)
|
||||
|
||||
# Element pop
|
||||
# Element pop from stack
|
||||
pop: int = stack.pop()
|
||||
print("Popped element pop =", pop)
|
||||
print("Stack after pop =", stack.to_list())
|
||||
print("After pop stack =", stack.to_list())
|
||||
|
||||
# Get the length of the stack
|
||||
size: int = stack.size()
|
||||
print("Stack length size =", size)
|
||||
print("Length of the stack size =", size)
|
||||
|
||||
# Determine if it's empty
|
||||
# Check if it is empty
|
||||
is_empty: bool = stack.is_empty()
|
||||
print("Is the stack empty =", is_empty)
|
||||
|
||||
@@ -11,32 +11,32 @@ if __name__ == "__main__":
|
||||
# Initialize double-ended queue
|
||||
deq: deque[int] = deque()
|
||||
|
||||
# Element enqueue
|
||||
deq.append(2) # Add to rear
|
||||
# Elements enqueue
|
||||
deq.append(2) # Add to the rear of the queue
|
||||
deq.append(5)
|
||||
deq.append(4)
|
||||
deq.appendleft(3) # Add to front
|
||||
deq.appendleft(3) # Add to the front of the queue
|
||||
deq.appendleft(1)
|
||||
print("Double-ended queue deque =", deq)
|
||||
print("double-ended queue deque =", deq)
|
||||
|
||||
# Access element
|
||||
front: int = deq[0] # Front element
|
||||
print("Front element front =", front)
|
||||
rear: int = deq[-1] # Rear element
|
||||
print("Rear element rear =", rear)
|
||||
# Access elements
|
||||
front: int = deq[0] # Front of the queue element
|
||||
print("Front of the queue element front =", front)
|
||||
rear: int = deq[-1] # Rear of the queue element
|
||||
print("Rear of the queue element rear =", rear)
|
||||
|
||||
# Element dequeue
|
||||
pop_front: int = deq.popleft() # Front element dequeue
|
||||
# Elements dequeue
|
||||
pop_front: int = deq.popleft() # Front of the queue element dequeues
|
||||
print("Front dequeued element pop_front =", pop_front)
|
||||
print("Deque after front dequeue =", deq)
|
||||
pop_rear: int = deq.pop() # Rear element dequeue
|
||||
print("After front dequeue deque =", deq)
|
||||
pop_rear: int = deq.pop() # Rear of the queue element dequeues
|
||||
print("Rear dequeued element pop_rear =", pop_rear)
|
||||
print("Deque after rear dequeue =", deq)
|
||||
print("After rear dequeue deque =", deq)
|
||||
|
||||
# Get the length of the double-ended queue
|
||||
size: int = len(deq)
|
||||
print("Double-ended queue length size =", size)
|
||||
print("Length of the double-ended queue size =", size)
|
||||
|
||||
# Determine if the double-ended queue is empty
|
||||
# Check if the double-ended queue is empty
|
||||
is_empty: bool = len(deq) == 0
|
||||
print("Is the double-ended queue empty =", is_empty)
|
||||
|
||||
@@ -6,17 +6,17 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
class ListNode:
|
||||
"""Double-linked list node"""
|
||||
"""Doubly linked list node"""
|
||||
|
||||
def __init__(self, val: int):
|
||||
"""Constructor"""
|
||||
self.val: int = val
|
||||
self.next: ListNode | None = None # Reference to successor node
|
||||
self.prev: ListNode | None = None # Reference to predecessor node
|
||||
self.next: ListNode | None = None # Successor node reference
|
||||
self.prev: ListNode | None = None # Predecessor node reference
|
||||
|
||||
|
||||
class LinkedListDeque:
|
||||
"""Double-ended queue class based on double-linked list"""
|
||||
"""Double-ended queue based on doubly linked list implementation"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
@@ -29,54 +29,54 @@ class LinkedListDeque:
|
||||
return self._size
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if the double-ended queue is empty"""
|
||||
"""Check if the double-ended queue is empty"""
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int, is_front: bool):
|
||||
"""Enqueue operation"""
|
||||
node = ListNode(num)
|
||||
# If the list is empty, make front and rear both point to node
|
||||
# If the linked list is empty, make both front and rear point to node
|
||||
if self.is_empty():
|
||||
self._front = self._rear = node
|
||||
# Front enqueue operation
|
||||
# Front of the queue enqueue operation
|
||||
elif is_front:
|
||||
# Add node to the head of the list
|
||||
# Add node to the head of the linked list
|
||||
self._front.prev = node
|
||||
node.next = self._front
|
||||
self._front = node # Update head node
|
||||
# Rear enqueue operation
|
||||
# Rear of the queue enqueue operation
|
||||
else:
|
||||
# Add node to the tail of the list
|
||||
# Add node to the tail of the linked list
|
||||
self._rear.next = node
|
||||
node.prev = self._rear
|
||||
self._rear = node # Update tail node
|
||||
self._size += 1 # Update queue length
|
||||
|
||||
def push_first(self, num: int):
|
||||
"""Front enqueue"""
|
||||
"""Front of the queue enqueue"""
|
||||
self.push(num, True)
|
||||
|
||||
def push_last(self, num: int):
|
||||
"""Rear enqueue"""
|
||||
"""Rear of the queue enqueue"""
|
||||
self.push(num, False)
|
||||
|
||||
def pop(self, is_front: bool) -> int:
|
||||
"""Dequeue operation"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Double-ended queue is empty")
|
||||
# Front dequeue operation
|
||||
# Front of the queue dequeue operation
|
||||
if is_front:
|
||||
val: int = self._front.val # Temporarily store the head node value
|
||||
# Remove head node
|
||||
val: int = self._front.val # Temporarily store head node value
|
||||
# Delete head node
|
||||
fnext: ListNode | None = self._front.next
|
||||
if fnext is not None:
|
||||
fnext.prev = None
|
||||
self._front.next = None
|
||||
self._front = fnext # Update head node
|
||||
# Rear dequeue operation
|
||||
# Rear of the queue dequeue operation
|
||||
else:
|
||||
val: int = self._rear.val # Temporarily store the tail node value
|
||||
# Remove tail node
|
||||
val: int = self._rear.val # Temporarily store tail node value
|
||||
# Delete tail node
|
||||
rprev: ListNode | None = self._rear.prev
|
||||
if rprev is not None:
|
||||
rprev.next = None
|
||||
@@ -86,21 +86,21 @@ class LinkedListDeque:
|
||||
return val
|
||||
|
||||
def pop_first(self) -> int:
|
||||
"""Front dequeue"""
|
||||
"""Front of the queue dequeue"""
|
||||
return self.pop(True)
|
||||
|
||||
def pop_last(self) -> int:
|
||||
"""Rear dequeue"""
|
||||
"""Rear of the queue dequeue"""
|
||||
return self.pop(False)
|
||||
|
||||
def peek_first(self) -> int:
|
||||
"""Access front element"""
|
||||
"""Access front of the queue element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Double-ended queue is empty")
|
||||
return self._front.val
|
||||
|
||||
def peek_last(self) -> int:
|
||||
"""Access rear element"""
|
||||
"""Access rear of the queue element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Double-ended queue is empty")
|
||||
return self._rear.val
|
||||
@@ -122,30 +122,30 @@ if __name__ == "__main__":
|
||||
deque.push_last(3)
|
||||
deque.push_last(2)
|
||||
deque.push_last(5)
|
||||
print("Double-ended queue deque =", deque.to_array())
|
||||
print("double-ended queue deque =", deque.to_array())
|
||||
|
||||
# Access element
|
||||
# Access elements
|
||||
peek_first: int = deque.peek_first()
|
||||
print("Front element peek_first =", peek_first)
|
||||
print("Front of the queue element peek_first =", peek_first)
|
||||
peek_last: int = deque.peek_last()
|
||||
print("Rear element peek_last =", peek_last)
|
||||
print("Rear of the queue element peek_last =", peek_last)
|
||||
|
||||
# Element enqueue
|
||||
# Elements enqueue
|
||||
deque.push_last(4)
|
||||
print("Element 4 rear enqueued, deque =", deque.to_array())
|
||||
print("Element 4 rear enqueue after deque =", deque.to_array())
|
||||
deque.push_first(1)
|
||||
print("Element 1 front enqueued, deque =", deque.to_array())
|
||||
print("Element 1 front enqueue after deque =", deque.to_array())
|
||||
|
||||
# Element dequeue
|
||||
# Elements dequeue
|
||||
pop_last: int = deque.pop_last()
|
||||
print("Rear dequeued element =", pop_last, ", deque after rear dequeue =", deque.to_array())
|
||||
print("Rear dequeued element =", pop_last, ", rear dequeue after deque =", deque.to_array())
|
||||
pop_first: int = deque.pop_first()
|
||||
print("Front dequeued element =", pop_first, ", deque after front dequeue =", deque.to_array())
|
||||
print("Front dequeued element =", pop_first, ", front dequeue after deque =", deque.to_array())
|
||||
|
||||
# Get the length of the double-ended queue
|
||||
size: int = deque.size()
|
||||
print("Double-ended queue length size =", size)
|
||||
print("Length of the double-ended queue size =", size)
|
||||
|
||||
# Determine if the double-ended queue is empty
|
||||
# Check if the double-ended queue is empty
|
||||
is_empty: bool = deque.is_empty()
|
||||
print("Is the double-ended queue empty =", is_empty)
|
||||
|
||||
@@ -12,7 +12,7 @@ from modules import ListNode
|
||||
|
||||
|
||||
class LinkedListQueue:
|
||||
"""Queue class based on linked list"""
|
||||
"""Queue based on linked list implementation"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
@@ -25,18 +25,18 @@ class LinkedListQueue:
|
||||
return self._size
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if the queue is empty"""
|
||||
"""Check if the queue is empty"""
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int):
|
||||
"""Enqueue"""
|
||||
# Add num behind the tail node
|
||||
# Add num after the tail node
|
||||
node = ListNode(num)
|
||||
# If the queue is empty, make the head and tail nodes both point to that node
|
||||
# If the queue is empty, make both front and rear point to the node
|
||||
if self._front is None:
|
||||
self._front = node
|
||||
self._rear = node
|
||||
# If the queue is not empty, add that node behind the tail node
|
||||
# If the queue is not empty, add the node after the tail node
|
||||
else:
|
||||
self._rear.next = node
|
||||
self._rear = node
|
||||
@@ -45,19 +45,19 @@ class LinkedListQueue:
|
||||
def pop(self) -> int:
|
||||
"""Dequeue"""
|
||||
num = self.peek()
|
||||
# Remove head node
|
||||
# Delete head node
|
||||
self._front = self._front.next
|
||||
self._size -= 1
|
||||
return num
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access front element"""
|
||||
"""Access front of the queue element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Queue is empty")
|
||||
return self._front.val
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
"""Convert to a list for printing"""
|
||||
"""Convert to list for printing"""
|
||||
queue = []
|
||||
temp = self._front
|
||||
while temp:
|
||||
@@ -71,27 +71,27 @@ if __name__ == "__main__":
|
||||
# Initialize queue
|
||||
queue = LinkedListQueue()
|
||||
|
||||
# Element enqueue
|
||||
# Elements enqueue
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
print("Queue queue =", queue.to_list())
|
||||
print("queue =", queue.to_list())
|
||||
|
||||
# Access front element
|
||||
# Access front of the queue element
|
||||
peek: int = queue.peek()
|
||||
print("Front element front =", peek)
|
||||
print("Front of the queue element front =", peek)
|
||||
|
||||
# Element dequeue
|
||||
pop_front: int = queue.pop()
|
||||
print("Dequeued element pop =", pop_front)
|
||||
print("Queue after dequeue =", queue.to_list())
|
||||
print("After dequeue queue =", queue.to_list())
|
||||
|
||||
# Get the length of the queue
|
||||
size: int = queue.size()
|
||||
print("Queue length size =", size)
|
||||
print("Length of the queue size =", size)
|
||||
|
||||
# Determine if the queue is empty
|
||||
# Check if the queue is empty
|
||||
is_empty: bool = queue.is_empty()
|
||||
print("Is the queue empty =", is_empty)
|
||||
|
||||
@@ -12,7 +12,7 @@ from modules import ListNode
|
||||
|
||||
|
||||
class LinkedListStack:
|
||||
"""Stack class based on linked list"""
|
||||
"""Stack based on linked list implementation"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
@@ -24,7 +24,7 @@ class LinkedListStack:
|
||||
return self._size
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if the stack is empty"""
|
||||
"""Check if the stack is empty"""
|
||||
return self._size == 0
|
||||
|
||||
def push(self, val: int):
|
||||
@@ -42,13 +42,13 @@ class LinkedListStack:
|
||||
return num
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access stack top element"""
|
||||
"""Access top of the stack element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Stack is empty")
|
||||
return self._peek.val
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
"""Convert to a list for printing"""
|
||||
"""Convert to list for printing"""
|
||||
arr = []
|
||||
node = self._peek
|
||||
while node:
|
||||
@@ -63,27 +63,27 @@ if __name__ == "__main__":
|
||||
# Initialize stack
|
||||
stack = LinkedListStack()
|
||||
|
||||
# Element push
|
||||
# Elements push onto stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
print("Stack stack =", stack.to_list())
|
||||
print("stack =", stack.to_list())
|
||||
|
||||
# Access stack top element
|
||||
# Access top of the stack element
|
||||
peek: int = stack.peek()
|
||||
print("Stack top element peek =", peek)
|
||||
print("Top of the stack element peek =", peek)
|
||||
|
||||
# Element pop
|
||||
# Element pop from stack
|
||||
pop: int = stack.pop()
|
||||
print("Popped element pop =", pop)
|
||||
print("Stack after pop =", stack.to_list())
|
||||
print("After pop stack =", stack.to_list())
|
||||
|
||||
# Get the length of the stack
|
||||
size: int = stack.size()
|
||||
print("Stack length size =", size)
|
||||
print("Length of the stack size =", size)
|
||||
|
||||
# Determine if it's empty
|
||||
# Check if it is empty
|
||||
is_empty: bool = stack.is_empty()
|
||||
print("Is the stack empty =", is_empty)
|
||||
|
||||
@@ -9,31 +9,31 @@ from collections import deque
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize queue
|
||||
# In Python, we generally consider the deque class as a queue
|
||||
# Although queue.Queue() is a pure queue class, it's not very user-friendly
|
||||
# In Python, we generally use the double-ended queue class deque as a queue
|
||||
# Although queue.Queue() is a proper queue class, it is not very convenient to use
|
||||
que: deque[int] = deque()
|
||||
|
||||
# Element enqueue
|
||||
# Elements enqueue
|
||||
que.append(1)
|
||||
que.append(3)
|
||||
que.append(2)
|
||||
que.append(5)
|
||||
que.append(4)
|
||||
print("Queue que =", que)
|
||||
print("queue que =", que)
|
||||
|
||||
# Access front element
|
||||
# Access front of the queue element
|
||||
front: int = que[0]
|
||||
print("Front element front =", front)
|
||||
print("Front of the queue element front =", front)
|
||||
|
||||
# Element dequeue
|
||||
pop: int = que.popleft()
|
||||
print("Dequeued element pop =", pop)
|
||||
print("Queue after dequeue =", que)
|
||||
print("After dequeue que =", que)
|
||||
|
||||
# Get the length of the queue
|
||||
size: int = len(que)
|
||||
print("Queue length size =", size)
|
||||
print("Length of the queue size =", size)
|
||||
|
||||
# Determine if the queue is empty
|
||||
# Check if the queue is empty
|
||||
is_empty: bool = len(que) == 0
|
||||
print("Is the queue empty =", is_empty)
|
||||
|
||||
@@ -7,30 +7,30 @@ Author: Peng Chen (pengchzn@gmail.com)
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize stack
|
||||
# Python does not have a built-in stack class, but you can use a list as a stack
|
||||
# Python does not have a built-in stack class, we can use list as a stack
|
||||
stack: list[int] = []
|
||||
|
||||
# Element push
|
||||
# Elements push onto stack
|
||||
stack.append(1)
|
||||
stack.append(3)
|
||||
stack.append(2)
|
||||
stack.append(5)
|
||||
stack.append(4)
|
||||
print("Stack stack =", stack)
|
||||
print("stack =", stack)
|
||||
|
||||
# Access stack top element
|
||||
# Access top of the stack element
|
||||
peek: int = stack[-1]
|
||||
print("Stack top element peek =", peek)
|
||||
print("Top of the stack element peek =", peek)
|
||||
|
||||
# Element pop
|
||||
# Element pop from stack
|
||||
pop: int = stack.pop()
|
||||
print("Popped element pop =", pop)
|
||||
print("Stack after pop =", stack)
|
||||
print("After pop stack =", stack)
|
||||
|
||||
# Get the length of the stack
|
||||
size: int = len(stack)
|
||||
print("Stack length size =", size)
|
||||
print("Length of the stack size =", size)
|
||||
|
||||
# Determine if it's empty
|
||||
# Check if it is empty
|
||||
is_empty: bool = len(stack) == 0
|
||||
print("Is the stack empty =", is_empty)
|
||||
|
||||
Reference in New Issue
Block a user