mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +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,12 +6,12 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Deque implemented with a circular array */
|
||||
class ArrayDeque {
|
||||
private:
|
||||
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
|
||||
vector<int> nums; // Array for storing deque elements
|
||||
int front; // Front index, identifying the first element
|
||||
int queSize; // Deque size
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -20,91 +20,91 @@ class ArrayDeque {
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
/* Get the deque capacity */
|
||||
int capacity() {
|
||||
return nums.size();
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
/* Calculate an index in the circular array */
|
||||
int index(int i) {
|
||||
// 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 */
|
||||
void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
cout << "Deque is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
// Add num to the front of the queue
|
||||
// Move the front index one position to the left
|
||||
// Use modulo to wrap front to the end when it passes the beginning of the array
|
||||
front = index(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 */
|
||||
void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
cout << "Deque is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Calculate the rear index, one position past the last element
|
||||
int 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 */
|
||||
int popFirst() {
|
||||
int num = peekFirst();
|
||||
// Move front pointer backward by one position
|
||||
// Advance the front index by one position
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Deque is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Deque is empty");
|
||||
// Initialize double-ended queue
|
||||
// Calculate the index of the last element
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return a vector for printing */
|
||||
vector<int> toVector() {
|
||||
// Elements enqueue
|
||||
// Copy only the elements in the occupied range
|
||||
vector<int> res(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
@@ -113,44 +113,44 @@ class ArrayDeque {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
ArrayDeque *deque = new ArrayDeque(10);
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "Double-ended queue deque = ";
|
||||
cout << "Deque deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Rear element peekLast = " << peekLast << endl;
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque->pushLast(4);
|
||||
cout << "After element 4 enqueues at rear, deque = ";
|
||||
cout << "After enqueuing element 4 at the rear, deque = ";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "After element 1 enqueues at front, deque = ";
|
||||
cout << "After enqueuing element 1 at the front, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Rear dequeue element = " << popLast << ", after rear dequeue, deque = ";
|
||||
cout << "Element dequeued from the rear = " << popLast << ", after dequeuing from the rear, deque = ";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Front dequeue element = " << popFirst << ", after front dequeue, deque = ";
|
||||
cout << "Element dequeued from the front = " << popFirst << ", after dequeuing from the front, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque->size();
|
||||
cout << "Double-ended queue length size = " << size << endl;
|
||||
cout << "Deque size = " << size << endl;
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Double-ended queue is empty = " << boolalpha << isEmpty << endl;
|
||||
cout << "Is the deque empty = " << boolalpha << isEmpty << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
class ArrayQueue {
|
||||
private:
|
||||
int *nums; // Array for storing queue elements
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int queSize; // Queue length
|
||||
int *nums; // Array that stores the queue elements
|
||||
int front; // Front index, identifying the first element
|
||||
int queSize; // Queue size
|
||||
int queCapacity; // Queue capacity
|
||||
|
||||
public:
|
||||
ArrayQueue(int capacity) {
|
||||
// Initialize array
|
||||
// Initialize the array
|
||||
nums = new int[capacity];
|
||||
queCapacity = capacity;
|
||||
front = queSize = 0;
|
||||
@@ -26,17 +26,17 @@ class ArrayQueue {
|
||||
delete[] nums;
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
/* Get the queue capacity */
|
||||
int capacity() {
|
||||
return queCapacity;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -47,10 +47,10 @@ class ArrayQueue {
|
||||
cout << "Queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Add num to the rear of the queue
|
||||
// Calculate the rear index, one position past the last element
|
||||
// Use modulo to wrap rear to the beginning when it passes the end of the array
|
||||
int rear = (front + queSize) % queCapacity;
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
@@ -58,22 +58,22 @@ class ArrayQueue {
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front index by one position, wrapping to the beginning if it passes the end
|
||||
front = (front + 1) % queCapacity;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Convert array to Vector and return */
|
||||
/* Convert the array to a vector and return it */
|
||||
vector<int> toVector() {
|
||||
// Elements enqueue
|
||||
// Copy only the elements in the occupied range
|
||||
vector<int> arr(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
arr[i] = nums[j % queCapacity];
|
||||
@@ -82,13 +82,13 @@ class ArrayQueue {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
int capacity = 10;
|
||||
ArrayQueue *queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
@@ -97,28 +97,28 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
peek = queue->pop();
|
||||
cout << "Dequeue element pop = " << peek << ", after dequeue, queue = ";
|
||||
cout << "Dequeued element pop = " << peek << "; after dequeueing, queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue->size();
|
||||
cout << "Queue length size = " << size << endl;
|
||||
cout << "Queue size = " << size << endl;
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue->push(i);
|
||||
queue->pop();
|
||||
cout << "After round " << i << " enqueue + dequeue, queue = ";
|
||||
cout << "Round " << i << " : after enqueue + dequeue, queue = ";
|
||||
printVector(queue->toVector());
|
||||
}
|
||||
|
||||
|
||||
@@ -6,18 +6,18 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
class ArrayStack {
|
||||
private:
|
||||
vector<int> stack;
|
||||
|
||||
public:
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size() {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
bool isEmpty() {
|
||||
return stack.size() == 0;
|
||||
}
|
||||
@@ -34,25 +34,25 @@ class ArrayStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* Return Vector */
|
||||
/* Return a vector */
|
||||
vector<int> toVector() {
|
||||
return stack;
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
ArrayStack *stack = new ArrayStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
@@ -61,22 +61,22 @@ int main() {
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int top = stack->top();
|
||||
cout << "Stack top element top = " << top << endl;
|
||||
cout << "Top element top = " << top << endl;
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
top = stack->pop();
|
||||
cout << "Pop element pop = " << top << ", after pop, stack = ";
|
||||
cout << "Popped element pop = " << top << "; after popping, stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack->size();
|
||||
cout << "Stack length size = " << size << endl;
|
||||
cout << "Stack size = " << size << endl;
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Stack is empty = " << empty << endl;
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete stack;
|
||||
|
||||
@@ -6,41 +6,41 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
deque<int> deque;
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.push_back(2);
|
||||
deque.push_back(5);
|
||||
deque.push_back(4);
|
||||
deque.push_front(3);
|
||||
deque.push_front(1);
|
||||
cout << "Double-ended queue deque = ";
|
||||
cout << "Deque deque = ";
|
||||
printDeque(deque);
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int front = deque.front();
|
||||
cout << "Front element front = " << front << endl;
|
||||
int back = deque.back();
|
||||
cout << "Back element back = " << back << endl;
|
||||
cout << "Rear element back = " << back << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
deque.pop_front();
|
||||
cout << "Front dequeue element popFront = " << front << ", after front dequeue, deque = ";
|
||||
cout << "Element removed from the front, popFront = " << front << ", after dequeuing from the front, deque = ";
|
||||
printDeque(deque);
|
||||
deque.pop_back();
|
||||
cout << "Rear dequeue element popLast = " << back << ", after rear dequeue, deque = ";
|
||||
cout << "Element removed from the rear, popLast = " << back << ", after dequeuing from the rear, deque = ";
|
||||
printDeque(deque);
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
cout << "Double-ended queue length size = " << size << endl;
|
||||
cout << "Deque size = " << size << endl;
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool empty = deque.empty();
|
||||
cout << "Double-ended queue is empty = " << empty << endl;
|
||||
cout << "Is the deque empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -9,17 +9,17 @@
|
||||
/* Doubly linked list node */
|
||||
struct DoublyListNode {
|
||||
int val; // Node value
|
||||
DoublyListNode *next; // Successor node pointer
|
||||
DoublyListNode *prev; // Predecessor node pointer
|
||||
DoublyListNode *next; // Pointer to the next node
|
||||
DoublyListNode *prev; // Pointer to the previous node
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
/* Deque implemented with a doubly linked list */
|
||||
class LinkedListDeque {
|
||||
private:
|
||||
DoublyListNode *front, *rear; // Head node front, tail node rear
|
||||
int queSize = 0; // Length of the double-ended queue
|
||||
DoublyListNode *front, *rear; // Head node front and tail node rear
|
||||
int queSize = 0; // Deque size
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -28,7 +28,7 @@ class LinkedListDeque {
|
||||
|
||||
/* Destructor */
|
||||
~LinkedListDeque() {
|
||||
// Traverse linked list to delete nodes and free memory
|
||||
// Traverse the linked list and delete each node to free its memory
|
||||
DoublyListNode *pre, *cur = front;
|
||||
while (cur != nullptr) {
|
||||
pre = cur;
|
||||
@@ -37,12 +37,12 @@ class LinkedListDeque {
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -53,28 +53,28 @@ class LinkedListDeque {
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// Front of the queue enqueue operation
|
||||
// Enqueue at the front
|
||||
else if (isFront) {
|
||||
// Add node to the head of the linked list
|
||||
front->prev = node;
|
||||
node->next = front;
|
||||
front = node; // Update head node
|
||||
// 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 deque size
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
@@ -82,63 +82,63 @@ class LinkedListDeque {
|
||||
/* Dequeue operation */
|
||||
int pop(bool isFront) {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
throw out_of_range("Deque is empty");
|
||||
int val;
|
||||
// When the deque has one element, front and rear point to the same node
|
||||
// When the deque size is 1, the head and tail point to the same node
|
||||
if (queSize == 1) {
|
||||
val = front->val;
|
||||
delete front; // Free the shared node once
|
||||
delete front; // Both point to the same node, so free it only once
|
||||
front = nullptr;
|
||||
rear = nullptr;
|
||||
}
|
||||
// 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
|
||||
DoublyListNode *fNext = front->next;
|
||||
fNext->prev = nullptr;
|
||||
front->next = nullptr;
|
||||
delete front;
|
||||
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
|
||||
DoublyListNode *rPrev = rear->prev;
|
||||
rPrev->next = nullptr;
|
||||
rear->prev = nullptr;
|
||||
delete rear;
|
||||
rear = rPrev; // Update tail node
|
||||
rear = rPrev; // Update the tail node
|
||||
}
|
||||
queSize--; // Update queue length
|
||||
queSize--; // Update the deque size
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Deque is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Deque is empty");
|
||||
return rear->val;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return a vector for printing */
|
||||
vector<int> toVector() {
|
||||
DoublyListNode *node = front;
|
||||
vector<int> res(size());
|
||||
@@ -150,45 +150,45 @@ class LinkedListDeque {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
LinkedListDeque *deque = new LinkedListDeque();
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "Double-ended queue deque = ";
|
||||
cout << "Deque deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Rear element peekLast = " << peekLast << endl;
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque->pushLast(4);
|
||||
cout << "After element 4 enqueues at back, deque =";
|
||||
cout << "After enqueuing element 4 at the rear, deque =";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "After element 1 enqueues at front, deque = ";
|
||||
cout << "After enqueuing element 1 at the front, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Rear dequeue element = " << popLast << ", after rear dequeue, deque = ";
|
||||
cout << "Element dequeued from the rear = " << popLast << ", after dequeuing from the rear, deque = ";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Front dequeue element = " << popFirst << ", after front dequeue, deque = ";
|
||||
cout << "Element dequeued from the front = " << popFirst << ", after dequeuing from the front, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque->size();
|
||||
cout << "Double-ended queue length size = " << size << endl;
|
||||
cout << "Deque size = " << size << endl;
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Double-ended queue is empty = " << boolalpha << isEmpty << endl;
|
||||
cout << "Is the deque empty = " << boolalpha << isEmpty << endl;
|
||||
|
||||
// Free memory
|
||||
delete deque;
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
/* Queue implemented with a linked list */
|
||||
class LinkedListQueue {
|
||||
private:
|
||||
ListNode *front, *rear; // Head node front, tail node rear
|
||||
ListNode *front, *rear; // Head node front and tail node rear
|
||||
int queSize;
|
||||
|
||||
public:
|
||||
@@ -20,16 +20,16 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
~LinkedListQueue() {
|
||||
// Traverse linked list to delete nodes and free memory
|
||||
// Traverse the linked list and delete each node to free its memory
|
||||
freeMemoryLinkedList(front);
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
@@ -38,12 +38,12 @@ class LinkedListQueue {
|
||||
void push(int num) {
|
||||
// Add num after the tail node
|
||||
ListNode *node = new 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 == nullptr) {
|
||||
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;
|
||||
@@ -54,7 +54,7 @@ class LinkedListQueue {
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Delete head node
|
||||
// Remove the head node
|
||||
ListNode *tmp = front;
|
||||
front = front->next;
|
||||
// Free memory
|
||||
@@ -63,14 +63,14 @@ class LinkedListQueue {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek() {
|
||||
if (size() == 0)
|
||||
throw out_of_range("Queue is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Vector and return */
|
||||
/* Convert the linked list to a vector and return it */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = front;
|
||||
vector<int> res(size());
|
||||
@@ -82,12 +82,12 @@ class LinkedListQueue {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
LinkedListQueue *queue = new LinkedListQueue();
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
@@ -96,22 +96,22 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
peek = queue->pop();
|
||||
cout << "Dequeue element pop = " << peek << ", after dequeue, queue = ";
|
||||
cout << "Dequeued element pop = " << peek << "; after dequeueing, queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue->size();
|
||||
cout << "Queue length size = " << size << endl;
|
||||
cout << "Queue size = " << size << endl;
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete queue;
|
||||
|
||||
@@ -6,11 +6,11 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
/* Stack implemented with a linked list */
|
||||
class LinkedListStack {
|
||||
private:
|
||||
ListNode *stackTop; // Use head node as stack top
|
||||
int stkSize; // Stack length
|
||||
ListNode *stackTop; // Use the head node as the stack top
|
||||
int stkSize; // Stack size
|
||||
|
||||
public:
|
||||
LinkedListStack() {
|
||||
@@ -19,16 +19,16 @@ class LinkedListStack {
|
||||
}
|
||||
|
||||
~LinkedListStack() {
|
||||
// Traverse linked list to delete nodes and free memory
|
||||
// Traverse the linked list and delete each node to free its memory
|
||||
freeMemoryLinkedList(stackTop);
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -52,14 +52,14 @@ class LinkedListStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stackTop->val;
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the linked list to a vector and return it */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = stackTop;
|
||||
vector<int> res(size());
|
||||
@@ -71,12 +71,12 @@ class LinkedListStack {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
LinkedListStack *stack = new LinkedListStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
@@ -85,22 +85,22 @@ int main() {
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int top = stack->top();
|
||||
cout << "Stack top element top = " << top << endl;
|
||||
cout << "Top element top = " << top << endl;
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
top = stack->pop();
|
||||
cout << "Pop element pop = " << top << ", after pop, stack = ";
|
||||
cout << "Popped element pop = " << top << "; after popping, stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack->size();
|
||||
cout << "Stack length size = " << size << endl;
|
||||
cout << "Stack size = " << size << endl;
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Stack is empty = " << empty << endl;
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete stack;
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
queue<int> queue;
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
@@ -20,22 +20,22 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printQueue(queue);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int front = queue.front();
|
||||
cout << "Front element front = " << front << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
queue.pop();
|
||||
cout << "Dequeue element front = " << front << ", after dequeue, queue = ";
|
||||
cout << "Dequeued element front = " << front << "; after dequeueing, queue = ";
|
||||
printQueue(queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.size();
|
||||
cout << "Queue length size = " << size << endl;
|
||||
cout << "Queue size = " << size << endl;
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty = queue.empty();
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
stack<int> stack;
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
@@ -20,22 +20,22 @@ int main() {
|
||||
cout << "Stack stack = ";
|
||||
printStack(stack);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int top = stack.top();
|
||||
cout << "Stack top element top = " << top << endl;
|
||||
cout << "Top element top = " << top << endl;
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
stack.pop(); // No return value
|
||||
cout << "Pop element pop = " << top << ", after pop, stack = ";
|
||||
cout << "Popped element pop = " << top << "; after popping, stack = ";
|
||||
printStack(stack);
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.size();
|
||||
cout << "Stack length size = " << size << endl;
|
||||
cout << "Stack size = " << size << endl;
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool empty = stack.empty();
|
||||
cout << "Stack is empty = " << empty << endl;
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user