Application of the stack ride on campus commuter trains

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.