数据结构:一个简单的栈模型

//学习数据结构后的一点心得体会

//stack.h文件:

#pragma once
#ifndef STACK_H
#define STACK_H

#include <stdexcept>
using namespace std;

template<class Entry, size_t Max>
class Stack
{
private:
size_t count;
Entry data[Max];

public:
Stack() {count = 0;}
bool empty() {return count == 0;}
void push(const Entry &e)
{
if(count == Max)throw exception("栈已满。");
data[count] = e;
count++;
}
void pop()
{
if(count == 0)throw exception("栈已空。");
count--;
}
Entry top()
{
if(count == 0)throw exception("栈已空。");
return data[count - 1];
}

};

#endif

//main.cpp文件:

#include "stack.h"

#include <iostream>
using namespace std;

int main()
{
    Stack<int, 100> psgs;
    int n;
    cout << "请输入游客人数:" << endl;
     cin >> n;
    cout << "按顺序输入乘客编号:" << endl;
    for(int i = 0; i < n; i++)
     {
         int item;
         cin >> item;
        psgs.push(item);
    }
    cout << "乘客下车次序是:" << endl;
    while(!psgs.empty())
    {
        cout << psgs.top() << ' ';
        psgs.pop();
    }
    cout << endl;
    system("pause");
    return 0;
}