|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <malloc.h>
typedef struct Stack
{
int data;
struct Stack * snext;
}Stack;
typedef struct pStack
{
struct Stack * top;
struct Stack * base;
int stacksize;
}pStack;
typedef struct pStack * PS;
int InitStack(PS);
int Push(PS,int );
int Pop(PS,int *);
int ShowStack(PS);
int GetStackNum(PS);
int main()
{
pStack S;
int e=100;
InitStack(&S);
Push(&S,100);
Push(&S,200);
Push(&S,300);
Push(&S,1700);
Push(&S,1500);
ShowStack( &S);
Pop(&S,&e);
printf("1.[%d] 出栈\n",e);
Pop(&S,&e);
printf("2.[%d] 出栈\n",e);
Pop(&S,&e);
printf("3.[%d] 出栈\n",e);
Pop(&S,&e);
printf("4.[%d] 出栈\n",e);
ShowStack( &S);
int i=0;
i=GetStackNum(&S);
printf("栈中的数据个数:%d \n",i);
getchar();
return 1;
}
int GetStackNum(PS S)
{
return S->stacksize;
}
int Pop(PS S,int *e)
{
Stack *s;
s=S->top;
*e=s->data;
S->top=s->snext;
free(s);
S->stacksize--;
return 1;
}
int Push(PS S,int e)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
if(!p)
return 0;
p->snext=S->top;
p->data=e;
S->top=p;
S->stacksize++;
return 1;
}
int ShowStack(PS S)
{
Stack *p=S->top;
while (p!=S->base)
{
printf("%d \n",p->data);
p=p->snext;
}
return 1;
}
int InitStack(PS S)
{
S->base=(Stack *)malloc(sizeof(Stack));
if(!S->base)
return 0;
S->top=S->base;
S->base->snext=NULL;
S->stacksize=0;
return 1;
} |
|