ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

构建数组栈类

2022-06-05 22:05:41  阅读:161  来源: 互联网

标签:栈类 template int stackTop 构建 数组 arrayStack arrayLength stack


构建数组栈类

代码

#include <iostream>

using namespace std;

// 改变一个一维数组的长度
template<class T>
void changeLength1D(T *&a, int oldLength, int newLength) {
    if (newLength < 0)
        cerr << "new Length must be > 0";
    T *temp = new T[newLength];
    int number = min(oldLength, newLength);
    copy(a, a + number, temp);
    delete[]a;
    a = temp;
}

template<class T>
class arrayStack {
public:
    explicit arrayStack(int initialCapacity = 10);

    ~arrayStack() { delete[] stack; }

    int illegalParameterValue(const string &s) {
        cout << s << endl;
        return -1;
    }

    [[maybe_unused]] [[nodiscard]] bool empty() const { return stackTop == -1; }

    [[maybe_unused]] [[nodiscard]] int size() const { return stackTop + 1; }

    [[maybe_unused]] T &top() {
        if (stackTop == -1)
            throw illegalParameterValue("Stack Empty");
        return stack[stackTop];
    }

    void pop() {
        if (stackTop == -1)
            throw illegalParameterValue("Stack Empty");
        stack[stackTop--].~T();
    }

    void push(const T &theElement);

    [[nodiscard]] int getStackTop() const;

    [[maybe_unused]] [[nodiscard]] int getArrayLength() const;

    T *getStack() const;;

private:
    int stackTop; // 当前栈顶
    int arrayLength; // 栈容量


    T *stack; // 元素数组
};

template<class T>
arrayStack<T>::arrayStack(int initialCapacity) {
    // 构造函数
    if (initialCapacity < 1)
        throw illegalParameterValue("initialCapacity must be > 0");
    arrayLength = initialCapacity;
    stack = new T[arrayLength];
    stackTop = -1;
}

template<class T>
void arrayStack<T>::push(const T &theElement) {
    // 将元素压入栈
    if (stackTop == arrayLength - 1) {
        // 空间已满,容量加倍
        changeLength1D(stack, arrayLength, arrayLength * 2);
        arrayLength *= 2;
    }
    // 在栈顶插入
    stack[++stackTop] = theElement;
}

template<class T>
int arrayStack<T>::getStackTop() const {
    return stackTop;
}

template<class T>
[[maybe_unused]] int arrayStack<T>::getArrayLength() const {
    return arrayLength;
}

template<class T>
T *arrayStack<T>::getStack() const {
    return stack;
}

template<class T>
ostream &operator<<(ostream &out, arrayStack<T> &stack) {
    int stackTop = stack.getStackTop();
    T *element = stack.getStack();
    for (int i = stackTop; i >= 0; --i)
        out << element[i] << ' ';
    return out;

}

int main() {
    auto *stack1 = new arrayStack<int>(20);
    for (int i = 0; i < 20; ++i)
        stack1->push(i);
    cout << *stack1<<"所储存数据Size:"<<stack1->size() << endl;
    stack1->pop();
    cout << *stack1<<"所储存数据Size:"<<stack1->size() << endl;
    cout<<"栈顶元素"<<endl;
    cout << stack1->top() << endl;
    return 0;
}

运行结果

19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 所储存数据Size:20
18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 所储存数据Size:19
栈顶元素
18

标签:栈类,template,int,stackTop,构建,数组,arrayStack,arrayLength,stack
来源: https://www.cnblogs.com/Reion/p/16345240.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有