C++--day6

将之前定义的栈类和队列类都实现成模板类

栈:

#include 

using namespace std;
template
class zn
{
private:
    T *num;
    int top;
    int size;
public:
//有参构造函数
zn(int a):top(-1),size(a){
    num=new T[size];
}
//析构函数
~zn()
{
    delete []num;
    num=NULL;
}

//判空
bool myemp()
{
    if(top==-1)
    {
        return true;
    }
    return false;
}
//判满
bool myfull()
{
    if(top==size)
    {
        return true;
    }
    return false;
}
//入栈
int myinput()
{
   if(myfull())
   {
       cout<<"入栈失败"<>num[top];
   cout<<"入栈成功"<top=-1;
   cout<<"清空成功"< a1(5);
    a1.myinput();
    a1.myinput();
    a1.myinput();
    a1.myinput();
    a1.gettop();
    a1.myoutput();
    a1.show();
    a1.myclear();
    a1.gettop();
    a1.mysize();
    return 0;
}

C++--day6_第1张图片

队列

#include 

using namespace std;
template
class dl
{
private:
    T *num;
    int tail=0;     //队尾
    int head=0;    //队头
public:
    //无参构造
    dl():num(new T[10]){}
    //拷贝构造
    dl(const dl &other):num(new int[10]),tail(other.tail),head(other.head)
    {
        int i=other.head;
        do
        {
            num[i]=other.num[i];
            i=(i+1)%10;
        }while(i!=other.tail);
    }
    //析构函数
    ~dl(){
        delete []num;
        num=NULL;
    }

    //入队
    void input()
    {
        if(myfull())
        {
            cout<<"队列满啦"<>num[tail];
            tail=(tail+1)%10;
        }
    }
    //出队
    void output()
    {
        if(myemp())
        {
            cout<<"队列无元素"< s1;
    s1.input();
    s1.show();
    return 0;
}

C++--day6_第2张图片

你可能感兴趣的:(c++,算法,开发语言)