-----------------------------------------------
stack.h
-----------------------------------------------

template <class format>

class stack
{
private:
    format *S; //堆疊陣列 - 後進先出
    int top;   //陣列大小
public:
    stack::stack()    //未定義大小預設 100
    {
        top = 0;
        S = new format[100];
    }
    
    stack::stack(int size)
    {
        top = 0;
        S = new format[size];
    }
    
    stack::~stack()
    {
        delete S;
    }
    
    void stack::push(format data) //放置資料
    {
        if(top < 100) S[top++] = data;
    }
    
    format stack::pop() //拿取資料
    {
        if(top > 0) return S[--top];  //先減 1 再傳回
        return(0);
    }
};

-----------------------------------------------

 

-----------------------------------------------
awei.cpp
-----------------------------------------------

// awei.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include "stack.h"
#include "iostream"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 stack <int> A(10); //可自定義資料型態
 A.push('A');
 cout << A.pop() << endl;
 return 0;
}

-----------------------------------------------

arrow
arrow
    全站熱搜

    AwEi 發表在 痞客邦 留言(0) 人氣()