动态数组实现堆栈
来源:互联网 发布:宏景软件科技有限公司 编辑:程序博客网 时间:2024/05/16 05:41
dy_stack.h
#define STACK_TYPE char
extern void push(STACK_TYPE value);
extern void pop(void);
extern STACK_TYPE top(void);
int is_empty();
int is_full();
extern void create_stack(int size);//unsigned int
extern void destroy_stack();
dy_stack.c
#include "dy_stack.h"
#include <assert.h>
#include <malloc.h>
#include <stdio.h>
//#define STACK_SIZE 100 /*堆栈中数值的最大限制*/
//存储堆栈中的数组和一个指向堆栈顶部元素的指针
//所有不属于外部接口的内容都被声明为static,这可防止用户使用
//预定义接口之外的任何方式访问堆栈的值
static int stack_size = 0;
static STACK_TYPE *stack;
static int top_element = -1;
void create_stack(int size)
{
assert(stack_size == 0);
stack_size = size;
stack = malloc(stack_size * sizeof(STACK_TYPE));
assert(stack != NULL);
}
void destroy_stack(void)
{
assert(stack_size > 0);
stack_size = 0;
free(stack);
stack = (STACK_TYPE *)NULL;
}
int is_empty()
{
assert(stack_size > 0);
return top_element == -1;
}
int is_full()
{
assert(stack_size > 0);
return top_element == stack_size - 1;
}
void push(STACK_TYPE value)
{
assert(!is_full());
top_element += 1;
stack[top_element] = value;
}
void pop()
{
assert(!is_empty());
top_element -= 1;
}
STACK_TYPE top()
{
assert(!is_empty());
return stack[top_element];
}
test.c
#include "dy_stack.h"
#include <stdio.h>
void main()
{
create_stack(100);
push('a');
push('b');
push('c');
push('d');
push('e');
pop();
pop();
printf("top = %c/n", top());
}
- 动态数组实现堆栈
- 动态数组实现的堆栈
- 用动态数组实现堆栈数据结构
- C语言堆栈实现( 动态数组 )
- 经典抽象数据类型之堆栈实现(动态数组)
- 数据结构与算法之动态数组实现堆栈
- 静态数组实现堆栈
- 用数组实现堆栈
- 数组实现堆栈
- 堆栈(数组实现)
- 堆栈的数组实现
- 堆栈用数组实现
- 数组实现堆栈
- 两个数组实现堆栈
- 数组实现堆栈操作
- 堆栈的数组实现
- 用数组实现堆栈
- 使用数组实现堆栈
- as3数据的浅复制和深复制
- 关于android AppWidget初探①
- hi,好啊
- 用数组实现堆栈
- BeanUtils.setProperty的使用
- 动态数组实现堆栈
- BOM 创建同步(消息类型和处理代码)
- vtk安装经典方法
- Android 桌面组件【app widget】 进阶项目②--心情记录器
- JavaScript大杂烩
- three20介绍
- c# 调用有字符串参数的 DLL
- 用户注册_表单验证
- 链表实现堆栈