mirror of
http://bgp.hk.skcks.cn:10086/https://github.com/krahets/hello-algo
synced 2026-04-20 21:00:58 +08:00
Some checks failed
C / build (Release, cl, codes/c, cl, windows-latest) (push) Has been cancelled
C / build (Release, cl, en/codes/c, cl, windows-latest) (push) Has been cancelled
C / build (Release, clang, codes/c, clang++, ubuntu-latest) (push) Has been cancelled
C / build (Release, clang, en/codes/c, clang++, ubuntu-latest) (push) Has been cancelled
C / build (Release, gcc, codes/c, g++, ubuntu-latest) (push) Has been cancelled
C / build (Release, gcc, en/codes/c, g++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, cl, codes/cpp, cl, windows-latest) (push) Has been cancelled
C++ / build (Release, cl, en/codes/cpp, cl, windows-latest) (push) Has been cancelled
C++ / build (Release, clang, codes/cpp, clang++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, clang, en/codes/cpp, clang++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, gcc, codes/cpp, g++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, gcc, en/codes/cpp, g++, ubuntu-latest) (push) Has been cancelled
Dart / Dart stable on macos-latest (push) Has been cancelled
Dart / Dart stable on ubuntu-latest (push) Has been cancelled
Dart / Dart stable on windows-latest (push) Has been cancelled
.NET / .NET 8.0.x on macos-latest (push) Has been cancelled
.NET / .NET 8.0.x on ubuntu-latest (push) Has been cancelled
.NET / .NET 8.0.x on windows-latest (push) Has been cancelled
Go / Go 1.19.x on macos-latest (push) Has been cancelled
Go / Go 1.19.x on ubuntu-latest (push) Has been cancelled
Go / Go 1.19.x on windows-latest (push) Has been cancelled
Java / Java 11 sample (push) Has been cancelled
Java / Java 17 sample (push) Has been cancelled
JavaScript / build (codes/javascript, macos-latest) (push) Has been cancelled
JavaScript / build (codes/javascript, ubuntu-latest) (push) Has been cancelled
JavaScript / build (codes/javascript, windows-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, macos-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, ubuntu-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, windows-latest) (push) Has been cancelled
Kotlin / Kotlin on macos-latest (push) Has been cancelled
Kotlin / Kotlin on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on macos-latest (push) Has been cancelled
Python / Python 3.10 on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on windows-latest (push) Has been cancelled
Ruby / Ruby 3.3 on macos-latest (push) Has been cancelled
Ruby / Ruby 3.3 on ubuntu-latest (push) Has been cancelled
Ruby / Ruby 3.3 on windows-latest (push) Has been cancelled
Rust / build (codes/rust, macos-latest) (push) Has been cancelled
Rust / build (codes/rust, ubuntu-latest) (push) Has been cancelled
Rust / build (codes/rust, windows-latest) (push) Has been cancelled
Rust / build (en/codes/rust, macos-latest) (push) Has been cancelled
Rust / build (en/codes/rust, ubuntu-latest) (push) Has been cancelled
Rust / build (en/codes/rust, windows-latest) (push) Has been cancelled
Swift / Swift on macos-14 (push) Has been cancelled
Swift / Swift on ubuntu-22.04 (push) Has been cancelled
TypeScript / build (codes/typescript, macos-latest) (push) Has been cancelled
TypeScript / build (codes/typescript, ubuntu-latest) (push) Has been cancelled
TypeScript / build (codes/typescript, windows-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, macos-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, ubuntu-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, windows-latest) (push) Has been cancelled
* 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
134 lines
3.5 KiB
C
134 lines
3.5 KiB
C
/**
|
|
* File: array_queue.c
|
|
* Created Time: 2023-01-28
|
|
* Author: Zero (glj0@outlook.com)
|
|
*/
|
|
|
|
#include "../utils/common.h"
|
|
|
|
/* Queue based on circular array implementation */
|
|
typedef struct {
|
|
int *nums; // Array for storing queue elements
|
|
int front; // Front pointer, points to the front of the queue element
|
|
int queSize; // Rear pointer, points to rear + 1
|
|
int queCapacity; // Queue capacity
|
|
} ArrayQueue;
|
|
|
|
/* Constructor */
|
|
ArrayQueue *newArrayQueue(int capacity) {
|
|
ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));
|
|
// Initialize array
|
|
queue->queCapacity = capacity;
|
|
queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);
|
|
queue->front = queue->queSize = 0;
|
|
return queue;
|
|
}
|
|
|
|
/* Destructor */
|
|
void delArrayQueue(ArrayQueue *queue) {
|
|
free(queue->nums);
|
|
free(queue);
|
|
}
|
|
|
|
/* Get the capacity of the queue */
|
|
int capacity(ArrayQueue *queue) {
|
|
return queue->queCapacity;
|
|
}
|
|
|
|
/* Get the length of the queue */
|
|
int size(ArrayQueue *queue) {
|
|
return queue->queSize;
|
|
}
|
|
|
|
/* Check if the queue is empty */
|
|
bool empty(ArrayQueue *queue) {
|
|
return queue->queSize == 0;
|
|
}
|
|
|
|
/* Return list for printing */
|
|
int peek(ArrayQueue *queue) {
|
|
assert(size(queue) != 0);
|
|
return queue->nums[queue->front];
|
|
}
|
|
|
|
/* Enqueue */
|
|
void push(ArrayQueue *queue, int num) {
|
|
if (size(queue) == capacity(queue)) {
|
|
printf("Queue is full\r\n");
|
|
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 = (queue->front + queue->queSize) % queue->queCapacity;
|
|
// Front pointer moves one position backward
|
|
queue->nums[rear] = num;
|
|
queue->queSize++;
|
|
}
|
|
|
|
/* Dequeue */
|
|
int pop(ArrayQueue *queue) {
|
|
int num = peek(queue);
|
|
// Move front pointer backward by one position, if it passes the tail, return to array head
|
|
queue->front = (queue->front + 1) % queue->queCapacity;
|
|
queue->queSize--;
|
|
return num;
|
|
}
|
|
|
|
/* Return array for printing */
|
|
int *toArray(ArrayQueue *queue, int *queSize) {
|
|
*queSize = queue->queSize;
|
|
int *res = (int *)calloc(queue->queSize, sizeof(int));
|
|
int j = queue->front;
|
|
for (int i = 0; i < queue->queSize; i++) {
|
|
res[i] = queue->nums[j % queue->queCapacity];
|
|
j++;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
/* Access front of the queue element */
|
|
int capacity = 10;
|
|
int queSize;
|
|
ArrayQueue *queue = newArrayQueue(capacity);
|
|
|
|
/* Elements enqueue */
|
|
push(queue, 1);
|
|
push(queue, 3);
|
|
push(queue, 2);
|
|
push(queue, 5);
|
|
push(queue, 4);
|
|
printf("Queue queue = ");
|
|
printArray(toArray(queue, &queSize), queSize);
|
|
|
|
/* Return list for printing */
|
|
int peekNum = peek(queue);
|
|
printf("Front element peek = %d\r\n", peekNum);
|
|
|
|
/* Element dequeue */
|
|
peekNum = pop(queue);
|
|
printf("Dequeue element pop = %d, queue after dequeue = ", peekNum);
|
|
printArray(toArray(queue, &queSize), queSize);
|
|
|
|
/* Get the length of the queue */
|
|
int queueSize = size(queue);
|
|
printf("Queue size = %d\r\n", queueSize);
|
|
|
|
/* Check if the queue is empty */
|
|
bool isEmpty = empty(queue);
|
|
printf("Is queue empty = %s\r\n", isEmpty ? "true" : "false");
|
|
|
|
/* Test circular array */
|
|
for (int i = 0; i < 10; i++) {
|
|
push(queue, i);
|
|
pop(queue);
|
|
printf("After round %d enqueue + dequeue, queue = ", i);
|
|
printArray(toArray(queue, &queSize), queSize);
|
|
}
|
|
|
|
// Free memory
|
|
delArrayQueue(queue);
|
|
|
|
return 0;
|
|
} |