C语言实现栈–自导入

koaker 发布于 2025-05-26 C语言


typedef struct {
    void** items;  // 指向一个 void 指针数组的指针
    int top;       // 栈顶元素索引 -1表示栈为空
    int capacity;  // 当前分配的容量
    size_t element_size;
} CStack;

typedef struct {
    const CStack* stack;
    int current_index;
} CStacKIterator;

#define VOS_OK 0
#define VOS_ERR 1
CStack* cstack_create(int initial_capacity, size_t element_size) {
    if (element_size == 0) {
        return NULL;
    }

    CStack* cstack = (CStack*)malloc(sizeof(CStack));
    if (cstack == NULL) {
        return NULL;
    }
    
    cstack->element_size = element_size;
    cstack->top = -1;

    if (initial_capacity < 10) {
        initial_capacity = 10;
    }

    // 检查整数溢出
    if (initial_capacity > INT_MAX / sizeof(void*)) {
        free(cstack);
        return NULL;
    }

    cstack->items = (void **)malloc(initial_capacity * sizeof(void *));
    memset(cstack->items, 0, initial_capacity * sizeof(void *));
    if (cstack->items == NULL) {
        free(cstack);
        return NULL;
    }

    cstack->capacity = initial_capacity;
    return cstack;
}

void cstack_destroy(CStack* stack) {
    if (stack == NULL) {
        return ;
    }

    if (stack->items != NULL) {
        free(stack->items);
        stack->items = NULL;
    }

    // 清理
    stack->top = -1;
    stack->capacity = 0;
    stack->element_size = 0;

    free(stack);
}

int cstack_push(CStack *stack, void* item) {
    if (stack == NULL || item == NULL) {
        return VOS_ERR;
    }

    if (stack->top == stack->capacity - 1) {
        if (stack->capacity > INT_MAX / 2) {
            return VOS_ERR;
        }

        int new_capacity = stack->capacity * 2;

        if (new_capacity > INT_MAX / sizeof(void*)) {
            return VOS_ERR;
        }

        void** new_items = (void**) realloc(stack->items, new_capacity * sizeof(void *));
        if (new_items == NULL) {
            return VOS_ERR;
        }
        memset(new_items + stack->capacity, 0, (new_capacity - stack->capacity) * sizeof(void*));
        
        stack->items = new_items;
        stack->capacity = new_capacity;
    }

    stack->top++;
    stack->items[stack->top] = item;
    return VOS_OK;
}

void *cstack_pop(CStack* stack) {
    if (stack == NULL || stack->top == -1) {
        return NULL;
    }

    void *item_to_pop = stack->items[stack->top];
    stack->items[stack->top] = NULL;
    stack->top--;

    if (stack->top + 1 < stack->capacity / 4 && stack->capacity > 10) {
        int new_capacity = stack->capacity / 2;
        if (new_capacity > 10) {
            void** new_items = (void**)realloc(stack->items, new_capacity * sizeof(void *));
            if (new_items != NULL) {
                stack->items = new_items;
                stack->capacity = new_capacity;
            }
        }
    }
    return item_to_pop;
}

void *cstack_top_element(const CStack* stack) {
    if (stack == NULL || stack->items == NULL || stack->top == -1) {
        return NULL;
    }
    return stack->items[stack->top];
}

int cstack_size(const CStack* stack) {
    if (stack == NULL) {
        return 0;
    }
    return stack->top + 1;
}

bool cstack_empty(const CStack* stack) {
    if (stack == NULL) {
        return true;
    }
    return stack->top == -1;
}

void cstack_cleanAll(CStack *stack) {
    if (stack == NULL) {
        return ;
    }
    while(!cstack_empty(stack)) {
        free(cstack_pop(stack));
    }
    cstack_destroy(stack);
}
此作者没有提供个人介绍。
最后更新于 2025-05-26