typedef struct SNode* Stack;
struct SNode
{
int Data;
struct SNode* Next;
};
Stack CreateStack()
{
Stack s;
s = (Stack)malloc(sizeof(struct SNode));
s->Next = NULL;
return s;
}
//判断堆栈s是否为空,若为空返回整数1,否则返回0
int IsEmpty(Stack s)
{
return (s->Next == NULL);
}
void Push(int item, Stack s)
{
struct SNode* TmpCell;
TmpCell = (struct SNode*)malloc(sizeof(struct SNode));
TmpCell->Data = item;
TmpCell->Next = s->Next;
s->Next = TmpCell;
}
int pop(Stack s)
{
struct SNode* FirstCell;
int TopElem;
if (IsEmpty(s))
{
printf("堆栈空"); return NULL;
}
else {
FirstCell = s->Next;
s->Next = FirstCell->Next;
TopElem = FirstCell->Data;
free(FirstCell);
return TopElem;
}
}
堆栈的链式存储实现
最新推荐文章于 2022-09-07 11:13:32 发布