#include "malloc.h"
typedef struct astack *Stack;
typedef struct astack{
int top;
int maxtop;
int *data;
}Astack;
Stack StackInit(int size)
{
Stack S=(Stack)malloc(sizeof(*Stack));
S->top=-1;
S->maxtop=size;
S->data=(int *)malloc(size*sizeof(int));
return S;
}
int StackFull(Stack s)
{
return S->top>=S->maxtop;
}
int StackEmpty(Stack S)
{
return S->top==-1;
}
void push(int x,Stack S)
{
if(!StackFull(S))S->data[++S->top]=x;
else printf("Stack is full");
}
int pop(Stack S)
{
if(StackEmpty(s)){printf("The stack is empty");return 0;}
return S->data[top--];
}
void StackPrint(Stack s)
{ int i=0;
if(StackEmpty(S))printf("the stack is empty");
else {printf("print the stack from bottom to top :\n");while(i<=S->top)printf("/t%d",S->data);}
}
void main(void)
{
int a[10],i;
printf("enter the 10 number:\n");
for(i=0;i<10;i++)
{
printf("the %d number is :",i+1);
scanf("%d",&a);
}
S=StackInit(10);
for(i=0;i<10;i++)
{
push(a,S);
}
StackPrint(S);
for(i=0;i<=10;i++)
{
if(StackEmpty(S)){printf("stack is empty");break;}
else printf("now,the top number is : %d\n",Pop(S));
};
} |