0830 练习

#include <iostream>

#define MAX_SIZE 128

using namespace std;

template<typename T>
class MyStack
{
private:
    T data[MAX_SIZE];
    int top;
public:
    //构造函数
    MyStack()
    {
        top = -1;
    }
    //析构函数
    ~MyStack(){}
    //拷贝构造函数
    MyStack(const MyStack& other)
    {
        memcpy(this->data,other.data,MAX_SIZE);
        this->top = other.top;
    }
    //拷贝赋值函数
    MyStack& operator=(const MyStack& other)
    {
        memcpy(this->data,other.data,MAX_SIZE);
        this->top = other.top;
        return *this;
    }
    //判空
    bool isEmpty()
    {
        return top==-1;
    }
    //访问栈顶元素
    T getTop()
    {
        if(isEmpty())
            throw -1;
        return data[top];
    }
    //判满
    bool isFull()
    {
        return top==MAX_SIZE-1;
    }
    //返回容纳的元素数
    int size()
    {
        return top+1;
    }
    //入栈
    void push(T t)
    {
        if(isFull())
            throw -1;
        top++;
        data[top] = t;
    }
    //出栈
    void pop()
    {
        if(isEmpty())
            throw -1;
        top--;
    }
    //交换内容
    void swap(MyStack& other)
    {
        T temp_arr[MAX_SIZE];
        int temp_top;

        memcpy(temp_arr, this->data, MAX_SIZE);
        memcpy(this->data, other.data, MAX_SIZE);
        memcpy(other.data, temp_arr, MAX_SIZE);

        temp_top = this->top;
        this->top = other.top;
        other.top = temp_top;
    }
};

template<typename T>
class MyQueue
{
private:
    T data[MAX_SIZE];
    int front;
    int back;
public:
    //构造函数
    MyQueue()
    {
        front = 0;
        back = 0;
    }
    //析构函数
    ~MyQueue(){}
    //拷贝构造函数
    MyQueue(const MyQueue& other)
    {
        memcpy(this->data,other.data,MAX_SIZE);
        this->front = other.front;
        this->back = other.back;
    }
    //拷贝赋值函数
    MyQueue& operator=(const MyQueue& other)
    {
        memcpy(this->data,other.data,MAX_SIZE);
        this->front = other.front;
        this->back = other.back;
        return *this;
    }
    //判空
    bool empty()
    {
        return front == back;
    }
    //判满
    bool full()
    {
        return back == MAX_SIZE;
    }
    //访问第一个元素
    T getfront()
    {
        if(empty())
            throw -1;
        return data[front];
    }
    //访问最后一个元素
    T getback()
    {
        if(empty())
            throw -1;
        return data[back-1];
    }
    //返回容纳的元素数
    int size()
    {
        return back-front;
    }
    //入队
    void push(T t)
    {
        if(full())
            throw -1;
        data[back] = t;
        back++;
    }
    //出队
    void pop()
    {
        if(empty())
            throw -1;
        front++;
    }
    //交换内容
    void swap(MyQueue& other)
    {
        T temp_arr[MAX_SIZE];
        int temp;

        memcpy(temp_arr, this->data, MAX_SIZE);
        memcpy(this->data, other.data, MAX_SIZE);
        memcpy(other.data, temp_arr, MAX_SIZE);

        temp = this->front;
        this->front = other.front;
        other.front = temp;

        temp = this->back;
        this->back = other.back;
        other.back = temp;
    }
};




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值