How to understand the stack implementation of Java data structure and algorithm
This article introduces how to understand the stack implementation of Java data structure and algorithm. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Preface
Stack, also known as stack, is a linear table with limited operations. Qualifies linear tables that insert and delete only at the end of the table. This end is called the top of the stack, on the contrary, the other end is called the bottom of the stack. Inserting a new element into a stack is also called entering, entering, or pressing the stack. It puts the new element on top of the stack and makes it a new top element. Removing an element from a stack is also called making a stack or unstack. It removes the top element of the stack and makes its adjacent elements a new top element of the stack.
Stack diagram
Java code implementation
Public class Stack {private int maxSize; private long [] arr; private int top; / / initialize stack size public Stack (int size) {maxSize = size; arr = new long [maxSize]; top =-1;} / / on-stack public void push (long value) {arr [+ + top] = value;} / / out-of-stack public long pop () {return arr [top--] } / / public long peek () {return arr [top];} / / empty public boolean isEmpty () {return (top = =-1);} / / determine whether the stack is full of public boolean isFull () {return (top = = maxSize-1);}}
Public static void main (String [] args) {Stack stack = new Stack (10); stack.push (1); stack.push (2); stack.push (3); stack.push (4); stack.push (5)
While (! ms.isEmpty ()) {System.out.println (stack.pop ());
}
The result of running the program:
On how to understand the Java data structure and algorithm in the stack implementation is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.