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