Skip to content

Instantly share code, notes, and snippets.

@EssamWisam
Last active April 8, 2025 21:27
Show Gist options
  • Save EssamWisam/e8122243e5433db96240a9f083efeddb to your computer and use it in GitHub Desktop.
Save EssamWisam/e8122243e5433db96240a9f083efeddb to your computer and use it in GitHub Desktop.
Dynamic Array
#include <stdio.h>
#include <stdlib.h>
// Define a structure for DynamicArray
typedef struct {
int *data; // Pointer to the array of elements
int size; // Number of elements currently in the array
int capacity; // Total capacity (allocated memory) of the array
} DynamicArray;
// Function to create a new dynamic array with an initial capacity
DynamicArray* create_array(int initial_capacity) {
// Allocate memory for the DynamicArray structure
DynamicArray* arr = malloc(sizeof(DynamicArray));
// Allocate memory for the data array with the specified capacity
arr->data = malloc(initial_capacity * sizeof(int));
// Initialize size to 0 and set the capacity to the provided value
arr->size = 0;
arr->capacity = initial_capacity;
return arr;
}
// Function to resize the array to a new capacity
void resize(DynamicArray* arr, int new_capacity) {
// Reallocate memory for the data array with the new capacity
arr->data = realloc(arr->data, new_capacity * sizeof(int));
// Update the capacity field
arr->capacity = new_capacity;
}
// Function to add a new element to the dynamic array
void push(DynamicArray* arr, int value) {
// Check if the array is full (size == capacity)
if (arr->size == arr->capacity) {
// Double the capacity of the array if full
resize(arr, arr->capacity * 2);
}
// Add the new value to the array and increment the size
arr->data[arr->size++] = value;
}
// Function to remove the last element from the dynamic array
void pop(DynamicArray* arr) {
// Ensure the array is not empty
if (arr->size == 0) return;
// Decrement the size of the array (removing the last element)
arr->size--;
// Check if the array size is less than or equal to 30% of its capacity
// and if the current capacity is more than 1 (to avoid shrinking to zero)
if (arr->size <= arr->capacity * 0.3 && arr->capacity > 1) {
// Halve the capacity of the array if the size is small enough
int new_capacity = arr->capacity / 2;
// Ensure the new capacity is at least 1
if (new_capacity < 1) new_capacity = 1;
// Resize the array to the new, smaller capacity
resize(arr, new_capacity);
}
}
// Function to free the memory allocated for the dynamic array
void free_array(DynamicArray* arr) {
// Free the memory allocated for the data array
free(arr->data);
// Free the memory allocated for the DynamicArray structure itself
free(arr);
}
// Function to print the elements in the dynamic array (for testing)
void print_array(DynamicArray* arr) {
printf("Array (size: %d, capacity: %d): ", arr->size, arr->capacity);
// Print each element in the array
for (int i = 0; i < arr->size; i++) {
printf("%d ", arr->data[i]);
}
printf("\n");
}
// Example usage of the dynamic array
int main() {
// Create a dynamic array with an initial capacity of 4
DynamicArray* arr = create_array(4);
// Add elements to the array (this will cause resizing)
for (int i = 0; i < 20; i++) {
push(arr, i); // Add the number i to the array
print_array(arr); // Print the current state of the array
}
// Remove elements from the array (this will cause shrinking)
for (int i = 0; i < 18; i++) {
pop(arr); // Remove the last element from the array
print_array(arr); // Print the current state of the array
}
// Free the memory when done
free_array(arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment