C++周末作业

1、顺序队列

代码

#include <iostream>
#define Max 20

using namespace std;
class Seqlist{
private:
    int *data;
    int head;
    int tail;
public:
   
    //创建队列
    void seque_create()
    {
        data = new int[Max];
        head = 0;
        tail = 0;
    }
    //判空
    bool seque_empty()
    {
        return head==tail;
    }

    //判满
    bool seque_full()
    {
        return (tail+1)%Max == head;
    }
    //入队
    void seque_insert()
    {
        if(seque_full())
        {
            cout<<"队列已满"<<endl;
            return ;
        }
        int num;
        cout<<"输入>>>";
        cin>>num;
        data[tail] = num;
        tail = (tail+1)%Max;

    }
    //出队
    void seque_out()
    {
        if(seque_empty())
        {
            cout<<"队列已空"<<endl;
            return;
        }
        cout << data[head] <<"出队成功" <<endl;
        head = (head+1)%Max;
    }
    //遍历
    void seque_show()
    {
        if(seque_empty())
        {
            cout<<"队列已空"<<endl;
            return;

        }
        for(int i = head ; i!=tail;i=(i+1)%Max)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }
    //队列长度
    void seque_length()
    {
        if(seque_empty())
        {
            cout<<"队列已空"<<endl;
            return;
        }
        cout << "seqlist size = " << (tail+Max-head)%Max << endl;
    }
    //删除
    void seque_delete()
    {
        delete []data;
        data = nullptr;
        cout<<"删除成功"<<endl;
    }
};

int main()
{
    Seqlist q;
    q.seque_show();
    q.seque_insert();
    q.seque_insert();
    q.seque_length();
    q.seque_full();
    q.seque_show();
    q.seque_out();
    q.seque_out();
    q.seque_show();
    q.seque_delete();
    cout << "Hello World!" << endl;
    return 0;
}

vector:

#include <iostream>
using namespace std;

template <typename T>
class My_vector{
private:
    T *first;
    T *last;
    T *end;
public:
    My_vector(){      //无参构造
        first = last = end = nullptr;
    }
    My_vector(int num,const T &value)    //有参构造
    {

        first = new T[num+1];
        last = first;
        for(int i=0;i<num;i++)
        {
            first[i]=value;
            last++;
        }
        end = last-1;
    }
    My_vector(const My_vector &other)   //拷贝构造
    {
        int size = other.last-other.first;
        first = new T[size+1];
        for(int i=0;i<size;i++)
        {
            first[i]=other.first[i];
            last++;
        }
        end = last-1;
    }
    My_vector &operator=(const My_vector &other)     //拷贝赋值
    {
        int size = other.last-other.first;
        first = new T[size+1];
        for(int i=0;i<size;i++)
        {
            first[i]=other.first[i];
            last++;
        }
        end = last-1;
        return *this;
    }

    ~My_vector() //析构函数
    {
    delete []first;
    }

    T &My_at(T pos)    //读取指定位置的字符
    {
        if(last == first)
        {
          throw out_of_range("An error occurred");
        }
        return first[pos];
    }
    bool My_empty()     //判空
    {
        return  first==last;
    }

    bool My_full()      //判满
    {
        return end==last;
    }
    T My_front()     //返回第一个元素
    {
        if(My_empty())
        {
            return ;
        }
        return first[0];

    }
    T My_back()      //返回最后一个元素
    {
        if(My_empty())
        {
            return ;
        }
        return *(last-1);
    }
    int My_size()     //返回vector元素数量的大小
    {
        if(My_empty())
        {
            return 0;
        }
        return last-first;
    }
    void My_clear()     //清空vector中的所有元素
    {
        if(My_empty())
        {
            return ;
        }
        last = first;
        end = first;
    }

    void My_expand()    //二倍扩容
    {
        T *temp;
        int size = last-first;
        temp = new T[size*2];
        for(int i=0;i<size;i++)
        {
            temp[i]=first[i];
            cout<<temp[i]<<endl;
        }
        delete []first;
        first = temp;
        last = first+size;
        end = first+size*2-1;
    }
    void My_push_back(const T &value)    //尾插
    {
        if(end == first)
        {
            T *temp=new T[2];
            first = temp;
            last = first;
            end=first+1;

        }
        else if(My_full())
        {
            My_expand();
        }
        *(last++) = value;
    }
    void My_pop_back()     //尾删
    {
        last--;
    }
    int My_capacity()    //当前最大容量
    {
        return end-first;
    }
};
int main()
{
    My_vector<int> v1;
    cout<<"v1.size = "<<v1.My_size()<<endl;
    cout<<"v1.capacity = "<<v1.My_capacity()<<endl;
    for(int i=0;i<10;i++)
    {
        v1.My_push_back(i);
        cout<<"v1 show ="<<v1.My_at(i)<<" ";
    }
    cout<<endl;
    cout<<"v1.size = "<<v1.My_size()<<endl;
    v1.My_pop_back();
    v1.My_size();

    cout<<endl;
    return 0;



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值