stack
来源:互联网 发布:投影仪检测软件 编辑:程序博客网 时间:2024/06/14 14:46
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>
#define ITYPE float
#define STACKINC 2
#define STACKSIZE 100
#define EMPTY 0
#define FULL -1
#define STACKINC 2
#define STACKSIZE 100
#define EMPTY 0
#define FULL -1
typedef enum{false, true}bool;
typedef struct Stack {
ITYPE *data;
int top;
int max;
}stack;
ITYPE *data;
int top;
int max;
}stack;
void Chack_Stack(stack *s)
{
if( NULL == s) {
printf("no stack\n");
exit(0);
}
}
{
if( NULL == s) {
printf("no stack\n");
exit(0);
}
}
bool Init_Stack(stack *s)
{
Chack_Stack(s);
s->data = (ITYPE *)malloc(STACKSIZE * sizeof(ITYPE));
if(s->data == NULL) return false;
s->top = -1;
s->max = STACKSIZE;
return true;
}
{
Chack_Stack(s);
s->data = (ITYPE *)malloc(STACKSIZE * sizeof(ITYPE));
if(s->data == NULL) return false;
s->top = -1;
s->max = STACKSIZE;
return true;
}
bool Extend_Stack(stack *s)
{
Chack_Stack(s);
ITYPE *data = s->data;
s->data = (ITYPE *)realloc(s->data, STACKINC * s->max * sizeof(ITYPE));
if(s->data == NULL) {
s->data = data;
data = NULL;
return false;
}
s->max *= STACKINC;
data = NULL;
return true;
}
{
Chack_Stack(s);
ITYPE *data = s->data;
s->data = (ITYPE *)realloc(s->data, STACKINC * s->max * sizeof(ITYPE));
if(s->data == NULL) {
s->data = data;
data = NULL;
return false;
}
s->max *= STACKINC;
data = NULL;
return true;
}
int Stack_Occupy(stack *s)
{
Chack_Stack(s);
if(-1 == s->top) return EMPTY;
if(s->max == s->top + 1) return FULL;
return s->top + 1;
}
{
Chack_Stack(s);
if(-1 == s->top) return EMPTY;
if(s->max == s->top + 1) return FULL;
return s->top + 1;
}
ITYPE Stack_Top(stack *s)
{
Chack_Stack(s);
if(Stack_Occupy(s) == EMPTY) {
printf("no data!\n");
exit(0);
}
return s->data[s->top];
}
{
Chack_Stack(s);
if(Stack_Occupy(s) == EMPTY) {
printf("no data!\n");
exit(0);
}
return s->data[s->top];
}
void Push_Stack(stack *s, ITYPE x)
{
Chack_Stack(s);
if(Stack_Occupy(s) == FULL) {
if(Extend_Stack(s) == false) {
printf("memory is full!\n");
exit(0);
}
}
s->top++;
s->data[s->top] = x;
}
{
Chack_Stack(s);
if(Stack_Occupy(s) == FULL) {
if(Extend_Stack(s) == false) {
printf("memory is full!\n");
exit(0);
}
}
s->top++;
s->data[s->top] = x;
}
ITYPE Pop_Stack(stack *s)
{
Chack_Stack(s);
if(Stack_Occupy(s) == EMPTY) {
printf("stack empty!\n");
exit(0);
}
s->top--;
return s->data[s->top + 1];
}
{
Chack_Stack(s);
if(Stack_Occupy(s) == EMPTY) {
printf("stack empty!\n");
exit(0);
}
s->top--;
return s->data[s->top + 1];
}
void Destory_Stack(stack *s)
{
Chack_Stack(s);
free(s->data);
s->data = NULL;
s = NULL;
printf("free stack successfully!\n");
}
{
Chack_Stack(s);
free(s->data);
s->data = NULL;
s = NULL;
printf("free stack successfully!\n");
}
阅读全文
0 1
- stack
- stack
- stack
- stack
- Stack
- Stack
- Stack
- Stack
- stack
- Stack
- stack
- stack
- Stack
- stack
- Stack
- Stack
- stack
- stack.
- SpringMvc 页面跳转
- [51Nod 2026] Gcd and Lcm
- 24_多线程
- C语言编写三子棋完整代码(分三个文件)
- jenkins构建Docker 镜像(基于Jenkins的Docker镜像及Jenkins插件)
- stack
- Vmware下CentOS7最小化安装
- Java8新特性
- window下的Python 环境配置
- 2015蓝桥杯A组C/C++第一题方程整数解
- 考试GG论 Week1
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest H. Palindromic Cut
- 蒟蒻复习之-----最小生成树
- C++实现向量构造的线性表