继承 继承 继承 屁

1.栈
#include <iostream>

using namespace std;
class Stack
{
    int *ptr;
    int size;
public:
    //无参构造
    Stack():size(-1){
            ptr=new int[8];
            cout<<"无参构造"<<endl;
    }
    //有参构造
    Stack(int a):size(0){
            ptr=new int[8];
            ptr[0]=a;
            cout<<"有参构造"<<endl;
    }
    //赋值
    Stack& operator=(const int& a)
    {
        if(size<=7)
        {
            this->ptr[size+1]=a;
            size++;
            return *this;
        }else
        {
            cout<<"容器已满"<<endl;
            return *this;
        }
    }
    //访问栈顶元素
    Stack top()
    {
        if(size>-1)
        {
            cout<<ptr[size]<<endl;
        return ptr[size];
        }else
        {
            cout<<"条件不足"<<endl;
        }
        return -1;
    }
    //判空
    bool empty()
    {
        return size==-1;
    }
    //判满
    bool full()
    {
        return size==7;
    }
    //容器中元素个数
    int size_type()
    {
        if(empty())
        {
            return 0;
        }else
        {
            return size+1;
        }
    }
    //向栈顶添加数据
    void push(const int &a)
    {
        if(size<=7)
        {
            this->ptr[size+1]=a;
            size++;
            cout<<"添加成功"<<endl;
            return;
        }else
        {
            cout<<"容器已满"<<endl;
            return;
    }
    }
    //删除栈顶数据
    void pop()
    {
        if(empty())
        {
          cout<<"操作失败,容器为空"<<endl;
          return ;
        }
        ptr[size]=0;
        size--;
        cout<<"删除成功"<<endl;
        return ;
    }
    ~Stack(){delete ptr;ptr=NULL;cout<<"细狗函数"<<endl;}

};
int main()
{
    Stack s1;
    Stack s2(1);
    s2=2;
    s2.top();
    if(s1.empty())
     {
        cout<<"为空"<<endl;
    }else
    {
        cout<<"不为空"<<endl;
    }
    s1=1;
    if(s1.empty())
     {
        cout<<"为空"<<endl;
    }else
    {
        cout<<"不为空"<<endl;
    }
    cout<<"容器中有"<<s2.size_type()<<"位数"<<endl;
    s2.push(3);
    cout<<"容器中有"<<s2.size_type()<<"位数"<<endl;
    s2.pop();
    cout<<"容器中有"<<s2.size_type()<<"位数"<<endl;
    cout<<"栈顶元素为";
    s2.top();
    cout<<endl;



    return 0;
}
2.队列
#include <iostream>

using namespace std;
class queue
{
    int frist;
    int tail;
    int *ptr;
public:
    //无参构造
    queue():frist(0),tail(0){ptr=new int[8];}
    //有参构造
    queue(int a):frist(0),tail(1){ptr=new int[8];ptr[0]=a;}
    //细狗
    ~queue(){delete ptr;}
    //判空
    bool empty()
    {
        return frist==tail;
    }
    //判满
    bool full()
    {
        return (tail+1)%8==tail;
    }
    //访问第一个元素
    queue front()
    {
        if(empty())
        {
            cout<<"容器为空,没有数据";
            return -1;
        }
        cout<<ptr[frist];
        return ptr[frist];
    }
    //访问最后一个元素
    queue back()
    {
        if(empty())
        {
            cout<<"容器为空,没有数据";
            return -1;
        }
         cout<<ptr[tail-1];
        return ptr[tail-1];
    }
    //赋值给容器
    queue& operator=(const int &a)
    {
        if(full())
        {
            cout<<"容器已满"<<endl;
            return *this;
        }
        ptr[tail]=a;
        tail=(tail+1)%8;
        return *this;
    }
    //容器内元素个数
   int size()
   {
       if(empty())
       {
           cout<<"元素数量为0"<<endl;
           return 0;
       }
       cout<<"元素数量为"<<(tail-frist)%8<<endl;
        return (tail+frist);
   }
   void push(const int& a)
   {
       if(full())
       {
           cout<<"容器已满,插入失败"<<endl;
           return ;
       }
       ptr[tail]=a;
       tail=(tail+1)%8;
       cout<<"插入成功"<<endl;
   }
   void pop()
   {
           if(empty())
           {
               cout<<"删除失败,容器为空"<<endl;
               return ;
           }
           ptr[frist]=0;
           this->frist=(frist+1)%8;
           cout<<"删除成功"<<endl;
           return ;

   }
};
int main()
{
    queue s1(1);
    s1.push(2);
    s1=3;
    s1.size();
    s1.pop();
    s1.size();
    cout<<"第一个元素是";
    s1.front();
    cout<<endl;
    cout<<"最后一个元素是";
    s1.back();
    cout<<endl;
    s1.size();
    s1.pop();
    cout<<"第一个元素是";
    s1.front();
    cout<<endl;
    s1.push(4);
    cout<<"最后一个元素是";
    s1.back();
    cout<<endl;
    s1.size();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值