This commit is contained in:
krahets
2024-05-06 14:40:36 +08:00
parent 7e7eb6047a
commit 5c7d2c7f17
54 changed files with 3456 additions and 215 deletions

View File

@@ -411,7 +411,81 @@ Below is the code for implementing a queue using a linked list:
=== "C++"
```cpp title="linkedlist_queue.cpp"
[class]{LinkedListQueue}-[func]{}
/* Queue class based on linked list */
class LinkedListQueue {
private:
ListNode *front, *rear; // Front node front, back node rear
int queSize;
public:
LinkedListQueue() {
front = nullptr;
rear = nullptr;
queSize = 0;
}
~LinkedListQueue() {
// Traverse the linked list, remove nodes, free memory
freeMemoryLinkedList(front);
}
/* Get the length of the queue */
int size() {
return queSize;
}
/* Determine if the queue is empty */
bool isEmpty() {
return queSize == 0;
}
/* Enqueue */
void push(int num) {
// Add num behind 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 (front == nullptr) {
front = node;
rear = node;
}
// If the queue is not empty, add that node behind the tail node
else {
rear->next = node;
rear = node;
}
queSize++;
}
/* Dequeue */
int pop() {
int num = peek();
// Remove head node
ListNode *tmp = front;
front = front->next;
// Free memory
delete tmp;
queSize--;
return num;
}
/* Access front element */
int peek() {
if (size() == 0)
throw out_of_range("Queue is empty");
return front->val;
}
/* Convert the linked list to Vector and return */
vector<int> toVector() {
ListNode *node = front;
vector<int> res(size());
for (int i = 0; i < res.size(); i++) {
res[i] = node->val;
node = node->next;
}
return res;
}
};
```
=== "Java"
@@ -638,7 +712,81 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar
=== "C++"
```cpp title="array_queue.cpp"
[class]{ArrayQueue}-[func]{}
/* Queue class based on circular array */
class ArrayQueue {
private:
int *nums; // Array for storing queue elements
int front; // Front pointer, pointing to the front element
int queSize; // Queue length
int queCapacity; // Queue capacity
public:
ArrayQueue(int capacity) {
// Initialize an 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;
}
/* Determine if the queue is empty */
bool isEmpty() {
return size() == 0;
}
/* Enqueue */
void push(int num) {
if (queSize == queCapacity) {
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
int rear = (front + queSize) % queCapacity;
// Add num to the rear
nums[rear] = num;
queSize++;
}
/* Dequeue */
int pop() {
int num = peek();
// Move front pointer one position backward, returning to the head of the array if it exceeds the tail
front = (front + 1) % queCapacity;
queSize--;
return num;
}
/* Access front element */
int peek() {
if (isEmpty())
throw out_of_range("Queue is empty");
return nums[front];
}
/* Convert array to Vector and return */
vector<int> toVector() {
// Only convert elements within valid length range
vector<int> arr(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) {
arr[i] = nums[j % queCapacity];
}
return arr;
}
};
```
=== "Java"