自学内容网 自学内容网

将之前实现的顺序表、栈、队列都更改成模板类

Queue.cpp

#include <iostream>
#include "Queue.hpp"
using namespace std;


int main()
{
    Queue <int>q;
    q.push(10);
    q.push(12);
    q.push(13);
    cout<<"q.front = "<<q.front()<<endl;
    q.pop();
    cout<<"q.back = "<<q.back()<<endl;
    q.show();
    cout<<"q.size = "<<q.get_size()<<endl;
    Queue<int> q1;
    q1=q;
    q1.show();


    Queue <double>q2;
    q2.push(1.1);
    q2.push(2.2);
    q2.push(3.3);
    cout<<"q.front = "<<q2.front()<<endl;
    q2.pop();
    cout<<"q.back = "<<q2.back()<<endl;
    q2.show();
    cout<<"q2.size = "<<q2.get_size()<<endl;
    Queue<double> q3;
    q2=q3;
    q3.show();
//    Queue <string>q4;
//    q4.push("1.1");
//    q4.push("2.2");
//    q4.push("3.3");
//    cout<<"q.front = "<<q4.front()<<endl;

    return 0;
}

Queue.hpp

#ifndef QUEUE_HPP
#define QUEUE_HPP


template <typename T>
class Queue
{
private:
    int len;
    T *data; //容器
    int size=15; //最大容量


public:
    Queue()
    {
        this->len = 0;

        data = new T [this->size];
        int i = 0;
        while (i<this->size) {
            this->data[i]=0;
            i++;
        }
    }
    ~Queue()
    {
        delete []data;
    }
    Queue &operator=(const Queue &other)
    {
        int i =0;
        if(this!=&other)
        {
            this->len=other.len;
            this->size = other.size;

            while( i<other.len){
                data[i] = other.data[i];
                i++;
            }
            data[other.len]='\0';

        }
        return *this;
    }

    void push(T value)
    {
        if(this->len>=this->size)
        {

            junzi();
        }
        data[this->len++] = value;
    }
    void pop()
    {
        int i = 0;
        while(i<this->len)
        {
            data[i] = data[i+1];
            i++;
        }
        this->len--;
    }

    int get_size()
    {
         return this->size;
    }

    bool empty()
    {
        return this->data==nullptr||this->len==0;
    }

    T front()
    {
        if(empty())
        {
            return 0;
        }
        else{
            return data[0];
        }
    }

    T back()
    {
        if(empty())
        {
            return 0;
        }
        else{
            return data[this->len-1];
        }
    }

    void show()
    {
        int i = 0;
        while(i<this->len)
        {
            //cout<<data[i]<<" ";
            i++;
        }
        //cout<<endl;
    }

    void junzi()
    {
        T *temp = new T[this->size*2];
        int i=0;
        while(i<this->len)
        {
            temp[i] = data[i];
            i++;
        }
        delete []data;
        this->data= temp;//是this->data改变
        this->size *=2;

    }

};


#endif // QUEUE_HPP

Stack.cpp

#include <iostream>
#include"Stack.hpp"
using namespace std;

int main()
{
    my_stack<int> s1;
    s1.push(10);
    s1.push(11);
    cout<<s1.get_size()<<endl;

    s1.pop();
    cout<<"top "<<s1.top()<<endl;

    my_stack<double> s2;
    s2.push(1.1);
    s2.push(1.2);
    cout<<s2.get_size()<<endl;

    s2.pop();
    cout<<"top "<<s2.top()<<endl;

//    my_stack<string> s3;
//    s3.push("nihao");
//    s3.push("hello");
//    cout<<s3.get_size()<<endl;

//    s3.pop();
//    cout<<"top "<<s3.top()<<endl;

    return 0;
}

Stack.hpp

#ifndef STACK_HPP
#define STACK_HPP
#include<exception>
template <typename T>
class my_stack{
private:
    int len;
    int size;
    T *data;

public:
    my_stack(){

        data = new T [this->size];
        this->len=0;
        this->size=15;

    }


    ~my_stack(){

        delete [] data;

    }
    my_stack &operator=(const my_stack&other )
    {
        if(this!=&other)
        {
            delete [] data;
            this->len = other.len;
            data = new T[size];//深拷贝
            int i =0;
            while(i<this->len)
            {
                data[i]=other.data[i];
            }
        }
        return *this;
    }

    T top()
    {
        return data[this->len-1];
    }
    bool empty()
    {
        return this->len ==0;
    }

    int get_size(){
        return this->size;
    }

    void push(T value){
        if(this->len>this->size-1)
        {
            return ;

        }
        data[this->len++]= value;
    }

    void pop()
    {
        if(empty())
        {
            return ;
        }
        this->len--;
    }

};




#endif // STACK_HPP

思维导图


原文地址:https://blog.csdn.net/qy3333/article/details/142603468

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!