#define MAXSIZE 200
#include
#include
typedef struct seqstack
{
int data[MAXSIZE];
int top;
}seq_stack,*stack_type;
stack_type creat_stack()
{
stack_type s;
if(s=(stack_type)malloc(sizeof(seq_stack)))
{
s->top=0;
printf("\ncreat success!");
}
return(s);
}
int full_stack(stack_type s)
{
if(s->top==MAXSIZE)
return(1);
else
return(0);
}
int emty_stack(stack_type s)
{
if(s->top==0)
return(1);
else
return(0);
}
void push(stack_type s)
{
if(full_stack(s))
{
printf("\nstack is full!");
return;
}
else
printf("\nplease input data:");
scanf("%d",&s->data[s->top]);
s->top++;
printf("\npush success!");
}
void free_stack(stack_type s)
{
free(s);
printf("...");
}
int pop(stack_type s,int *tmp)
{
if(emty_stack(s))
{
printf("stack is emty!");
return(0);
}
else
{
s->top--;
*tmp=s->data[s->top];
return (1);
}
}
void restart_stack(stack_type s)
{
s->top=0;
}
void print_stack(stack_type s)
{
int i=s->top-1;
for(;i<=0;i--)
{
printf("%s ",s->data[s->top]);
}
}
void main()
{
int select,data;
stack_type s;
printf("\n");
printf("\n");
printf(" -------------------------\n");
printf(" | enter 0 to exit stack |\n");
printf(" | enter 1 to creat stack |\n");
printf(" | enter 2 to push stack |\n");
printf(" | enter 3 to pop stack |\n");
printf(" | enter 4 to print stack |\n");
printf(" | enter 5 to restr stack |\n");
printf(" | enter 6 to free stack |\n");
printf(" -------------------------\n");
printf("\n");
scanf("%d",&select);
while(select)
{
switch(select)
{
case 1: s=creat_stack();break;
case 2: push(s); break;
case 3: pop(s, &data);
printf("\nthe data is:");
printf("%d\n",data);
break;
case 4: print_stack(s);break;
case 5: restart_stack(s);break;
case 6: free_stack(s);break;
default : printf("\ninout erro!");
}
printf("\n");
printf("\n");
printf(" -------------------------\n");
printf(" | enter 0 to exit stack |\n");
printf(" | enter 1 to creat stack |\n");
printf(" | enter 2 to push stack |\n");
printf(" | enter 3 to pop stack |\n");
printf(" | enter 4 to print stack |\n");
printf(" | enter 5 to restr stack |\n");
printf(" | enter 6 to free stack |\n");
printf(" -------------------------\n");
printf("\n");
scanf("%d",&select);
}
}
阅读(1197) | 评论(0) | 转发(0) |