当前位置:网站首页>Simple example of class template

Simple example of class template

2022-06-30 06:14:00 Qiu cute

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;


template<class Type>
class Queue {
    
public:
    int real,length,maxSize;
    Type *q;
    Queue() {
    
        this->real= -1;
        this->length = 0;
        this->maxSize = 0;
        this->q = NULL;
    }
    Queue(int maxSize) {
    
        this->real = -1;
        this->length = 0;
        this->maxSize = maxSize;
        this->q = new Type(maxSize);
    }
    bool isFull() {
    
        return length == maxSize;
    }
    bool isEmpty() {
    
        return length == 0;
    }
    void add(Type t);
    Type del();
    void print(){
    
        for(int i = 0 ; i < length;i++){
    
            cout<<q[i]<<" ";
        }
    }
};
//Queue  Scope specified first  <> Specify the type 
template<class Type>
void Queue<Type>::add(Type t) {
    
    if(isFull()) {
    
        return ;
    }
    real = (real+1)%maxSize;
    length++;
    q[real] = t;
}
template<class Type>			
Type Queue<Type>::del() {
    		
    if(isEmpty()) {
    
        return;
    }
    int ft = (real+1+maxSize-length)%maxSize;
    length--;
    return q[ft];
}


int main() {
    
    Queue<int> q(5);
    q.add(1);
    q.print();
}

原网站

版权声明
本文为[Qiu cute]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160552050167.html