9月13日作业

思维导图

用模板实现栈

#include <iostream>

using namespace std;

template <typename T, int MaxSize>
class Stack {
public:
    //构造函数
    Stack() : top(-1) {}
    //入栈
    void push(const T& value);
    //出栈
    T pop();
    //判空
    bool empty() const;
    //判满
    bool full()const;
    //清空栈
    bool clear();
    //获取栈顶元素
    T stacktop();
    //求栈的大小
    int stacksize();
    //输出栈元素
    bool show();
private:
    T data[MaxSize];
    int top;
};

int main() {
    Stack<int, 3> intStack;
    intStack.push(1);
    intStack.push(2);
    intStack.push(3);
    intStack.pop();
    intStack.show();

    Stack<double, 5> doubleStack;
    doubleStack.push(1.1);
    doubleStack.push(2.2);
    doubleStack.push(3.3);
    doubleStack.pop();
    doubleStack.show();


    return 0;
}

//入栈
template <typename T, int MaxSize>
void Stack<T, MaxSize>::push(const T& value) {
    if (top == MaxSize - 1) {
        throw std::overflow_error("该栈已满");
    }
    data[++top] = value;
}
//出栈
template <typename T, int MaxSize>
T Stack<T, MaxSize>::pop() {
    if (empty()) {
        throw std::underflow_error("该栈为空");
    }
    return data[top--];
}
//判空
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::empty() const {
    return top == -1;
}

//判满
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::full()const
{
    return top = MaxSize-1;
}

//清空栈
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::clear()
{
    if(empty()){
        cout<<"该栈为空"<<endl;
        return false;
    }else {
        top = -1;
        return true;
    }
}

//获取栈顶元素
template <typename T, int MaxSize>
T Stack<T, MaxSize>::stacktop()
{
    return data[top];
}

//求栈的大小
template <typename T, int MaxSize>
int Stack<T, MaxSize>::stacksize()
{
    return top+1;
}

//输出栈元素
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::show()
{
    if(empty()){
        cout<<"该栈为空"<<endl;
        return false;
    }else {
        for(int i=0;i<=top; i++){
            cout<<data[i]<<" ";
        }
        cout<<endl;
        return true;
    }
}

用模板实现队列

#include <iostream>
#define MAXSIZE 16

using namespace std;
template <typename T, int MaxSize>
class LoopQueue
{
public:
    //构造函数
    LoopQueue():FrontIndex(0), RearIndex(0){};
    //入队列
    bool push(T value);
    //出队列并返回队列顶元素
    T pop();
    //清空队列
    bool clear();
    //判空
    bool isEmpty();
    //判满
    bool isFull();
    //求队列的大小
    int LoopQueuesize();
    //输出队列元素
    bool show();
private:
    T Data[MaxSize];
    int FrontIndex;
    int RearIndex;
};

int main()
{
    LoopQueue<int, 5> intL;
    intL.push(5);
    intL.push(7);
    intL.push(10);
    intL.pop();
    intL.show();

    LoopQueue<double, 5> doubleL;
    doubleL.push(5.5);
    doubleL.push(7.4);
    doubleL.push(10.3);
    doubleL.pop();
    doubleL.show();

    return 0;
}

//入队列
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::push(T value)
{
    if(isFull()){
        cout<<"该队列已满"<<endl;
        return false;
    }else{
        Data[RearIndex] = value;
        RearIndex++;
        return true;
    }
}

//出队列并返回队列顶元素
template <typename T, int MaxSize>
T LoopQueue<T, MaxSize>::pop()
{
    if(isEmpty()){
        throw std::underflow_error("该队列为空");
    }else {
        int Frontvalue = Data[RearIndex];
        FrontIndex++;
        return Frontvalue;
    }
}

//清空队列
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::clear()
{
    if(isEmpty()){
        cout<<"该队列为空"<<endl;
        return false;
    }else {
        FrontIndex = -1;
        RearIndex = -1;
        return true;
    }
}

//判空
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::isEmpty()
{
    return FrontIndex==RearIndex;
}

//判满
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::isFull()
{
    return (RearIndex+1)%MAXSIZE==FrontIndex;
}

//求队列的大小
template <typename T, int MaxSize>
int LoopQueue<T, MaxSize>::LoopQueuesize()
{
    return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
}

//输出队列元素
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::show()
{
    if(isEmpty()){
        cout<<"该队列为空"<<endl;
        return false;
    }else {
        int i=FrontIndex;
        cout<<"该队列为>>";
        while(1)
        {
            cout<<Data[i]<<" ";
            i++;
            if(MAXSIZE==i)
            {
                i=0;
            }
            if(i==RearIndex)
            {
                break;
            }
        }
        cout<<endl;
        return true;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值