stack

#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;  // 栈空则返回true
 int top() const; // 返回栈顶元素,如果栈为空则返回0
 bool pop();  
 bool push(int _number); // 请注意push失败的情况,即new失败
 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  // 栈空则返回true
    {
        if(mtop==NULL)
            return true;
        return false;
    }
    int myStack::top() const // 返回栈顶元素,如果栈为空则返回0
    {
        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)// 请注意push失败的情况,即new失败
    {
        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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值