#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");
}