11102

来源:互联网 发布:ubuntu安装exe 编辑:程序博客网 时间:2024/06/03 20:09
#include <stdio.h>
#include <stdlib.h>
#define max 10


typedef struct stack
{
int stack[max];
int top;
}Stack;


enum return_result {full_ok,full_no,empty_ok,empty_no,
push_ok,push_no,pop_ok,pop_no=-1};


void creat_stack(Stack ** stack)
{
    (*stack)=(Stack*)malloc(sizeof(Stack));
if((*stack) == NULL)
{
printf("creat error!\n");
exit(-1);
}
}


void init_stack(Stack ** stack)
{
(*stack)->top = -1;
}


int is_full(Stack ** stack)
{
if((*stack)->top >= max)
{
return full_ok;
}
return full_no;
}


int push_stack(Stack ** stack,int num)
{
if(is_full(stack) == full_ok)
{
        printf("the stack is full!\n");
return push_ok;
}
(*stack)->top++;
(*stack)->stack[(*stack)->top] = num;
    return push_no;
}


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("the stack is empty!\n");
return pop_no;
}
return ((*stack)->stack[((*stack)->top)--]);
}


int main()
{
Stack * stack;
int i;
int ret;


creat_stack(&stack);
init_stack(&stack);


for(i = 0;i < max;i++)
{
push_stack(&stack,i);
//printf("push success!\n");
}


for(i = 0;i < max;i++)
{
        ret = pop_stack(&stack);
if(ret == pop_no)
{
break;
}
printf("stack[%d]=%d\n",i,ret);
}
    return 0;
}
0 0