Implementation of sequential Stack

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Const int maxstacksize = 20;
Template <class T>
Class seqstack
{
Public:
Seqstack (t a [], int n =-1): Top (N)
{
If (n> maxstacksize | n <0)
Cout <"N value error !! "<Endl;
Int I;
For (I = 0; I <n; I ++)
Data [I] = A [I];
}
~ Seqstack ()
{
Top =-1;
Cout <"the sequence Stack has been analyzed !! "<Endl;
}
Bool isempty ()
{
Return (Top =-1 );
}
Bool isfull ()
{
Return (Top = (MaxStackSize-1 ));
}
Void push (t x)
{
If (isfull ())
{
Cout <"the stack is full, and the stack import operation cannot be completed !! "<Endl;
Exit (1 );
}
Data [Top] = X;
Top ++;
}
Void POP (T & X)
{
If (isempty ())
{
Cout <"the stack is empty, and the operation on the outbound stack cannot be completed !! "<Endl;
Exit (1 );
}
X = data [Top];
-- Top;
}
T gettop ()
{
Return data [Top-1];
}
Void showstack ()
{
Int I;
For (I = 0; I <top; I ++)
Cout <data [I] <",";
Cout <Endl;
}
PRIVATE:
T data [maxstacksize];
Int top;
};
Int main ()
{
Int A [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Seqstack <int> seq (A, 10 );
Seq. showstack ();
Int x = 11;
Seq. Push (X );
Cout <"11:" <Endl;
Seq. showstack ();
Seq. Pop (X );
Cout <"the result after the stack is output is:" <Endl;
Seq. showstack ();
Int y = seq. gettop ();
// Seq. showstack ();
Cout <"the top element of the stack is:" <Y <Endl;
Return 0;
}

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.