#include <stdio.h>
#include <stdlib.h>
enum return_val{EMPTY_OK,EMPTY_NO,POP_OK,POP_NO,PUSH_OK,PUSH_NO,GET_NO,GET_OK};
struct stack_data
{
int num;
struct stack_data *next;
};
typedef struct stack_data Stack;
int is_malloc_ok(Stack *node)
{
if(node==NULL)
{
return 0;
}
else
{
return 1;
}
}
int create_node(Stack **node)
{
*node=(Stack*)malloc(sizeof(Stack));
is_malloc_ok(*node);
}
void create_head(Stack **head)
{
create_node(head);
(*head)->next=NULL;
}
int is_empty_ok(Stack *node)
{
if(node->next==NULL)
{
return EMPTY_OK;
}
else
{
return EMPTY_NO;
}
}
int push_stack(Stack *head,int num)
{
Stack *p;
if(create_node(&p)==0)
{
return PUSH_NO;
}
p->num=num;
p->next=head->next;
head->next=p;
return PUSH_OK;
}
int gettop_stack(Stack *head,int *num)
{
if(is_empty_ok(head)==EMPTY_OK)
{
return GET_NO;
}
else
{
*num=head->next->num;
head->next=head->next->next;
return GET_OK;
}
}
int pop_stack(Stack *head,int *num)
{
if(is_empty_ok(head)==EMPTY_OK)
{
return POP_NO;
}
else
{
Stack *p;
p=head->next;
*num=head->next->num;
head->next=head->next->next;
free(p);
return POP_OK;
}
}
void empty_stack(Stack *head)
{
Stack *p;
while(head->next!=NULL)
{
p=head->next;
head->next=head->next->next;
free(p);
}
}
void release_stack(Stack**head)
{
empty_stack(*head);
*head=NULL;
free(*head);
}
int main()
{
Stack *head;
int i;
int num;
create_head(&head);
for(i=0;i<10;i++)
{
if(push_stack(head,i+1)==PUSH_NO)
{
printf("push no!\n");
}
else
{
printf("push ok!num=%d\n",i+1);
}
}
for(i=0;i<7;i++)
{
if(gettop_stack(head,&num)==GET_NO)
{
printf("get no!\n");
}
else
{
printf("get ok!num=%d\n",num);
}
}
for(i=0;i<10;i++)
{
if(pop_stack(head,&num)==POP_NO)
{
printf("pop no!\n");
}
else
{
printf("pop ok!num=%d\n",num);
}
}
release_stack(&head);
printf("ok!");
return 0;
}