栈的链式存储
作者:
zzu
,
2024-03-27 20:48:08
,
所有人可见
,
阅读 2
#include <iostream>
using namespace std;
const int N = 100010;
struct Node {
int val;
Node *next;
Node (): next(NULL){}
Node(int _val): val(_val), next(NULL){}
};
void print(Node *top)
{
for(auto p = top->next; p; p = p->next)
{
cout << p->val << " ";
}cout << endl;
}
int main()
{
Node *top = new Node();
auto a = new Node(1);
a->next = top->next;
top->next = a;
print(top);
cout << "栈顶是" << top->next->val << endl;
auto b = new Node(2);
b->next = top->next;
top->next = b;
print(top);
cout << "栈顶是"<< top->next->val << endl;
top->next = top->next->next;
delete b;
print(top);
cout << "栈顶是" << top->next->val << endl;
top->next = top->next->next;
delete a;
if(top->next == NULL)
cout << "栈为空" << endl;
else
cout << "栈不为空" << endl;
return 0;
}