Dynamically Growing Array in C – GeeksforGeeks

#include <stdio.h>

#include <stdlib.h>

  

#define INITIAL_SIZE 8

  

typedef struct {

    size_t size;

    size_t capacity;

    int* array;

}dynamic_array;

  

void arrayInit(dynamic_array** arr_ptr);

void freeArray(dynamic_array* container);

  

void insertItem(dynamic_array* container, int item);

void updateItem(dynamic_array* container, int i, int item);

int getItem(dynamic_array* container, int i);

void deleteItem(dynamic_array* container, int item);

void printArray(dynamic_array* container);

  

  

int main()

{

    dynamic_array* arr;

    arrayInit(&arr);

      

    for (int i = 0; i < 6; i++) {

        insertItem(arr, i + 11);

    }

    printArray(arr);

    printf("%d\n", getItem(arr, 3));

    deleteItem(arr, 3);

    printArray(arr);

  

    for (int i = 0; i < 5; i++) {

        insertItem(arr, i + 17);

    }

  

    printArray(arr);

  

    freeArray(arr);

    int var;

    return 0;

}

  

void arrayInit(dynamic_array** arr_ptr)

{

    dynamic_array *container;

    container = (dynamic_array*)malloc(sizeof(dynamic_array));

    if(!container) {

        printf("Memory Allocation Failed\n");

        exit(0);

    }

  

    container->size = 0;

    container->capacity = INITIAL_SIZE;

    container->array = (int *)malloc(INITIAL_SIZE * sizeof(int));

    if (!container->array){

        printf("Memory Allocation Failed\n");

        exit(0);

    }

  

    *arr_ptr = container;

}

  

void insertItem(dynamic_array* container, int item)

{

    if (container->size == container->capacity) {

        int *temp = container->array;

        container->capacity <<= 1;

        container->array = realloc(container->array, container->capacity * sizeof(int));

        if(!container->array) {

            printf("Out of Memory\n");

            container->array = temp;

            return;

        }

    }

    container->array[container->size++] = item;

}

  

int getItem(dynamic_array* container, int index)

{

    if(index >= container->size) {

        printf("Index Out of Bounds\n");

        return -1;

    }

    return container->array[index];

}

  

void updateItem(dynamic_array* container, int index, int item)

{

    if (index >= container->size) {

        printf("Index Out of Bounds\n");

        return;

    }

    container->array[index] = item;

}

  

void deleteItem(dynamic_array* container, int index)

{

    if(index >= container->size) {

        printf("Index Out of Bounds\n");

        return;

    }

  

    for (int i = index; i < container->size; i++) {

        container->array[i] = container->array[i + 1];

    }

    container->size--;

}

  

void printArray(dynamic_array* container)

{

    printf("Array elements: ");

    for (int i = 0; i < container->size; i++) {

        printf("%d ", container->array[i]);

    }

    printf("\nSize: ");

    printf("%lu", container->size);

    printf("\nCapacity: ");

    printf("%lu\n", container->capacity);

}

  

void freeArray(dynamic_array* container)

{

    free(container->array);

    free(container);

}

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
arraydynamicallyfineradar updateFree Fire Redeem Codesgadget updateGeeksforGeeksgrowingLatest tech newsTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment