Java Data Structure Series-stack (1): sequential storage structure and operation of stacks

Source: Internet
Author: User

Package stack;/** * Stack definition: limited to the table at the end of the addition and deletion of the linear table * Stack features: Advanced after the Filo (first in the last out) * Usually we put the allowable insertion and deletion of a paragraph called the top of the stack (top), the other end * A stack called a stack that does not contain any elements is called a stack of empty stacks. We are generally referred to as a stack or a stack or a stack of delete operations we generally call the stack or stack * * Here we use arrays to implement the sequential storage structure of stacks and various operations * @author WL * */public clas s Mystack {private int[] array;//the element used to hold the stack private int top;//stack top pointer/** * default constructor for initializing the stack, where we * initialize the stack size by default to ten */public Mystack () {array=new int[10];top=-1;} /** * Custom constructor, which can be used to specify the initialization of the stack * Space size */public mystack (int capacity) {array=new int[capacity];top=-1;} /* * Stack operation */public void push (int value) {if (Isfull ()) {///stack full throw new RuntimeException ("stack full, Element '" "+value+" ' stack unsuccessful ");} Array[++top]=value;} public int Peek () {if (IsEmpty ()) {//If the stack is empty throw new RuntimeException ("element in stack is empty");} return array[top];} /** * Stack operation * @return */public int pop () {if (IsEmpty ()) {//If the stack is empty throw new RuntimeException ("element in stack is empty");} return array[top--];} /** * Determine if the stack is empty * @return */public boolean isEmpty () {return top==-1;} /** * Determine if the stack is full */public boolean isfull () {return top==array.length-1;}}

Java data Structure Series-stack (1): sequential storage structure and operation of stacks

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.