Sequential stack--java Implementation

Source: Internet
Author: User

1 /*Stacks and queues: logical structure is a linear table with limited operational constraints2 * Stack: Features advanced after out, only allowed on the top of the stack operation3 * How stacks are implemented: sequential stacks and chain stacks4 * Common operations: Into the stack, out of the stack, get the top of the stack, empty, sentenced to full, the capacity of the stack5 * Application of Stack6 * * Reverse-order related operations7 * * . Delimiter matching8  * */9 //Sequential StacksTen  Public classMystack { One     Private Long[] arr; A     Private intMaxSize;//capacity of the stack -     Private intTop//stack Top pointer -      the      PublicMystack (ints) { -MaxSize =s; -arr =New Long[maxSize]; -top =-1; +     } -      +      Public BooleanIsfull () { A         returntop = = MaxSize-1; at     } -      -      Public BooleanIsEmpty () { -         returntop = =-1; -     } -      in     //the stack must be judged full first. -      Public voidPushLongkey) { to         if(!Isfull ()) { +Arr[++top] =key; -         } the     } *      $     //before the stack to determine whether it is emptyPanax Notoginseng      Public Longpop () { -         Longnum = 0; the         if(!IsEmpty ()) { +num = arr[top--]; A         } the         returnnum;  +     } -      $     //get top of stack element $      Public LongGetpeek () { -         returnArr[top]; -     } the      -     //displaying the elements of a stackWuyi      Public voidDiaplaystack () { the          for(inti = top;i >= 0; i--){ -System.out.print (Arr[i] + ""); Wu         } - System.out.println (); About     } $      -      Public intsize () { -         returnTop + 1; -     } A      +      Public LongGETPEEKN (intN) { the         returnArr[n]; -     } $      the      Public voidDisplaystack (String s) { the System.out.println (s); theSystem.out.println ("stack----bottom to top"); the          for(inti = 0; I < size (); i++){ -System.out.print (Getpeekn (i) + ""); in         } the System.out.println (); the     } About      the}

Sequential stack--java Implementation

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.