C语言实现堆栈
来源:互联网 发布:mac万能五笔输入法 编辑:程序博客网 时间:2024/05/19 03:45
堆栈特点:后进先出(Last-In First-Out,LIFO)
基本操作:进栈(push)和出栈(pop)
其他操作:空堆栈不能执行pop操作,因此需要判断堆栈是否为空。堆栈如果存在最大长度限制,那么需要判断堆栈是否已满。
基本操作:进栈(push)和出栈(pop)
其他操作:空堆栈不能执行pop操作,因此需要判断堆栈是否为空。堆栈如果存在最大长度限制,那么需要判断堆栈是否已满。
一、数组实现堆栈
#include#include #includeusing namespace std;#define STACK_TYPE int#define STACK_SIZE 100void push(STACK_TYPE value);STACK_TYPE pop(void);STACK_TYPE top(void);int is_Empty(void);int is_full(void);static STACK_TYPE stack[STACK_SIZE];static int top_element = -1;voidpush(STACK_TYPE value){assert(!is_full());top_element += 1;stack[top_element] = value;}STACK_TYPEpop(void){STACK_TYPE temp;assert(!is_Empty());temp = stack[top_element];top_element -= 1;return temp;}STACK_TYPEtop(void){assert(!is_Empty());return stack[top_element];}intis_Empty(){return top_element == -1;}int is_full(){return top_element == STACK_SIZE - 1;}
二、动态数组实现堆栈
#include#include #include#include #include using namespace std;#define STACK_TYPE intstatic STACK_TYPE *stack;static size_t stack_size;static int top_element = -1;void create_stack(size_t size);void destory_stack(void);void push(STACK_TYPE value);STACK_TYPE pop(void);STACK_TYPE top(void);int is_Empty();int is_Full();voidcreate_stack(size_t size){assert(stack_size==0);stack_size = size;stack = (STACK_TYPE*)malloc(stack_size * sizeof(STACK_TYPE));assert(stack!=NULL);}voiddestory_stack(){assert(stack_size>0);stack_size = 0;free(stack);stack = NULL;}voidpush(STACK_TYPE value){assert(!is_Full());top_element += 1;stack[top_element] = value;}STACK_TYPEpop(){STACK_TYPE temp;assert(!is_Empty());temp = stack[top_element];top_element -= 1;return temp;}STACK_TYPE top(){assert(!is_Empty());return stack[top_element];}intis_Empty(){assert(stack_size>0);return top_element == - 1;}int is_Full(){assert(stack_size>0);return top_element == stack_size-1;}
三、链式堆栈
#include#include #include#include #include using namespace std;#define FALSE 0#define STACK_TYPE inttypedef struct STACK_NODE {STACK_TYPE value;struct STACK_NODE *next;}StackNode;static StackNode *stack;void destory_stack();void push(STACK_TYPE value);void pop();STACK_TYPE top();int is_Empty();int is_Full();voiddestory_stack(){while (!is_Empty())pop();}voidpush(STACK_TYPE value){StackNode *new_node;new_node = (StackNode*)malloc(sizeof(StackNode));assert(new_node != NULL);new_node->value = value;new_node->next = stack;stack = new_node;}voidpop(){StackNode *first_node;assert(!is_Empty);first_node = stack;stack = first_node->next;free(first_node);}STACK_TYPEtop(){assert(!is_Empty());return stack->value;}int is_Empty(){return stack == NULL;}intis_Full(){return FALSE; //链式堆栈不会满}
阅读全文
1 0
- C语言实现堆栈
- C语言实现堆栈
- 堆栈的c 语言实现
- 堆栈的C语言实现
- C语言实现堆栈(栈)的数据结构
- 简单堆栈实现(C语言)
- C语言堆栈实现( 动态数组 )
- 堆栈实现迷宫出路(C语言)
- c语言 链表实现堆栈
- 堆栈数据结构的C语言实现
- 堆栈数据结构的C语言实现
- 堆栈的实现(c语言)
- 链式堆栈——C语言实现
- c语言 堆栈
- C语言的堆栈
- c语言的堆栈
- C语言堆栈入门
- c语言和堆栈
- Python学习之开发环境/开发工具(PyCharm)配置
- 通过listener理解域对象的生命周期
- 01:查找特定的值
- 多条件模糊查询
- 对于!=,负向查询肯定不能命中索引
- C语言实现堆栈
- 音乐播放时进度条和时间
- Searsar之更新,删除,插入操作的方法命名-yellowcong
- (03)oracle新人笔记------表的相关操作
- “FCoE全解系列”之网络融合交换机类型
- OpenCV3.3中逻辑回归(Logistic Regression)使用举例
- python查询数据库并导处csv文件
- 回归云平台的本质,通过平台+应用模式实现软件资源线上共享
- DataBinding系列(二):DataBinding的基本用法