Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing a Dynamic Array-Based Sequential List

Tech 2

DynamicSeqList.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define DEFAULT_START_SIZE 4

typedef double DslDataType;

typedef struct DynamicSeqList {
    DslDataType* data;
    int elementCount;
    int maxCapacity;
} Dsl;

void DslDisplay(const Dsl* list);
void DslInitialize(Dsl* list);
void DslFree(Dsl* list);
static void DslEnsureSpace(Dsl* list);
void DslAppend(Dsl* list, DslDataType value);
void DslPrepend(Dsl* list, DslDataType value);
void DslRemoveLast(Dsl* list);
void DslRemoveFirst(Dsl* list);
int DslSearch(const Dsl* list, DslDataType target);
void DslInsertAt(Dsl* list, int index, DslDataType value);
void DslDeleteAt(Dsl* list, int index);

DynamicSeqList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "DynamicSeqList.h"

void DslDisplay(const Dsl* list) {
    assert(list != NULL);
    for (int i = 0; i < list->elementCount; ++i) {
        printf("%.2f ", list->data[i]);
    }
    printf("\n");
}

void DslInitialize(Dsl* list) {
    assert(list != NULL);
    list->elementCount = 0;
    list->maxCapacity = DEFAULT_START_SIZE;
    list->data = (DslDataType*)malloc(sizeof(DslDataType) * DEFAULT_START_SIZE);
    if (list->data == NULL) {
        perror("Failed to allocate initial memory for DynamicSeqList");
        exit(EXIT_FAILURE);
    }
}

void DslFree(Dsl* list) {
    assert(list != NULL);
    free(list->data);
    list->data = NULL;
    list->elementCount = 0;
    list->maxCapacity = 0;
}

static void DslEnsureSpace(Dsl* list) {
    assert(list != NULL);
    if (list->elementCount == list->maxCapacity) {
        int newCap = list->maxCapacity * 2;
        DslDataType* expanded = (DslDataType*)realloc(list->data, sizeof(DslDataType) * newCap);
        if (expanded == NULL) {
            perror("Failed to expand DynamicSeqList capacity");
            exit(EXIT_FAILURE);
        }
        list->data = expanded;
        list->maxCapacity = newCap;
    }
}

void DslAppend(Dsl* list, DslDataType value) {
    assert(list != NULL);
    DslEnsureSpace(list);
    list->data[list->elementCount++] = value;
}

void DslPrepend(Dsl* list, DslDataType value) {
    assert(list != NULL);
    DslEnsureSpace(list);
    for (int i = list->elementCount; i > 0; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[0] = value;
    list->elementCount++;
}

void DslRemoveLast(Dsl* list) {
    assert(list != NULL);
    assert(list->elementCount > 0);
    list->elementCount--;
}

void DslRemoveFirst(Dsl* list) {
    assert(list != NULL);
    assert(list->elementCount > 0);
    for (int i = 0; i < list->elementCount - 1; ++i) {
        list->data[i] = list->data[i + 1];
    }
    list->elementCount--;
}

int DslSearch(const Dsl* list, DslDataType target) {
    assert(list != NULL);
    for (int i = 0; i < list->elementCount; ++i) {
        if (list->data[i] == target) {
            return i;
        }
    }
    return -1;
}

void DslInsertAt(Dsl* list, int index, DslDataType value) {
    assert(list != NULL);
    assert(index >= 0 && index <= list->elementCount);
    DslEnsureSpace(list);
    for (int shift = list->elementCount; shift > index; --shift) {
        list->data[shift] = list->data[shift - 1];
    }
    list->data[index] = value;
    list->elementCount++;
}

void DslDeleteAt(Dsl* list, int index) {
    assert(list != NULL);
    assert(index >= 0 && index < list->elementCount);
    for (int shift = index; shift < list->elementCount - 1; ++shift) {
        list->data[shift] = list->data[shift + 1];
    }
    list->elementCount--;
}

test_dsl.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "DynamicSeqList.h"

void BasicOperationsTest() {
    Dsl myList;
    DslInitialize(&myList);

    DslAppend(&myList, 1.1);
    DslAppend(&myList, 2.2);
    DslAppend(&myList, 3.3);
    DslAppend(&myList, 4.4);
    DslDisplay(&myList);

    DslPrepend(&myList, 0.0);
    DslDisplay(&myList);

    DslInsertAt(&myList, 3, 99.9);
    DslDisplay(&myList);

    DslRemoveLast(&myList);
    DslDisplay(&myList);

    DslRemoveFirst(&myList);
    DslDisplay(&myList);

    DslDeleteAt(&myList, 1);
    DslDisplay(&myList);

    int idx = DslSearch(&myList, 3.3);
    printf("Index of 3.3: %d\n", idx);

    DslFree(&myList);
}

int main() {
    BasicOperationsTest();
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.