#include<stdio.h>
#include <stdlib.h>
#define ElementType int
struct SNode {
ElementType Data;
struct SNode* Next;
};
typedef struct SNode* Stack;
Stack CreateStack()
{
Stack s = (Stack)malloc(sizeof(struct SNode));
s->Next = nullptr;
return s;
}
bool IsEmpty(Stack s)
{
return s->Next == nullptr ? true : false;
}
void Push(ElementType item, Stack s)
{
Stack node = (Stack)malloc(sizeof(struct SNode));
node->Data = item;
node->Next = s->Next;
s->Next = node;
}
ElementType Pop(Stack s)
{
if (IsEmpty(s)) {
printf("stack is empty\n");
return NULL;
}
Stack node = s->Next;
ElementType value = node->Data;
s->Next = node->Next;
free(node);
return value;
}
int main()
{
Stack s = CreateStack();
bool status;
status = IsEmpty(s);
if (status) {
printf("栈为空\n");
}
else {
printf("栈非空\n");
}
Push(10, s);
Push(20, s);
ElementType value = Pop(s);
printf("栈顶元素=%d\n", value);
value = Pop(s);
printf("栈顶元素=%d\n", value);
}
