mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
2778a6f9c7
* 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
130 lines
3.1 KiB
C++
130 lines
3.1 KiB
C++
/**
|
|
* File: array_queue.cpp
|
|
* Created Time: 2022-11-25
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* Queue based on circular array implementation */
|
|
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 queCapacity; // Queue capacity
|
|
|
|
public:
|
|
ArrayQueue(int capacity) {
|
|
// Initialize array
|
|
nums = new int[capacity];
|
|
queCapacity = capacity;
|
|
front = queSize = 0;
|
|
}
|
|
|
|
~ArrayQueue() {
|
|
delete[] nums;
|
|
}
|
|
|
|
/* Get the capacity of the queue */
|
|
int capacity() {
|
|
return queCapacity;
|
|
}
|
|
|
|
/* Get the length of the queue */
|
|
int size() {
|
|
return queSize;
|
|
}
|
|
|
|
/* Check if the queue is empty */
|
|
bool isEmpty() {
|
|
return size() == 0;
|
|
}
|
|
|
|
/* Enqueue */
|
|
void push(int num) {
|
|
if (queSize == queCapacity) {
|
|
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
|
|
int rear = (front + queSize) % queCapacity;
|
|
// Front pointer moves one position backward
|
|
nums[rear] = num;
|
|
queSize++;
|
|
}
|
|
|
|
/* Dequeue */
|
|
int pop() {
|
|
int num = peek();
|
|
// Move front pointer backward by one position, if it passes the tail, return to array head
|
|
front = (front + 1) % queCapacity;
|
|
queSize--;
|
|
return num;
|
|
}
|
|
|
|
/* Return list for printing */
|
|
int peek() {
|
|
if (isEmpty())
|
|
throw out_of_range("Queue is empty");
|
|
return nums[front];
|
|
}
|
|
|
|
/* Convert array to Vector and return */
|
|
vector<int> toVector() {
|
|
// Elements enqueue
|
|
vector<int> arr(queSize);
|
|
for (int i = 0, j = front; i < queSize; i++, j++) {
|
|
arr[i] = nums[j % queCapacity];
|
|
}
|
|
return arr;
|
|
}
|
|
};
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
/* Access front of the queue element */
|
|
int capacity = 10;
|
|
ArrayQueue *queue = new ArrayQueue(capacity);
|
|
|
|
/* Elements enqueue */
|
|
queue->push(1);
|
|
queue->push(3);
|
|
queue->push(2);
|
|
queue->push(5);
|
|
queue->push(4);
|
|
cout << "Queue queue = ";
|
|
printVector(queue->toVector());
|
|
|
|
/* Return list for printing */
|
|
int peek = queue->peek();
|
|
cout << "Front element peek = " << peek << endl;
|
|
|
|
/* Element dequeue */
|
|
peek = queue->pop();
|
|
cout << "Dequeue element pop = " << peek << ", after dequeue, queue = ";
|
|
printVector(queue->toVector());
|
|
|
|
/* Get the length of the queue */
|
|
int size = queue->size();
|
|
cout << "Queue length size = " << size << endl;
|
|
|
|
/* Check if the queue is empty */
|
|
bool empty = queue->isEmpty();
|
|
cout << "Queue is empty = " << empty << endl;
|
|
|
|
/* Test circular array */
|
|
for (int i = 0; i < 10; i++) {
|
|
queue->push(i);
|
|
queue->pop();
|
|
cout << "After round " << i << " enqueue + dequeue, queue = ";
|
|
printVector(queue->toVector());
|
|
}
|
|
|
|
// Free memory
|
|
delete queue;
|
|
|
|
return 0;
|
|
}
|