mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-04 03:00:06 +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,12 +6,12 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Double-ended queue class based on circular array */
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
class ArrayDeque {
|
||||
private:
|
||||
vector<int> nums; // Array used to store elements of the double-ended queue
|
||||
int front; // Front pointer, pointing to the front element
|
||||
int queSize; // Length of the double-ended queue
|
||||
vector<int> nums; // Array for storing double-ended queue elements
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int queSize; // Double-ended queue length
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -30,81 +30,81 @@ class ArrayDeque {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
int index(int i) {
|
||||
// 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 + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
/* Front of the queue enqueue */
|
||||
void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
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
|
||||
// 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
|
||||
front = index(front - 1);
|
||||
// Add num to the front
|
||||
// Add num to front of queue
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
/* Rear of the queue enqueue */
|
||||
void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Calculate rear pointer, pointing to rear index + 1
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
int rear = index(front + queSize);
|
||||
// Add num to the rear
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
/* Rear of the queue dequeue */
|
||||
int popFirst() {
|
||||
int num = peekFirst();
|
||||
// Move front pointer one position backward
|
||||
// Move front pointer backward by one position
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
/* Access rear of the queue element */
|
||||
int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
throw out_of_range("Deque is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
/* Driver Code */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
// Calculate rear element index
|
||||
throw out_of_range("Deque is empty");
|
||||
// Initialize double-ended queue
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
vector<int> toVector() {
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
vector<int> res(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
@@ -115,7 +115,7 @@ class ArrayDeque {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
ArrayDeque *deque = new ArrayDeque(10);
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
@@ -123,34 +123,34 @@ int main() {
|
||||
cout << "Double-ended queue deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Back element peekLast = " << peekLast << endl;
|
||||
cout << "Rear element peekLast = " << peekLast << endl;
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque->pushLast(4);
|
||||
cout << "Element 4 enqueued at the tail, deque = ";
|
||||
cout << "After element 4 enqueues at rear, deque = ";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "Element 1 enqueued at the head, deque = ";
|
||||
cout << "After element 1 enqueues at front, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Deque tail element = " << popLast << ", after dequeuing from the tail";
|
||||
cout << "Rear dequeue element = " << popLast << ", after rear dequeue, deque = ";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Deque front element = " << popFirst << ", after dequeuing from the front";
|
||||
cout << "Front dequeue element = " << popFirst << ", after front dequeue, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque->size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
cout << "Double-ended queue length size = " << size << endl;
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Is the double-ended queue empty = " << boolalpha << isEmpty << endl;
|
||||
cout << "Double-ended queue is empty = " << boolalpha << isEmpty << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue class based on circular array */
|
||||
/* Queue based on circular array implementation */
|
||||
class ArrayQueue {
|
||||
private:
|
||||
int *nums; // Array for storing queue elements
|
||||
int front; // Front pointer, pointing to the front element
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int queSize; // Queue length
|
||||
int queCapacity; // Queue capacity
|
||||
|
||||
public:
|
||||
ArrayQueue(int capacity) {
|
||||
// Initialize an array
|
||||
// Initialize array
|
||||
nums = new int[capacity];
|
||||
queCapacity = capacity;
|
||||
front = queSize = 0;
|
||||
@@ -36,7 +36,7 @@ class ArrayQueue {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -47,10 +47,10 @@ class ArrayQueue {
|
||||
cout << "Queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// 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
|
||||
// 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
|
||||
int rear = (front + queSize) % queCapacity;
|
||||
// Add num to the rear
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
@@ -58,13 +58,13 @@ class ArrayQueue {
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Move front pointer one position backward, returning to the head of the array if it exceeds the tail
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
front = (front + 1) % queCapacity;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
@@ -73,7 +73,7 @@ class ArrayQueue {
|
||||
|
||||
/* Convert array to Vector and return */
|
||||
vector<int> toVector() {
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
vector<int> arr(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
arr[i] = nums[j % queCapacity];
|
||||
@@ -84,11 +84,11 @@ class ArrayQueue {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize queue */
|
||||
/* Access front of the queue element */
|
||||
int capacity = 10;
|
||||
ArrayQueue *queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
@@ -97,28 +97,28 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
peek = queue->pop();
|
||||
cout << "Element dequeued = " << peek << ", after dequeuing";
|
||||
cout << "Dequeue element pop = " << peek << ", after dequeue, queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue->size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
cout << "Queue length size = " << size << endl;
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
|
||||
/* Test circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue->push(i);
|
||||
queue->pop();
|
||||
cout << "After the " << i << "th round of enqueueing + dequeuing, queue = ";
|
||||
cout << "After round " << i << " enqueue + dequeue, queue = ";
|
||||
printVector(queue->toVector());
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Stack class based on array */
|
||||
/* Stack based on array implementation */
|
||||
class ArrayStack {
|
||||
private:
|
||||
vector<int> stack;
|
||||
@@ -17,7 +17,7 @@ class ArrayStack {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* Determine if the stack is empty */
|
||||
/* Check if the stack is empty */
|
||||
bool isEmpty() {
|
||||
return stack.size() == 0;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class ArrayStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
@@ -49,10 +49,10 @@ class ArrayStack {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize stack */
|
||||
/* Access top of the stack element */
|
||||
ArrayStack *stack = new ArrayStack();
|
||||
|
||||
/* Element push */
|
||||
/* Elements push onto stack */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
@@ -61,22 +61,22 @@ int main() {
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int top = stack->top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
cout << "Stack top element top = " << top << endl;
|
||||
|
||||
/* Element pop */
|
||||
/* Element pop from stack */
|
||||
top = stack->pop();
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
cout << "Pop element pop = " << top << ", after pop, stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack->size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
cout << "Stack length size = " << size << endl;
|
||||
|
||||
/* Determine if it's empty */
|
||||
/* Check if empty */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
cout << "Stack is empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete stack;
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
deque<int> deque;
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque.push_back(2);
|
||||
deque.push_back(5);
|
||||
deque.push_back(4);
|
||||
@@ -20,27 +20,27 @@ int main() {
|
||||
cout << "Double-ended queue deque = ";
|
||||
printDeque(deque);
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int front = deque.front();
|
||||
cout << "Front element of the queue front = " << front << endl;
|
||||
cout << "Front element front = " << front << endl;
|
||||
int back = deque.back();
|
||||
cout << "Back element of the queue back = " << back << endl;
|
||||
cout << "Back element back = " << back << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
deque.pop_front();
|
||||
cout << "Front element dequeued = " << front << ", after dequeuing from the front";
|
||||
cout << "Front dequeue element popFront = " << front << ", after front dequeue, deque = ";
|
||||
printDeque(deque);
|
||||
deque.pop_back();
|
||||
cout << "Back element dequeued = " << back << ", after dequeuing from the back";
|
||||
cout << "Rear dequeue element popLast = " << back << ", after rear dequeue, deque = ";
|
||||
printDeque(deque);
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
cout << "Double-ended queue length size = " << size << endl;
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool empty = deque.empty();
|
||||
cout << "Is the double-ended queue empty = " << empty << endl;
|
||||
cout << "Double-ended queue is empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,19 +6,19 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Double-linked list node */
|
||||
/* Doubly linked list node */
|
||||
struct DoublyListNode {
|
||||
int val; // Node value
|
||||
DoublyListNode *next; // Pointer to successor node
|
||||
DoublyListNode *prev; // Pointer to predecessor node
|
||||
DoublyListNode *next; // Successor node pointer
|
||||
DoublyListNode *prev; // Predecessor node pointer
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* Double-ended queue class based on double-linked list */
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
class LinkedListDeque {
|
||||
private:
|
||||
DoublyListNode *front, *rear; // Front node front, back node rear
|
||||
DoublyListNode *front, *rear; // Head node front, tail node rear
|
||||
int queSize = 0; // Length of the double-ended queue
|
||||
|
||||
public:
|
||||
@@ -28,7 +28,7 @@ class LinkedListDeque {
|
||||
|
||||
/* Destructor */
|
||||
~LinkedListDeque() {
|
||||
// Traverse the linked list, remove nodes, free memory
|
||||
// Traverse linked list to delete nodes and free memory
|
||||
DoublyListNode *pre, *cur = front;
|
||||
while (cur != nullptr) {
|
||||
pre = cur;
|
||||
@@ -42,7 +42,7 @@ class LinkedListDeque {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -50,18 +50,18 @@ class LinkedListDeque {
|
||||
/* Enqueue operation */
|
||||
void push(int num, bool isFront) {
|
||||
DoublyListNode *node = new DoublyListNode(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 (isEmpty())
|
||||
front = rear = node;
|
||||
// Front enqueue operation
|
||||
// Front of the queue enqueue operation
|
||||
else if (isFront) {
|
||||
// Add node to the head of the list
|
||||
// Add node to the head of the linked list
|
||||
front->prev = node;
|
||||
node->next = front;
|
||||
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
|
||||
rear->next = node;
|
||||
node->prev = rear;
|
||||
rear = node; // Update tail node
|
||||
@@ -69,12 +69,12 @@ class LinkedListDeque {
|
||||
queSize++; // Update queue length
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
/* Front of the queue enqueue */
|
||||
void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
/* Rear of the queue enqueue */
|
||||
void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
@@ -84,10 +84,10 @@ class LinkedListDeque {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
int val;
|
||||
// Front dequeue operation
|
||||
// Temporarily store head node value
|
||||
if (isFront) {
|
||||
val = front->val; // Temporarily store the head node value
|
||||
// Remove head node
|
||||
val = front->val; // Delete head node
|
||||
// Delete head node
|
||||
DoublyListNode *fNext = front->next;
|
||||
if (fNext != nullptr) {
|
||||
fNext->prev = nullptr;
|
||||
@@ -95,10 +95,10 @@ class LinkedListDeque {
|
||||
}
|
||||
delete front;
|
||||
front = fNext; // Update head node
|
||||
// Rear dequeue operation
|
||||
// Temporarily store tail node value
|
||||
} else {
|
||||
val = rear->val; // Temporarily store the tail node value
|
||||
// Remove tail node
|
||||
val = rear->val; // Delete tail node
|
||||
// Update tail node
|
||||
DoublyListNode *rPrev = rear->prev;
|
||||
if (rPrev != nullptr) {
|
||||
rPrev->next = nullptr;
|
||||
@@ -111,27 +111,27 @@ class LinkedListDeque {
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
/* Rear of the queue dequeue */
|
||||
int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
/* Access rear of the queue element */
|
||||
int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
throw out_of_range("Deque is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
/* Driver Code */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
throw out_of_range("Deque is empty");
|
||||
return rear->val;
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ class LinkedListDeque {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
LinkedListDeque *deque = new LinkedListDeque();
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
@@ -157,35 +157,35 @@ int main() {
|
||||
cout << "Double-ended queue deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Back element peekLast = " << peekLast << endl;
|
||||
cout << "Rear element peekLast = " << peekLast << endl;
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque->pushLast(4);
|
||||
cout << "Element 4 rear enqueued, deque =";
|
||||
cout << "After element 4 enqueues at back, deque =";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "Element 1 enqueued at the head, deque = ";
|
||||
cout << "After element 1 enqueues at front, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Deque tail element = " << popLast << ", after dequeuing from the tail";
|
||||
cout << "Rear dequeue element = " << popLast << ", after rear dequeue, deque = ";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Deque front element = " << popFirst << ", after dequeuing from the front";
|
||||
cout << "Front dequeue element = " << popFirst << ", after front dequeue, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque->size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
cout << "Double-ended queue length size = " << size << endl;
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Is the double-ended queue empty = " << boolalpha << isEmpty << endl;
|
||||
cout << "Double-ended queue is empty = " << boolalpha << isEmpty << endl;
|
||||
|
||||
// Free memory
|
||||
delete deque;
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue class based on linked list */
|
||||
/* Queue based on linked list implementation */
|
||||
class LinkedListQueue {
|
||||
private:
|
||||
ListNode *front, *rear; // Front node front, back node rear
|
||||
ListNode *front, *rear; // Head node front, tail node rear
|
||||
int queSize;
|
||||
|
||||
public:
|
||||
@@ -20,7 +20,7 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
~LinkedListQueue() {
|
||||
// Traverse the linked list, remove nodes, free memory
|
||||
// Traverse linked list to delete nodes and free memory
|
||||
freeMemoryLinkedList(front);
|
||||
}
|
||||
|
||||
@@ -29,21 +29,21 @@ class LinkedListQueue {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
void push(int num) {
|
||||
// Add num behind the tail node
|
||||
// Add num after the tail node
|
||||
ListNode *node = new 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 (front == nullptr) {
|
||||
front = node;
|
||||
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 {
|
||||
rear->next = node;
|
||||
rear = node;
|
||||
@@ -54,7 +54,7 @@ class LinkedListQueue {
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Remove head node
|
||||
// Delete head node
|
||||
ListNode *tmp = front;
|
||||
front = front->next;
|
||||
// Free memory
|
||||
@@ -63,14 +63,14 @@ class LinkedListQueue {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek() {
|
||||
if (size() == 0)
|
||||
throw out_of_range("Queue is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* Convert the linked list to Vector and return */
|
||||
/* Convert linked list to Vector and return */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = front;
|
||||
vector<int> res(size());
|
||||
@@ -84,10 +84,10 @@ class LinkedListQueue {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize queue */
|
||||
/* Access front of the queue element */
|
||||
LinkedListQueue *queue = new LinkedListQueue();
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
@@ -96,22 +96,22 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
peek = queue->pop();
|
||||
cout << "Element dequeued = " << peek << ", after dequeuing";
|
||||
cout << "Dequeue element pop = " << peek << ", after dequeue, queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue->size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
cout << "Queue length size = " << size << endl;
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete queue;
|
||||
|
||||
@@ -6,11 +6,11 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Stack class based on linked list */
|
||||
/* Stack based on linked list implementation */
|
||||
class LinkedListStack {
|
||||
private:
|
||||
ListNode *stackTop; // Use the head node as the top of the stack
|
||||
int stkSize; // Length of the stack
|
||||
ListNode *stackTop; // Use head node as stack top
|
||||
int stkSize; // Stack length
|
||||
|
||||
public:
|
||||
LinkedListStack() {
|
||||
@@ -19,7 +19,7 @@ class LinkedListStack {
|
||||
}
|
||||
|
||||
~LinkedListStack() {
|
||||
// Traverse the linked list, remove nodes, free memory
|
||||
// Traverse linked list to delete nodes and free memory
|
||||
freeMemoryLinkedList(stackTop);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class LinkedListStack {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* Determine if the stack is empty */
|
||||
/* Check if the stack is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -52,14 +52,14 @@ class LinkedListStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stackTop->val;
|
||||
}
|
||||
|
||||
/* Convert the List to Array and return */
|
||||
/* Convert List to Array and return */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = stackTop;
|
||||
vector<int> res(size());
|
||||
@@ -73,10 +73,10 @@ class LinkedListStack {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize stack */
|
||||
/* Access top of the stack element */
|
||||
LinkedListStack *stack = new LinkedListStack();
|
||||
|
||||
/* Element push */
|
||||
/* Elements push onto stack */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
@@ -85,22 +85,22 @@ int main() {
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int top = stack->top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
cout << "Stack top element top = " << top << endl;
|
||||
|
||||
/* Element pop */
|
||||
/* Element pop from stack */
|
||||
top = stack->pop();
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
cout << "Pop element pop = " << top << ", after pop, stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack->size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
cout << "Stack length size = " << size << endl;
|
||||
|
||||
/* Determine if it's empty */
|
||||
/* Check if empty */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
cout << "Stack is empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete stack;
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize queue */
|
||||
/* Access front of the queue element */
|
||||
queue<int> queue;
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
@@ -20,22 +20,22 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printQueue(queue);
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int front = queue.front();
|
||||
cout << "Front element of the queue front = " << front << endl;
|
||||
cout << "Front element front = " << front << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
queue.pop();
|
||||
cout << "Element dequeued = " << front << ", after dequeuing";
|
||||
cout << "Dequeue element front = " << front << ", after dequeue, queue = ";
|
||||
printQueue(queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
cout << "Queue length size = " << size << endl;
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
bool empty = queue.empty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize stack */
|
||||
/* Access top of the stack element */
|
||||
stack<int> stack;
|
||||
|
||||
/* Element push */
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
@@ -20,22 +20,22 @@ int main() {
|
||||
cout << "Stack stack = ";
|
||||
printStack(stack);
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int top = stack.top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
cout << "Stack top element top = " << top << endl;
|
||||
|
||||
/* Element pop */
|
||||
/* Element pop from stack */
|
||||
stack.pop(); // No return value
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
cout << "Pop element pop = " << top << ", after pop, stack = ";
|
||||
printStack(stack);
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
cout << "Stack length size = " << size << endl;
|
||||
|
||||
/* Determine if it's empty */
|
||||
/* Check if empty */
|
||||
bool empty = stack.empty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
cout << "Stack is empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user