C++ day6

1. Implement the previously defined stack class and queue class into template classes

1) stack

#include <iostream>
#include <cstring>

using namespace std;

template<typename T>
class Stack
{
private:
    T *p = nullptr;      //存储栈的数组
    int top;               //栈顶元素的下标
    int max;

public:

    //定义析构函数
    ~Stack()
    {
        delete []p;              //释放成员指针的空间

        cout<<"Stu::析构函数:"<<this<<endl;
    }

    //定义拷贝构造函数
    Stack(const Stack &other):p(new int[other.max]), top(other.top)
    {
        memcpy(p, other.p, sizeof(int)*other.max);
        cout<<"拷贝构造函数"<<endl;
    }

    //初始化
    Stack(T size)
    {
        max = size;
        p = new T[max];       //构造函数
        top = -1;
    }

    //判空
    bool stack_empty()
    {
        if(top==-1)
        {
            cout<<"栈为空"<<endl;
            return 1;
        }
        return 0;
    }

    //判满
    bool stack_full()
    {
        if(top==max-1)
        {
            cout<<"栈已满"<<endl;
            return 1;
        }
        return 0;
    }

    //入栈
    int stack_push(T e)
    {
        if(stack_full())
        {
            cout<<"入栈失败"<<endl;
            return -1;
        }
        top++;
        p[top]=e;
        cout<<"入栈成功"<<endl;
        return 0;
    }

    //出栈
    int stack_pop()
    {
        if(stack_empty())
        {
            cout<<"出栈失败"<<endl;
            return -1;
        }
        T e=p[top];
        top--;
        cout<<e<<"出栈成功"<<endl;
        return 0;
    }

    //获取栈顶元素
    int stack_top()
    {
        cout<<"栈顶元素是:"<<p[top]<<endl;
        return 0;
    }

    //求栈的大小
    int stack_getsize()
    {
        return top+1;
    }

    //清空栈
    int stack_clear()
    {
        top = -1;
        p = nullptr;

        cout<<"清空成功"<<endl;
        return 0;
    }
};

int main()
{
    //设置栈存储的最大值max
    int m;
    cout<<"设置栈的大小"<<endl;
    cin>>m;

    Stack<double> s1(m);
    double e;
    int n;

    //判空
    s1.stack_empty();

    //入栈
    cout<<"请输入要入栈的个数:";
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cout<<"请输入要入栈的元素:";
        cin>>e;
        s1.stack_push(e);
    }
    s1.stack_top();
    s1.stack_getsize();

    cout<<endl;
    s1.stack_clear();

    return  0;
}

2) Queue

#include <iostream>
#define MAX 128

using namespace std;

template<typename T>
class Queue
{
private:
    T *p;         //队列的数组
    int tail;       //记录队尾元素
    int head;       //记录对头元素
    int max;

public:

    //初始化
    Queue(T size)
    {
        max = size;
        p = new T[max];       //构造函数
        head = 0;
        tail = 0;
    }

    //定义析构函数
    ~Queue()
    {
        delete []p;              //释放成员指针的空间

        cout<<"Stu::析构函数:"<<this<<endl;
    }
    //拷贝构造函数
    Queue(const Queue &other):tail(other.tail),head(other.head)
    {
        memcpy(p, other.p, sizeof(int)*other.max);
        cout<<"拷贝构造函数"<<endl;
    }

    //判空
    bool queue_empty()
    {
        if(head==tail)
        {
            cout<<"队列为空"<<endl;
            return 1;
        }
        return 0;
    }

    //判满
    bool queue_full()
    {
        if(((tail+1)%max)==0)
        {
            cout<<"队列已满"<<endl;
            return 1;
        }
        return 0;
    }

    //入队
    int queue_push(int e)
    {
        if(queue_full())
        {
            cout<<"入队失败"<<endl;
            return -1;
        }
        p[tail] = e;
        tail = (tail+1)%max;
        cout<<"入队成功"<<endl;
        return 0;
    }

    //出队
    int queue_pop()
    {
        if(queue_empty())
        {
            cout<<"出队失败"<<endl;
            return -1;
        }
        int e = p[head];
        head = (head+1)%max;
        cout<<e<<"出队成功"<<endl;
        return 0;
    }

    //队列的大小
    void queue_getsize()
    {
        int size;
        size = (tail-head+max)%max;
        cout<<"队的大小为:"<<size<<endl;
    }

    //清空队列
    void queue_clear()
    {
        head = tail = 0;
        p = nullptr;
        cout<<"清空队列成功"<<endl;

    }
};

int main()
{
    //设置队列存储的最大值max
    int m;
    cout<<"设置队列的大小"<<endl;
    cin>>m;

    Queue<double> q1(m);
    double e;
    int n;

    cout<<"请输入要入队的个数:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cout<<"请输入要入队的元素:";
        cin>>e;
        q1.queue_push(e);
    }
    q1.queue_getsize();

    cout<<endl;
    q1.queue_clear();

    return 0;
}

2. Mind map

Guess you like

Origin blog.csdn.net/Lychee_z23/article/details/132864683