C++day6

#include <iostream>

using namespace std;

class my_stack
{
private:
    int *num;
    int size;
    int len=0;
    int top;
public:
    my_stack(){};
    my_stack(int si):size(si),top(-1){
        num=new int[size];
    }
    ~my_stack(){}
    my_stack &operator=( int *x){
        for(int i=0;i<10;i++)
        {
        num[++top]=x[i];
        len++;
        }
        return *this;
    }
    void Top()
    {
        cout<<"栈顶元素是"<< num[top]<<endl;
    }
    void Empty(){
        if(len==0)
        {
            cout<<"栈为空"<<endl;
        }
        else
        {
            cout<<"栈不为空"<<endl;
        }
    }
    int Size()
    {
        return len;
    }
    void Push(int n)
    {
        num[++top]=n;
        len++;
    }
    void Pop()
    {
        top--;
        len--;
    }
    void show()
    {
        for(int i=0;i<len;i++)
        {
            cout<<num[i]<<"  ";

        }
        cout<<endl;
    }
};

int main()
{
    my_stack s1(50);
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    s1=a;
    s1.show();
    cout<<"容纳的元素个数为"<<s1.Size()<<endl;
    s1.Empty();
    s1.Top();
    s1.Push(666);
    s1.Pop();
    s1.show();
    return 0;
}

队列


#include <iostream>

using namespace std;

class my_stack
{
private:
    int *num;
    int size;
    int len=0;
    int top;
    int ptr;
public:
    my_stack(){};
    my_stack(int si):size(si),top(-1),ptr(0){
        num=new int[size];
    }
    ~my_stack(){}
    my_stack &operator=( int *x){
        for(int i=0;i<10;i++)
        {
        num[++top]=x[i];
        len++;
        }
        return *this;
    }
    void Top()
    {
        cout<<"链尾元素是"<< num[top]<<endl;
    }
    void Empty(){
        if(len==0)
        {
            cout<<"链为空"<<endl;
        }
        else
        {
            cout<<"链不为空"<<endl;
        }
    }
    int Size()
    {
        return len;
    }
    void Push(int n)
    {
        num[++top]=n;
        len++;
    }
    void Pop()
    {
        ptr++;
        len--;

    }
    void show()
    {
        for(int i=ptr;i<len+ptr;i++)
        {
            cout<<num[i]<<"  ";

        }
        cout<<endl;
    }
};

int main()
{
    my_stack s1(50);
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    s1=a;
    s1.show();
    cout<<"容纳的元素个数为"<<s1.Size()<<endl;
    s1.Empty();
    s1.Top();
    s1.Push(666);
    s1.show();
    s1.Pop();
    s1.show();
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值