c++基础练习---实现vector类和queue类(2023-4-5)

本文展示了如何使用C++实现模板类Myvector,具备动态数组的功能,包括构造、拷贝、赋值、访问元素、判断空满、增删元素等操作。同时,还实现了Queue模板类,支持队列的基本操作如入队、出队和显示队列内容,当队列满时能给出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现vector

#include <iostream>

using namespace std;
template <typename T>
class Myvector
{
private:
    T *first;
    T *last;
    T *end;
public:
    //无参构造函数
    Myvector(){};
    Myvector(int s):first(new T(s))
    {
        last = first;
        end = first+s;
    }
    //析构函数
    ~Myvector(){};
    //拷贝构造
    Myvector(const Myvector<T> &other)
    {
        int len = other.last-other.first;
        int size = other.end - other.first;
        this->first=new T[size];
        memcpy(this->first,other.first,len*sizeof(T));
        this->last=this->first+len;
        this->end = this->first+size;
    }
    Myvector & operator=(const Myvector<T> & other)
    {
           this->first = other.first;
           this->last = other.last;
           this->end = other.end;
       }

       T &at(int n)
       {
           int size = last-first;
           if(n > size || n < 0)
           {
               return NULL;
           }
           return first[n];
       }

       bool empty()
       {
           if(this->first == this->last)
           {
                return 1;
               }
           else
           {
               return 0;
           }
       }

       bool full()
       {
           if(this->end == this->last)
           {
                return 1;
               }
           else
           {
               return 0;
           }
       }

       T &front()const
       {
           return *first;
       }

       T &back()const
       {
           return *(last-1);
       }

       int size()
       {
           return last-first;
       }

       void clear()
       {
           last = first;
       }

       void expandsize()
       {
           int size = this->end - this->first;

           T *temp = new T[2*size];

           memcpy(temp, this->first, size*sizeof(T));

           delete []first;

           first = temp;
           last = first+size;
           end = first+2*size;
       }

       void push_back(const T val)
       {
           if(this->full())
           {
               this->expand();
           }
           *last = val;
           last++;
       }

       void pop_back()
       {
           if(this->empty())
           {
               return;
           }
           --last;
       }

       void display()
       {
           T *temp= first;
           while(temp < last)
           {
               cout << *temp << " ";
               temp++;
           }
           cout << endl;
       }
};

实现queue

#include <iostream>

using namespace std;

template <typename T>
class Queue{
private:
    int head;
    int tail;
    T *s;
    int len;
public:
    Queue(){}
    Queue(int h):s(new T(h)), len(h){
        this->head = 0;
        this->tail = 0;
    }
    void QuereAdd(T q){
        int n = (tail+len-head)%len;

        if(n < len - 1){
            *(s+tail) = q;
            tail = (tail+1)%len;
            return ;
        }
        cout << "队列满" << endl;
    }
    void QueueDelete(){
        head += 1;
    }
    void show(){
        int first = head;
        while(first != tail){
            cout << *(s+first) << " ";
            first = (first+1)%len;
        }
        cout << endl;
    }
};

int main()
{
    Queue<int> Q1(5);
    Q1.QuereAdd(1);
    Q1.QuereAdd(2);
    Q1.QuereAdd(3);
    Q1.show();
    Q1.QueueDelete();
    Q1.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值