刚才改的有错,这回所有功能我都试了。没问题了,记得加分
#include
#include
#include
#define Max 10
typedef int BOOL;
typedef int SElem;
typedef struct stack
{
int top;
int base;
SElem Elem[Max];
}Stack;
void push_stack(Stack *s,SElem e);
void pop_stack(Stack *s,SElem *e);
void gettop_stack(Stack *s,SElem *e);
void stack_traverse(Stack *s,SElem *e);
void stack_status(Stack *s);
void stack_length(Stack *e);
void destroy_stack(Stack *s);
void create_stack(Stack *s);
void push(Stack *s,SElem e);
void pop(Stack *s,SElem *e);
BOOL isempty(Stack *s);
BOOL isfull(Stack *s);
void free(Stack *s);
void push_stack(Stack *s,SElem e)
{
int ans;
if(isfull(s))
printf("This is a full stack\n");
else
{
printf("Insert data:\n");
scanf("%d%c",&e,&ans);
push(s,e);
}
}
void pop_stack(Stack *s,SElem *e)
{
if(isempty(s))
printf("This is a empty stack\n");
else
{
pop(s,e);
printf("Elements:%d",*e);
}
}
void gettop_stack(Stack *s,SElem *e)
{
if(isempty(s))
printf("This is a empty stack\n");
else
{
*e=s->Elem[s->top-1];
printf("Data at top of stack:%d",*e);
}
}
void stack_traverse(Stack *s,SElem *e)
{
if(isempty(s))
printf("This is a empty stack\n");
else
{
s->top--;
*e=s->top;
printf("Data:%d",*e);
push(s,*e);
}
}
void stack_status(Stack *s)
{
if(isempty(s))
printf("This is a empty stack\n");
else if(isfull(s))
printf("This is a full stack\n");
else
printf("This is not a full or empty stack\n");
}
void stack_length(Stack *e)
{
int i;
SElem p;
i=0;
p=e->top;
while(p!=e->base)
{
p++;
i++;
}
printf("Number of element=%d\n",i+1);
}
void destroy_stack(Stack *s)
{
free(s);
}
void create_stack(Stack *s)
{
s->top=0;
s->base=9;
}
void push(Stack *s,SElem e)
{
s->Elem[s->top]=e;
s->top++;
}
void pop(Stack *s,SElem *e)
{
s->top--;
*e=s->Elem[s->top];
}
void free(Stack *s)
{
s->top=s->base;
}
BOOL isempty(Stack *s)
{
return((BOOL)(s->top==0));
}
BOOL isfull(Stack *s)
{
return((BOOL)(s->top>=Max));
}
void main()
{
Stack h;
SElem e=0;
create_stack(&h);
int num;
do
{
printf("\n\nDemo stack is running\n");
printf("1.Push data\n");
printf("2.Pop data\n");
printf("3.Print data at top of stack\n");
printf("4.Print entire data\n");
printf("5.Print status\n");
printf("6.Number of element\n");
printf("7.Destroy stack and quit\n");
printf("Please insert your selection\n");
scanf("%d",&num);
switch(num)
{
case 1:push_stack(&h,e);break;
case 2:pop_stack(&h,&e);break;
case 3:gettop_stack(&h,&e);break;
case 4:stack_traverse(&h,&e);break;
case 5:stack_status(&h);break;
case 6:stack_length(&h);break;
case 7:destroy_stack(&h);return;
default:
printf("Selection invalid\n");
}
}while(1);
return;
}
应该注意一点:
在任何函数体中,尽量将变量定义放在该函数的开头,
而不要把变量定义放在中间或者后面..
...
void main()
{
...
int num;
create_stack(&h);
....
}