前言
动态数组栈的基本操作
一、头文件
#ifndef DYNAMIC_STACK_H
#define DYNAMIC_STACK_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ElementType;
#define DEFAULT_CAPACITY 10
typedef struct {
ElementType* data;
int size;
int capacity;
}DynamicStack;
DynamicStack* stack_create();
void stack_push(DynamicStack* s, ElementType val);
ElementType stack_peek(DynamicStack* s);
bool is_empty(DynamicStack* s);
void print_stack(DynamicStack* s);
void stack_destroy(DynamicStack* s);
#endif
二、基本操作
#include "DynamicStack.h"
DynamicStack* stack_create() {
DynamicStack* stack = calloc(1, sizeof(DynamicStack));
if (stack == NULL) {
printf("error:calloc failed in stack_create.\n");