实现这个的初衷是想要 写树的非递归遍历的
/********************************************************************************
* Copyright(c) 2019-20xx
* All right resered.
*
* @file dynaminc_stack.h
* @author hongsmallgod
* @version V1.0.1
* @data 2019-9-10 15:53:29
* @brief 静态数组源文件
********************************************************************************/
#ifndef _DYNAMIC_STACK_H
#define _DYNAMIC_STACK_H
#include <stdbool.h>
typedef int element_type;
typedef struct stack {
int top; /* 从 0 开始 */
int size;
element_type *arr;
int (*num)(struct stack *); /* 堆栈元素的数量 */
bool (*pop)(struct stack *, element_type *);
bool (*push)(struct stack *, element_type);
bool (*full)(struct stack *);
bool (*empty)(struct stack *);
} stack;
extern stack *create_stack(int size);
extern void destory_stack(stack *s);
#endif
/********************************************************************************
* Copyright(c) 2019-20xx
* All right resered.
*
* @file dynaminc_stack.c
* @author hongsmallgod
* @version V1.0.1
* @data 2019-9-10 15:53:29
* @brief 静态数组头文件
********************************************************************************/
#include <stdlib.h>
#include "dynamic_stack.h"
static bool full(stack *s);
static bool empty(stack *s);
static bool push(stack *s, element_type d);
static bool pop(stack *s, element_type *d);
static int get_stack_num(stack *);
stack *create_stack(int size)
{
stack *s = (stack *)malloc(sizeof(stack));
if (NULL == s)
return NULL;
s->top = -1;
s->size = size;
s->arr = (element_type *)malloc(sizeof(element_type));
if (NULL == s->arr) {
free(s);
return NULL;
}
s->num = get_stack_num;
s->pop = pop;
s->push = push;
s->full = full;
s->empty = empty;
return s;
}
static int get_stack_num(stack *s)
{
return s->top + 1;
}
static bool full(stack *s)
{
if ((s->top + 1) == s->size)
return true;
return false;
}
static bool empty(stack *s)
{
return s->top == -1;
}
static bool push(stack *s, element_type d)
{
if (full(s))
return false;
s->arr[++s->top] = d;
return true;
}
static bool pop(stack *s, element_type *d)
{
if (empty(s))
return 0;
*d = s->arr[s->top--];
return 1;
}
void destory_stack(stack *s)
{
free(s->arr);
s->arr = NULL;
free(s);
}
609

被折叠的 条评论
为什么被折叠?



