/*************************栈的链式存储--链栈**********************/
#include"stdio.h"
#include"stdlib.h"
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct StackNode
{
ElemType data;
struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct
{
LinkStackPtr top;
int count;
}LinkStack;
/********************Operator****************************/
void InitStack(LinkStack *s) //初始化栈
{
s->count=0;
}
Status Push(LinkStack *s, ElemType e) //入栈操作
{
LinkStackPtr p;
p=(LinkStackPtr)malloc(sizeof(StackNode));
p->data=e;
p->next=s->top;
s->top=p;
s->count++;
return TRUE;
}
Status StackEmpty(LinkStack s) //判断栈是否为空
{
if(0==s.count)
return TRUE;
return FALSE;
}
Status Pop(LinkStack *s,ElemType *e) //出栈
{
LinkStackPtr p;
if(StackEmpty(*s))
return FALSE;
*e=s->top->data;
p=s->top;
s->top=p->next;
free(p);
s->count--;
return TRUE;
}
void PrintStack(LinkStack s) //打印栈
{
LinkStackPtr p;
p=s.top;
while(s.count--)
{
printf("%6d",p->data);
p=p->next;
}
printf("\n");
}
main()
{
LinkStack *s;
ElemType *e;
s=(LinkStack*)malloc(sizeof(LinkStack));
e=(ElemType*)malloc(sizeof(ElemType));
InitStack(s);
Push(s,15);
Push(s,13);
PrintStack(*s);
Pop(s,e);
PrintStack(*s);
printf("%6d\n",*e);
}
数据结构之链栈的实现
最新推荐文章于 2024-01-18 16:29:45 发布