9.3C++

思维导图

作业

自己封装 栈和队列

#include <iostream>

using namespace std;
class mystack
{
private:
    int *data;
    int top;
public:
    mystack():data(NULL),top(-1){}
    ~mystack(){delete[] data;}
    mystack & operator=(const mystack &R)
    {
        if(this!=&R)
        {
            delete []this->data;
            this->data=new int[R.top+1];
            for(int i=0;i<=R.top;i++)
            {
                *(this->data+i)=*(R.data+i);
            }
            this->top=R.top;
        }

        return *this;
    }
    bool empty()
    {
        return top==-1;
    }
    int size()
    {
        return top+1;
    }
    bool push(int e)
    {
        int *temp=new int[top+2];
        for(int i=0;i<=top;i++)
        {
            *(temp+i)=*(data+i);
        }
        top++;
       *(temp+top)=e;
        if(top>0)
        {
            delete []data;
        }
        data=temp;
        return true;
    }
    bool pop()
    {
        *(data+top)='\0';
        top--;
        return true;
    }
    void show()
    {
        for(int i=top;i>-1;i--)
        {
            cout<<"第"<<i<<"个元素为:"<<data[i]<<endl;
        }

    }
};
int main()
{
    mystack a;
    a.push(3);
    a.show();
    a.push(4);
    a.show();
    a.push(2);
    a.show();
    a.push(1);
    a.show();
    mystack b;
    b.push(4);
    b.push(7);
    b.show();
    cout<<b.size()<<endl;
    b=a;
    b.show();
    cout<<b.size()<<endl;
    return 0;
}

队列

#include <iostream>

using namespace std;
class myqueue
{
private:
    int* data;
    int front;
    int tail;
public:
    myqueue():data(NULL),front(0),tail(0){}
    ~myqueue(){delete[]data;}
    myqueue & operator=(const myqueue &R)
    {
        delete []this->data;
        this->data=new int[R.tail+1];
        for(int i=0;i<=R.tail;i++)
        {
            *(this->data+i)=*(R.data+i);
        }
        this->tail=R.tail;
        this->front=R.front;
        return *this;
    }
    int &Front()
    {
        return *(data+front);
    }
    int &Tail()
    {
        return *(data+tail-1);
    }
    bool empty()
    {
        return front==tail;
    }
    int size()
    {
        return tail-front;
    }
    bool push(int e)
    {
        int *temp=new int[tail+2];
        for(int i=0;i<tail;i++)
        {
            *(temp+i)=*(data+i);
        }
        *(temp+tail)=e;
        tail++;
        if(tail>1)
        {
            delete[]data;
        }
        data=temp;
        return true;
    }
    bool pop()
    {
        *(data+front)='\0';
        front++;
        return true;
    }
    void show()
    {
        for(int i=front;i<tail;i++)
        {
            cout<<"第"<<i<<"个元素为:"<<data[i]<<endl;
        }
    }
};
int main()
{
    myqueue a;
    a.push(2);
    a.show();
    a.push(4);
    a.push(7);
    a.show();
    a.pop();
    a.show();
    a.pop();
    a.show();
    a.push(9);
    a.show();
    cout<<a.size()<<endl;
    a.Front()=1;
    a.show();
    a.Tail()=10;
    a.show();

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值