1.结构体定义
typedef int ElementType;
typedef struct SNode *Stack;
struct SNode
{
ElementType Data; //数据域
struct SNode *Next; //指针域
};
2.初始化
只初始化头结点
Stack CreateStack()
{
//构造一个堆栈的头结点并返回指针
Stack S;
S = (Stack)malloc (sizeof(struct SNode));
S->Next = NULL;
return S;
}
3.是否为空
判断:头结点指向空
int IsEmpty(Stack S)
{
return S->Next ==NULL;
}
4.入栈
void Push(ElementType item,Stack S)
{
struct SNode *tmp;
tmp = (struct SNode *)malloc(sizeof(struct SNode ));
tmp->Data = item;
tmp->Next = S->Next;
S->Next = tmp;
}
5.出栈
ElementType Pop(Stack S)
{
struct SNode *firstcell;<