/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#define Max 10
struct stack{
int stack_text[Max];
int top;//数组元素(栈元素)下标
};
typedef struct stack Stack;
enum return_result{PUSH_ok,PUSH_no,FULL_ok,FULL_no,EMPTY_ok,EMPTY_no};
void is_malloc_ok(Stack *stack)
{
if(stack == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_stack(Stack **stack)//分配空间
{
(*stack) = (Stack*)malloc(sizeof(Stack));//分配空间
is_malloc_ok(*stack);
}
void init_stack(Stack *stack)//初始化
{
stack->top = -1; //top赋值为-1,位置在栈顶
}
int is_full(Stack *stack)//判断栈是否满
{
if(stack->top == Max)
return FULL_ok;
else
return FULL_no;
}
int push_stack(Stack *stack,int num)//入栈操作
{
if(is_full(stack) == FULL_ok)//栈满
{
printf("the stack is full!\n");
return PUSH_no;
}
//栈未满
stack->top++; //top为-1 执行完top为0
stack->stack_text[stack->top] = num; //值写入
return PUSH_ok;
}
int is_empty(Stack* stack)//判断栈是否为空
{
if(stack->top == -1)
{
return EMPTY_ok;
}
return EMPTY_no;
}
int pop_stack(Stack *stack)//出栈操作
{
if(is_empty(stack) == EMPTY_ok)//栈已空
{
printf("stack is empty\n");
return EMPTY_ok;
}
else //栈不空 栈内有内容
return EMPTY_no;
}
int main()
{
Stack* stack;
int i;
creat_stack(&stack);//创建栈
init_stack(stack);//栈初始化
for(i = 0;i < Max;i++)//入栈次数
{
if(push_stack(stack,i + 1) == PUSH_ok)//入栈
printf("入栈成功!\n");
else
printf("入栈失败!\n");
}
for(i = 0;i < Max;i++)//出栈次数
{
if(pop_stack(stack) == EMPTY_no)//出栈判断栈是否为空
{
printf("num = %d\n",stack->stack_text[stack->top]);
stack->top--;//前移
}
else
{
printf("栈空!\n");
}
}
return 0;
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#define Max 10
struct stack{
int stack_text[Max];
int top;//数组元素(栈元素)下标
};
typedef struct stack Stack;
enum return_result{PUSH_ok,PUSH_no,FULL_ok,FULL_no,EMPTY_ok,EMPTY_no};
void is_malloc_ok(Stack *stack)
{
if(stack == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void creat_stack(Stack **stack)//分配空间
{
(*stack) = (Stack*)malloc(sizeof(Stack));//分配空间
is_malloc_ok(*stack);
}
void init_stack(Stack *stack)//初始化
{
stack->top = -1; //top赋值为-1,位置在栈顶
}
int is_full(Stack *stack)//判断栈是否满
{
if(stack->top == Max)
return FULL_ok;
else
return FULL_no;
}
int push_stack(Stack *stack,int num)//入栈操作
{
if(is_full(stack) == FULL_ok)//栈满
{
printf("the stack is full!\n");
return PUSH_no;
}
//栈未满
stack->top++; //top为-1 执行完top为0
stack->stack_text[stack->top] = num; //值写入
return PUSH_ok;
}
int is_empty(Stack* stack)//判断栈是否为空
{
if(stack->top == -1)
{
return EMPTY_ok;
}
return EMPTY_no;
}
int pop_stack(Stack *stack)//出栈操作
{
if(is_empty(stack) == EMPTY_ok)//栈已空
{
printf("stack is empty\n");
return EMPTY_ok;
}
else //栈不空 栈内有内容
return EMPTY_no;
}
int main()
{
Stack* stack;
int i;
creat_stack(&stack);//创建栈
init_stack(stack);//栈初始化
for(i = 0;i < Max;i++)//入栈次数
{
if(push_stack(stack,i + 1) == PUSH_ok)//入栈
printf("入栈成功!\n");
else
printf("入栈失败!\n");
}
for(i = 0;i < Max;i++)//出栈次数
{
if(pop_stack(stack) == EMPTY_no)//出栈判断栈是否为空
{
printf("num = %d\n",stack->stack_text[stack->top]);
stack->top--;//前移
}
else
{
printf("栈空!\n");
}
}
return 0;
}