#include <iostream>
#include <cstdio>
using namespace std;
struct Node {
int number;
Node *next;
};
class myStack {
private:
Node *mtop;
int _size;
public:
myStack();
myStack(const myStack &stack);
myStack(const int *arr, const int _size);
~myStack();
bool empty() const;
int top() const;
bool pop();
bool push(int _number);
int size() const;
myStack& operator=(const myStack& stack);
friend ostream& operator<<(ostream& out, const myStack& stack) {
Node *t = stack.mtop;
if (t == NULL) {
out << "The stack is empty!";
return out;
}
while (t != NULL) {
out << t->number << ' ';
t = t->next;
}
return out;
};
};
using namespace std;
int main() {
int a[100];
int n, m, num;
cin >> n >> m;
myStack s1;
cout << "s1 is empty? " << s1.empty() << endl;
for (int i = 0; i < n/2; ++i) {
cin >> num;
s1.push(num);
}
cout << "s1's size is " << s1.size() << endl;
myStack s2(s1), s3;
for (int i = 0; i < m; ++i) {
s1.pop();
}
cout << "now s1's size is " << s1.size() << endl;
for (int i = n/2; i < n; ++i) {
cin >> num;
s1.push(num); s3.push(num);
}
cout << "s1's size is " << s1.size() << endl;
cout << "s1 is " << s1 << endl;
cout << "s3 is " << s3 << endl;
cout << "s2's top is " << s2.top() << endl;
s2.pop();
cout << "s2 is " << s2 << endl;
cout << "s2 is empty?" << s2.empty() << endl;
s2 = s1;
cout << "now s2 is " << s2 << endl;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
myStack s4(a, n);
cout << "s4's size is " << s4.size() << endl;
cout << "s4 is " << s4 << endl;
s3 = s4; s3.pop();
cout << "s3 is " << s3 << endl;
return 0;
}
myStack::myStack()
{
mtop=NULL;
_size=0;
}
myStack::myStack(const myStack &stack)
{
*this=stack;
}
myStack::myStack(const int *arr, const int _size)
{
for(int i=0;i<_size;++i)
push(arr[i]);
}
myStack::~myStack()
{
delete mtop;
}
bool myStack::empty() const
{
if(mtop==NULL)
return true;
return false;
}
int myStack::top() const
{
if(_size)
return mtop->number;
return 0;
}
bool myStack::pop()
{
if(_size)
{
mtop=mtop->next;
--_size;
return true;
}
return false;
}
bool myStack::push(int _number)
{
if(mtop==NULL)
{
++_size;
mtop=new Node;
mtop->next=NULL;
mtop->number=_number;
return true;
}
++_size;
Node *temp=new Node;
temp->number=_number;
temp->next=mtop;
mtop=temp;
return true;
}
int myStack::size() const
{
return _size;
}
myStack& myStack::operator=(const myStack& stack)
{
while(mtop!=NULL)
mtop=mtop->next;
this->_size=stack._size;
Node *temp=stack.mtop;
int a[1000000];
int i=0;
while(temp!=NULL)
{
a[i++]=temp->number;
temp=temp->next;
}
for(int j=i-1;j>=0;--j)
push(a[j]);
return *this;
}