这只是个很基础的练习,实现了链栈的后进先出功能,纯属无聊。
#include<memory>
#include<iostream>
#include<string>
using namespace std;
class linkstacknode
{
public:
int data;
linkstacknode* next;
linkstacknode(){ next = nullptr; }
};
class linkstack
{
public:
linkstacknode *top;
int count;
linkstack(){ count = 0; top = nullptr; }
void push(int e);
void pop(int& e);
};
void linkstack::push(int e)
{
linkstacknode *temp = new linkstacknode();
temp->data = e;
temp->next = top;
top = temp;
count++;
}
void linkstack::pop(int& e)
{
if (count==0)
{
return;
}
e = top->data;
count--;
linkstacknode *tem = top;
top = top->next;
delete tem;
}
int main()
{
int a;
linkstack lys;
cin >> a;
while (a)
{
lys.push(a);
cin >> a;
}
cout <<"size:"<< lys.count << endl;
while (lys.count)
{
lys.pop(a);
cout << a;
cout << " current size:"<<lys.count << endl;
}
return 0;
}