每日一題18:棧

用C++寫了一個棧模板,其間用了一些《Effective C++》的準則,記錄在這裏嘍。這個類還沒有做到異常安全,以後改進!
Stack.h文件。

#ifndef _STACK_H_
#define _STACK_H_

namespace MyDataStructure
{
    template <typename T>
    class Stack
    {
    private:
        int Capacity;
        int Top;
        T* Vals;

        bool null()
        {
            return Capacity == 0;
        }
        void init(int capacity)
        {
            Capacity = capacity;
            Top = 0;
            Vals = new T[capacity];
        }
        void copy(const Stack& s)
        {
            Capacity = s.Capacity;
            Top = s.Top;
            Vals = NULL;
            if(s.Capacity != 0)
            {
                Vals = new T[Capacity];
                memcpy(Vals,s.Vals,Capacity*sizeof(T));
            }
        }

        void destroy()
        {
            if(Vals != NULL) delete []Vals;
            Top = 0;
            Capacity = 0;
        }
    public:
        Stack() : Capacity(0),Top(0),Vals(NULL){};
        Stack(int capacity)
            :Capacity(capacity),Top(0)
        {
            Vals = new T[capacity];
        }
        Stack(const Stack& s)
        {
            copy(s);
        }
        Stack& operator = (const Stack& s)
        {
            if(this == &s) return *this;
            destroy();
            copy(s);
            return *this;
        }
        ~Stack()
        {
            destroy();
        }
        bool resize(int capacity)
        {
            if(capacity <= 0) return false;
            if(capacity == Capacity) return true;
            if(!null())
            {

                T* vals = new T[capacity];
                if(capacity >= Top)
                {
                    memcpy(vals,Vals,Top*sizeof(T));
                }
                else
                {
                    memcpy(vals,Vals,capacity*sizeof(T));
                    Top = capacity;
                }
                Capacity = capacity;
                delete []Vals;
                Vals = vals;
            }
            else
                init(capacity);
            return true;
        }
        bool push(T val)
        {
            if(!full() && !null())
            {
                Vals[Top++] = val;
                return true;
            }
            return false;
        }
        bool pop(T& val)
        {
            if(!null() && !empty())
            {
                val = Vals[--Top];
                return true;
            }
            return false;
        }
        bool top(T &val)
        {
            if(!null() && !empty())
            {
                val = Vals[Top - 1];
                return true;
            }
            return false;
        }
        void clear()
        {
            Top = 0;
        }
        void size()
        {
            return Top;
        }
        bool empty()
        {
            return Top == 0;
        }
        bool full()
        {
            return Top == Capacity;
        }

    };

}
#endif 

下面是測試函數:
StackTest.cpp

// StackTest.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include "Stack.h"
#include <iostream>

using namespace MyDataStructure;
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    Stack<int> s1(10);
    for (int i = 1; i < 11; ++i)
    {
        s1.push(i);
    }

    Stack<int> s2(s1);
    s1.resize(20);
    for (int i = 1; i < 11; ++i)
    {
        int val;
        if(s1.pop(val))
        {
            cout<<val<<' ';
        }
    }
    cout<<endl;

    for (int i = 1; i < 11; ++i)
    {
        int val;
        if(s2.pop(val))
        {
            cout<<val<<' ';
        }
    }
    cout<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章