• C++ 实现模板类Stack


      Fibonacci.h使用递归方法实现斐波那契数列函数。Stack.h,定义一个Stack模板类,接收任意类型,模拟栈的使用,内部使用一个数组,通过size和capacity不断根据输入的多寡增容或缩小数组,实现push函数,压入数据到栈中,以及top函数,查看栈顶数据,pop函数,弹出栈顶数组。

    #pragma once
    
    unsigned int Fibonacci(unsigned int n)
    {
        if (n == 0)
        {
            return 0;
        }
        else if (n == 1 || n == 2)
        {
            return 1;
        }
        else
        {
            return Fibonacci(n - 1) + Fibonacci(n - 2);
        }
    }
    Fibonacci.h
    #pragma once
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    class Stack
    {
    private:
        size_t capacity;    // 可容纳对象数量
        size_t size;        // 大小,存储对象数量
        size_t ptr;            // 栈顶索引
        T *arr;                // 数组指针
    public:
        Stack() {
            capacity = 2;
            size = 0;
            ptr = -1;
            arr = new T[capacity];
        }
        ~Stack() {
            delete[]arr;
        }
    
        size_t Size() { return this->size; }
        size_t Capacity() { return this->capacity; }
    
        void push(const T&);
        void pop();
        T top() const;
        bool empty() const {
            return size == 0 ? true : false;
        }
    };
    
    template <typename T>
    void Stack<T>::push(const T& elem)
    {
        // 如果数组已满,扩容
        if (size == capacity) {
            T *tmp = arr;
            capacity *= 2;
            arr = new T[capacity];
            memcpy(arr, tmp, sizeof(T) * size);
            delete[]tmp;
        }
        ++ptr;
        arr[ptr] = elem;
        ++size;
    }
    
    template <typename T>
    void Stack<T>::pop()
    {
        if (this->empty()) {
            throw out_of_range("Stack<>::pop(): Stack is empty");
        }
        --ptr;
        --size;
        // 如果数组capacity比size的四倍还大,缩小数组大小
        if (capacity >= size * 4 && size != 0)
        {
            T *tmp = arr;
            capacity = size * 2;
            arr = new T[capacity];
            memcpy(arr, tmp, sizeof(T) * size);
            delete[]tmp;
        }
    }
    
    template <typename T>
    T Stack<T>::top() const
    {
        if (empty()) {
            throw out_of_range("Stack<>::pop(): Stack is empty");
        }
        T elem = arr[ptr];
        return elem;
    }
    Stack.h
    #include <iostream>
    #include <string>
    #include "Stack.h"
    #include "Fibonacci.h"
    
    using namespace std;
    
    void test1()
    {
        // 测试Stack实现栈类
        try {
            Stack<int> ms;
            for (int i = 0; i < 8; ++i)
            {
                ms.push(Fibonacci(i));
            }
            cout << "size: " << ms.Size() << "; capacity: " << ms.Capacity() << "; empty? " << ms.empty() << endl;
            for (int i = 0; i < 6; ++i) 
            {
                cout << ms.top() << endl;
                ms.pop();
            }
            cout << "size: " << ms.Size() << "; capacity: " << ms.Capacity() << "; empty? " << ms.empty() << endl;
            cout << ms.top() << endl;
            ms.pop();
            cout << ms.top() << endl;
            ms.pop();
            cout << "size: " << ms.Size() << "; capacity: " << ms.Capacity() << "; empty? " << ms.empty() << endl;
        }
        catch (const exception& e) {
            cout << e.what() << endl;
        }
    }
    
    void test2()
    {
        // 测试Stack实现栈类
        try {
            Stack<string> ms;
            ms.push("Hello, world!");
            ms.push("你好,世界!");
            cout << "size: " << ms.Size() << "; capacity: " << ms.Capacity() << "; empty? " << ms.empty() << endl;
            cout << ms.top() << endl;
            ms.pop();
            cout << ms.top() << endl;
            ms.pop();
            ms.pop();
        }
        catch (const exception& e) {
            cout << e.what() << endl;
        }
    }
    
    int main()
    {
        test1();
        test2();
    
        return 0;
    }
    main.cpp

  • 相关阅读:
    百奥谷
    3月13日火箭VS老鹰
    百度 hi 下载地址(内测版,正式版)
    中兴u980
    2008年清明节放假通知
    cyp740703 一个女人的自白
    黄唇鱼
    3月9日火箭vs黄蜂
    3月3日火箭vs掘金
    百度hi邀请码
  • 原文地址:https://www.cnblogs.com/noonjuan/p/12366755.html
Copyright © 2020-2023  润新知