Files
hello-algo/en/codes/c/chapter_stack_and_queue/array_stack.c
Yudong Jin 2778a6f9c7 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
2025-12-31 07:44:52 +08:00

104 lines
2.1 KiB
C

/**
* File: array_stack.c
* Created Time: 2023-01-12
* Author: Zero (glj0@outlook.com)
*/
#include "../utils/common.h"
#define MAX_SIZE 5000
/* Stack based on array implementation */
typedef struct {
int *data;
int size;
} ArrayStack;
/* Constructor */
ArrayStack *newArrayStack() {
ArrayStack *stack = malloc(sizeof(ArrayStack));
// Initialize with large capacity to avoid expansion
stack->data = malloc(sizeof(int) * MAX_SIZE);
stack->size = 0;
return stack;
}
/* Destructor */
void delArrayStack(ArrayStack *stack) {
free(stack->data);
free(stack);
}
/* Get the length of the stack */
int size(ArrayStack *stack) {
return stack->size;
}
/* Check if the stack is empty */
bool isEmpty(ArrayStack *stack) {
return stack->size == 0;
}
/* Push */
void push(ArrayStack *stack, int num) {
if (stack->size == MAX_SIZE) {
printf("Stack is full\n");
return;
}
stack->data[stack->size] = num;
stack->size++;
}
/* Return list for printing */
int peek(ArrayStack *stack) {
if (stack->size == 0) {
printf("Stack is empty\n");
return INT_MAX;
}
return stack->data[stack->size - 1];
}
/* Pop */
int pop(ArrayStack *stack) {
int val = peek(stack);
stack->size--;
return val;
}
/* Driver Code */
int main() {
/* Access top of the stack element */
ArrayStack *stack = newArrayStack();
/* Elements push onto stack */
push(stack, 1);
push(stack, 3);
push(stack, 2);
push(stack, 5);
push(stack, 4);
printf("Stack stack = ");
printArray(stack->data, stack->size);
/* Return list for printing */
int val = peek(stack);
printf("Top element top = %d\n", val);
/* Element pop from stack */
val = pop(stack);
printf("Pop element pop = %d, stack after pop = ", val);
printArray(stack->data, stack->size);
/* Get the length of the stack */
int size = stack->size;
printf("Stack size = %d\n", size);
/* Check if empty */
bool empty = isEmpty(stack);
printf("Is stack empty = %s\n", empty ? "true" : "false");
// Free memory
delArrayStack(stack);
return 0;
}