手动实现封装栈、队列、vector模板类

stack

#include <iostream>

using namespace std;

template <typename T>
class My_Stack
{
private:
    T *data;
    int top;
    int capacity;
public:
    My_Stack():data(NULL),top(-1) {}     //无参构造
    My_Stack(int c):capacity(c)  //有参构造
    {
        data=new T[c] ();
        top=-1;
    }

    ~My_Stack()                         //析构函数
    {
        delete [] data;
    }

    bool empty()                        //判空
    {
        if(-1==top)
            return true;
        else
            return false;
    }

    bool full()                         //判满
    {
        if(top==capacity-1)
            return true;
        else
            return false;
    }

    bool push(const T &value)           //入栈操作
    {
        if(full())
            return false;
        else{
            top++;
            data[top]=value;
        }
        return true;
    }

    bool pop(T &value)                          //出栈操作
    {
        if(empty())
            return false;
        value = data[top];
        top--;
        return true;
    }
    void show()									//打印所有栈成员
    {
        while (top!=-1) {
            cout<<data[top]<<endl;
            top--;
        }
    }
};

int main()
{
    My_Stack<char> s(5);
    if(s.empty())
    {
        cout<<"栈空"<<endl;
    }
    s.push('a');
    s.push('b');
    s.push('c');
    s.push('d');
    s.push('e');
    if(s.full())
    {
        cout<<"栈满"<<endl;
    }
    char a;
    char &r=a;
    s.pop(r);
    cout<<a<<"出栈了"<<endl;
    s.show();
    return 0;
}

queue

#include <iostream>


using namespace std;

template <typename T>

class My_Queue
{
private:
    T *data;
    int head;
    int tail;
    int capacity;
public:
    My_Queue():data(NULL) {}            //无参构造

    My_Queue(int c):capacity(c)       //有参构造
    {
        data=new T[c] ();
        head=tail=0;
    }

    ~My_Queue()                         //析构函数
    {
        delete [] data;
    }

    bool empty()                        //判空
    {
        if(head==tail)
            return true;
        else
            return false;
    }

    bool full()                         //判满
    {
        if((tail+1)%capacity==head)
            return true;
        else
            return false;
    }

    bool push(const T &value)           //入队操作
    {
        if(full())

            return false;
        else{
            data[tail]=value;
            tail=(tail+1)%capacity;
        }
        return true;
    }

    bool pop(T &value)                  //出队操作
    {
        if(empty())
            return false;
        value=data[head];
        head=(head+1)%capacity;
        return true;
    }

    int sise(My_Queue q)                //求队列长度
    {
        return (q.tail-q.head+capacity)%capacity;
    }

    void show()                         //输出队列成员
    {
        while(head!=tail)
        {
            cout<<data[head]<<endl;
            head++;
        }
    }

};
int main()
{
    My_Queue<int> q(5);
    if(q.empty())
        cout<<"队列为空"<<endl;
    q.push(1);

    q.push(2);

    q.push(3);

    q.push(4);

    cout<<q.sise(q)<<endl;
    if(q.full())
        cout<<"队列满"<<endl;
    int a;
    int &r=a;
    q.pop(r);
    cout<<a<< "出队"<<endl;
    if(q.full())
        cout<<"队列满"<<endl;
    int size;
    size=q.sise(q);
    cout<<"size = "<<size<<endl;
    q.show();
    return 0;
}

vector

#include <iostream>

using namespace std;
template <typename T>
class myvector
{
private:
    T *first;
    T *last;
    T *end;
public:
    //构造
    myvector(int size =2)
    {
        first = new T[size];

        last = first;
        end = first + size;
    }
    //析构
    ~myvector()
    {
        delete  []first;
        first = last = end =nullptr;

    }
    //拷贝构造
    myvector(const myvector &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;
    }
    //判满
    bool full()
    {
        return this->last == this->end;
    }
    //判空
    bool empty()
    {
        return this->last == this->first;
    }
    //扩容
    void doulb()
    {
        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->doulb();
        }
        *last = val;
        last++;
    }
    //尾删
    void pop_back()
    {
        if(this->empty())
        {
            return;
        }

        last--;
    }
    T &front()const
    {
        return first;
    }
    int size()
    {
        return end-first;
    }
    int len()
    {
        return last-first;
    }
    T &at(int index)
    {
        if(index<0||index>this->len())
        {
            cout<<"越界了"<<endl;
        }
        return first[index];
    }
};


int main()
{
  myvector<int> a;
  a.push_back(77);
  for(int i=1;i<=10;i++)
  {
    a.push_back(i);
    cout<<a.size()<<" ";
  }
  cout<<endl;
  for(int i=0;i<=10;i++)
  {

    cout<<a.at(i)<<" ";
  }
  cout<<endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值