C++ - day3

1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量

成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

头文件

#ifndef TEXT_H
#define TEXT_H
#include <iostream>

using namespace std;
class zn
{
private:
    int *num;
    int top=-1;
public:
    //无参构造函数
    zn();
    //有参构造函数
    zn(int p);
    //析构函数
    ~zn();
    //拷贝构造函数
    zn(const zn &other);

    //判空
    bool myemp();
    //判满
    bool myfull();
    //入栈
    int myinput();
    //出栈
    int myoutput();
    //获取栈顶元素
    int gettop();
    //清空栈
    int myclear();
    //求栈的大小
    void mysize();
    void show();
};
#endif // TEXT_H

函数文件

#include"text.h"
//无参构造函数
zn::zn():num(new int[5]){}
//有参构造函数
zn::zn(int p):top(p){
    num=new int[5];
    for(int i=0;i<=this->top;i++)
    {
        cin>>num[i];
    }
}
//析构函数
zn::~zn()
{
    delete []num;
}
//拷贝构造函数
zn::zn(const zn &other):top(other.top){
    num=new int[5];
    for(int i=0;i<=other.top;i++)
    {
        num[i]=other.num[i];
    }
}

//判空
bool zn::myemp()
{
    if(top==-1)
    {
        return true;
    }
    return false;
}
//判满
bool zn::myfull()
{
    if(top==5)
    {
        return true;
    }
    return false;
}
//入栈
int zn::myinput()
{
   if(myfull())
   {
       cout<<"入栈失败"<<endl;
       return 0;
   }
   top+=1;
   cin>>num[top];
   cout<<"入栈成功"<<endl;
   return 1;
}
//出栈
int zn::myoutput()
{
   if(myemp())
   {
       cout<<"出栈失败"<<endl;
       return 0;
   }
   top-=1;
   cout<<"出栈成功"<<endl;
   return 1;
}
//获取栈顶元素
int zn::gettop()
{
    if(myemp())
    {
        cout<<"获取失败"<<endl;
        return 0;
    }
    cout<<"栈顶元素为:"<<num[top]<<endl;
    return 1;
}
//清空栈
int zn::myclear()
{
   if(myemp())
   {
       return 0;
   }
   this->top=-1;
   cout<<"清空成功"<<endl;
   return 1;
}
//求栈的大小
void zn::mysize()
{
    cout<<"栈的大小为:"<<top+1<<endl;
}
void zn::show()
{
    for(int i=0;i<=(top);i++)
    {
        cout<<num[i]<<endl;
    }
}

测试文件

#include <iostream>
#include "text.h"
using namespace std;

int main()
{
    //无参构造
    zn a1;
    a1.myinput();
    a1.myinput();
    a1.myinput();
    a1.myinput();
    a1.gettop();
    a1.myoutput();
    //拷贝构造
    zn a2=a1;
    a2.show();
    a2.mysize();
    cout<<"**************"<<endl;
    a1.gettop();
    a1.show();
    cout<<"**************"<<endl;
    a1.myclear();
    a1.gettop();
    a1.mysize();
    return 0;
}

2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置

成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

头文件

#ifndef TEXT_H
#define TEXT_H
#include <iostream>

using namespace std;
class dl
{
private:
    int *num;
    int tail=0;     //队尾
    int head=0;    //队头
public:
    //无参构造
    dl();
    //拷贝构造
    dl(const dl &other);
    //析构函数
    ~dl();

    //入队
    void input();
    //出队
    void output();
    //清空队列
    void clear();
    //判空
    bool myemp();
    //判满
    bool myfull();
    //求队列的大小
    void mysize();
    void show();
};
#endif // TEXT_H

函数文件

#include"text.h"
//无参构造
dl::dl():num(new int[10]){};
//拷贝构造
dl::dl(const dl &other):tail(other.tail),head(other.head)
{
    num=new int[10];
    int i=other.head;
    do
    {
        num[i]=other.num[i];
        i=(i+1)%10;
    }while(i!=other.tail);
}
//析构函数
dl::~dl(){
    delete []num;
}

//入队
void dl::input()
{
    if(myfull())
    {
        cout<<"队列满啦"<<endl;
    }else
    {
        cin>>num[tail];
        tail=(tail+1)%10;
    }
}
//出队
void dl::output()
{
    if(myemp())
    {
        cout<<"队列无元素"<<endl;
    }else
    {
        cout<<num[head]<<"出队成功"<<endl;;
        head=(head+1)%10;
    }
}
//清空队列
void dl::clear()
{
    head=tail;
    cout<<"清空成功"<<endl;
}
//判空
bool dl::myemp()
{
    if(head==tail)
    {
        return true;
    }
    return false;
}
//判满
bool dl::myfull()
{
    if((tail+1)%10==head)
    {
        return true;
    }
    return false;
}
//求队列的大小
void dl::mysize()
{
    cout<<"队列大小为"<<(tail-head+10)%10<<endl;
}
void dl::show()
{
    cout<<"队列元素为:";
    int i=head;
    do
    {
        cout<<num[i]<<" ";
        i=(i+1)%10;
    }while(i!=tail);
    cout<<endl;
}

测试文件

#include <iostream>
#include "text.h"
using namespace std;

int main()
{
    dl s1;
    s1.input();
    s1.input();
    s1.input();
    s1.show();
    s1.output();
    s1.input();
    s1.input();
    s1.show();
    s1.input();
    s1.mysize();
    dl s2(s1);
    s2.show();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值