include [HTML_REMOVED]
using namespace std;
//节点定义
struct Node{
int val;
Node * next;
}
//初始化
Node * init()
{
Node * dummy = new Node(-1);
return dummy;
}
//判断栈为空
bool empty(Node * dummy){
return dummy->next == NULL;
}
//入栈
void push(Node * dummy,int x){
Node * node = new Node(x);
node -> next = dummy -> next;
dummy -> next = node;
}
//出栈
void pop(Node * dummy){
dummy->next = dummy->next->next;
}
//读取栈顶元素
int top(Node * dummy){
return dummy->next->val;
}
int main()
{
// 测试链栈各操作
Node *dummy = init();
push(dummy, 1);
push(dummy, 2);
push(dummy, 3);
push(dummy, 4);
printf("栈顶元素:%d\n", top(dummy));
pop(dummy);
printf("栈顶元素:%d\n", top(dummy));
printf("栈是否为空:%d\n", empty(dummy));
print(dummy);
return 0;
}