c语言编程之栈(数组实现)

时间:2022-09-26 20:41:51

  用数组实现的顺序栈,完成了出栈入栈功能。

   #include"stdio.h"
typedef int element;
#define max 100
typedef struct Stack{
element data[max];
element bottom;
element top;
}stack,*pStack; //push data into the stack
element push(pStack P,element num)
{
int i=;
if(P->top==P->bottom)
{
printf("stack is empty\n");
return ;
}
++(P->top);
P->data[P->top]=num;
printf("p->top:%d\n",P->top);
} //pop data out of the stack
element pop(pStack P)
{
if(P->top==P->bottom)
{
printf("stack is empty\n");
return ;
}
printf("pop num position:%d pop num :%d\n",P->top,P->data[P->top]);
P->top--;
return ;
} //init a stack
element Init_stack(pStack P,element num)
{
int i=;
P->top=-;
P->bottom=;
for(;i<num;i++)
{
P->data[i]=i;
++(P->top);
printf("data:%d\n",P->data[i]);
}
printf("init stack is finished\n");
return ;
}
//clear a stack
element Clear_stack()
{ }
//get a data from top stack
element Get_topdata()
{ }
//delet a stack
element Delet_stack()
{ }
//print stack's data
element Print_stack(pStack P)
{
int num=;
num=P->top;
while(num>=(P->bottom))
{
printf("stacknum:%d\n",P->data[num]);
--num;
}
}
element main()
{
pStack P;
element *numb=;
P=(pStack)malloc(sizeof(struct Stack));
Init_stack(P,);
push(P,);
push(P,);
push(P,);
push(P,);
//push(P,80);
Print_stack(P);
pop(P);
pop(P);
Print_stack(P);
return ;
}