Sequential stacks (array implementations)

Source: Internet
Author: User

The above code implements the stack's IsEmpty (), Isfull (), clear (), push (), pop (), Peek () methods. The sequential stack must be checked both under overflow (underflow) and on Overflow (overflow).

 Public classStack {Private int[] stack; Private Static Final intDefaultSize = 100; Private intsize; Private intTopIndex;  PublicStack () {setUp (defaultsize); }     PublicStack (intSZ)    {setUp (SZ); }     Public voidSetUp (intSZ) {Size= SZ; TopIndex = 0; stack =New int[size]; }     Public BooleanIsEmpty () {returnTopIndex = = 0; }     Public BooleanIsfull () {returnTopIndex = =size; }     Public voidClear () {TopIndex= 0; }     Public voidPushintXthrowsException {if(Isfull ())Throw NewException ("Overflow"); Elsestack[topindex++] =x; }     Public intPop ()throwsException {if(IsEmpty ())Throw NewException ("Underflow"); Else returnstack[--TopIndex]; }     Public intPeek ()throwsException {if(IsEmpty ())Throw NewException ("Underflow"); Else returnStack[topindex-1]; }}

Sequential stacks (array implementations)

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.