#include <iostream>
using namespace Std;
typedef int Stackentry;
const int maxstack = maximum size of 100;//stack
Class stack{
Public
Stack ();
void Pop ();
void push (const stackentry &item);
void Top (Stackentry &item) const;
BOOL empty () const;
Private
int count;
Stackentry Data[maxstack];
};
void stack::p ush (const stackentry &item)//If the stack is not full, the element item is pressed into the top of the stack, otherwise the error
{
if (Count>=maxstack)
cout<< "Stack Overflow, unable to press into the element. ";
Else
data[count++] = Item;
}
void stack::p op ()//If the stack is not empty, the top element of the stack is deleted, otherwise the error
{
if (count = = 0)
cout<< "Stack Overflow, unable to eject the element. ";
Else
--count;
}
void Stack::top (Stackentry &item) const//If the stack is not empty, remove the top element of the stack and put it in the item, otherwise the error
{
if (count = = 0)
cout<< "Stack underflow, unable to read elements. ";
Else
item = data[count-1];
}
BOOL Stack::empty () const//Judging whether the stack is empty
{
if (Count > 0)
return false;
return true;
}
Stack::stack ()//Build function, initialize an empty stack
{
Count = 0;
}
Ride on campus commuter trains
int main ()
Input: The user provides a value of N, representing the number of n passengers and N passengers
Output: Deliver the passenger's number backwards
{
int n;
int item;
Stack passenger; Define a stack, the name is passagers
cout<< "Enter passenger number n" <<endl;
cin>>n;
cout<< "Enter passenger number by boarding order" <<endl;
for (int i = 0;i<n;i++)
{
cin>>item;
Passenger.push (item);
}
cout<<endl<<endl;
cout<< "The order of the passengers alight is:";
while (!passenger.empty ()) {
Passenger.top (item);
cout<<item<< "";
Passenger.pop ();
}
cout<<endl;
return 0;
}
Application of the stack ride on campus commuter trains