#include <stdio.h>
#include <stdlib.h>
//queue 先进先出 FIFO
//Stack 先进后出 FILO
typedef int Boo;
#define TRUE 1;
#define False 0;
typedef struct Node
{
int id;
struct Node *p;
}stack;
void Push(stack **pptop,int id); //入栈
Boo kong(stack *pptop);
stack *Pop(stack **pptop); //出栈
int main()
{
int i;
stack *ptop = NULL;
stack *pmark = NULL;
Push(&ptop,1);
Push(&ptop,2);
Push(&ptop,3);
Push(&ptop,4);
pmark = Pop(&ptop);
/*while(ptop != NULL)
{
printf("%d\n",ptop ->id);
ptop = ptop ->p;
}*/
return 0;
}
void Push(stack **pptop,int id)
{
stack *ptemp = (stack *)malloc(sizeof(stack));
ptemp ->id = id;
ptemp ->p = NULL;
//判断栈里是否有节点
//没有
if(*pptop == NULL)
{
//栈顶指针指向新来的节点
*pptop = ptemp;
}
else
{
//新来的节点的指针指向栈顶
ptemp ->p = *pptop;
//栈顶指针指向新来的节点
*pptop = ptemp;
}
}
Boo kong(stack *pptop)
{
int count = 0;
while(pptop != NULL)
{
count ++;
pptop = pptop ->p;
}
if(count == 0)
return TRUE;
return False;
}
stack *Pop(stack **pptop)
{
stack *pMark =NULL;
if(!kong(*pptop) ) //kong(*pptop) == 0
{
pMark = *pptop;
*pptop = (*pptop)->p;
pMark->p = NULL;
}
return pMark;
}
栈
最新推荐文章于 2024-07-12 03:00:00 发布