#include <stdio.h>
#include <stdlib.h>
#define MAX 10
enum st
{
STACK_EMPTY,
STACK_NOEMPTY,
STACK_FULL,
STACK_NOFULL,
PUSH_OK,
PUSH_ERR,
POP_OK,
POP_ERR
};
typedef struct node
{
int stack[MAX];
int top;
}STACK1;
void int_stack(STACK1 *s)
{
s->top = -1;
}
int empty_stack(STACK1 *s)
{
if(s->top == -1)
{
return STACK_EMPTY;
}
else
{
return STACK_NOEMPTY;
}
}
int full_stack(STACK1 *s)
{
if(s->top == MAX-1)
{
return STACK_FULL;
}
else
{
return STACK_NOFULL;
}
}
int push_stack(STACK1 *s,int data)
{
if(full_stack(s) != STACK_FULL)
{
s->top++;
s->stack[s->top] = data;
return PUSH_OK;
}
else
{
return PUSH_ERR;
}
}
int pop_stack(STACK1 *s)
{
if(empty_stack(s) != STACK_EMPTY)
{
return s->stack[s->top--];
}
return POP_ERR;
}
int main()
{
STACK1 *s = (STACK1 *)malloc(sizeof(STACK1));
if(s == NULL)
{
printf("malloc error!\n");
return 0;
}
int_stack(s);
int i = 0;
int temp = 0;
for(i = 0;i < 5;i++)
{
if(push_stack(s,i+1) != PUSH_ERR)
{
push_stack(s,i+1);
printf("%d\n",s->stack[s->top]);
}
}
printf("出栈:\n");
for(i = 0;i < 5;i++)
{
if((temp = pop_stack(s)) != POP_ERR)
{
printf("%d\n",pop_stack(s));
}
}
return 0;
}
对于栈的相关操作
最新推荐文章于 2022-07-06 11:04:08 发布