Source: https://segmentfault.com/a/1190000002516799
A stack is a linear list that restricts insertion and deletion operations to only one end of the list.
Java does not have a data structure like a stack. If you want to use a data structure like first-in-last-out (FILO), you must implement it yourself.
To implement Stack, it should at least include:
1. pop() pop operation, pop the top element of the stack.
2. push(E e) push operation
3. peek() to view the top element of the stack
4. isEmpty() stack is empty
In addition, when implementing a stack, several issues should be considered:
1. The initial size of the stack and how to add new stack space after the stack is full
2. Synchronization is required when updating the stack
There are three ways to implement it, array, container, and linked list.
data:
package gsm; import java.util.*; public class StackArray{ private int[] array;//implemented with array private int top; //Stack top pointer private final static int size = 100; public StackArray(){ array = new int[size]; top = -1; //When the stack is empty } //Push onto the stack public void push(int element){ if(top == size-1){ throw new StackOverflowError(); } else array[++top] = element; } //pop the stack public int pop(){ if( top == -1){ throw new EmptyStackException(); } return array[top--]; } //Judge whether it is empty public boolean isEmpty(){ return top == -1; } //Return the top element of the stack public Integer peek(){ if( top == -1){ throw new EmptyStackException(); } return array[top]; } } container
package gsm; public interface Stack<T> { public T pop(); public void push(T element); public boolean isEmpty(); public T peek(); } package gsm; import java.util.*; public class StackList<T> implements Stack<T> { private List list ; //implemented with container StackList(){ list = new ArrayList(); } //pop the stack public T pop(){ if(this.isEmpty() == true){ throw new EmptyStackException(); } return list.remove(list.size()-1); } //Push onto the stack public void push(T element){ list.add(element); } //Judge whether it is empty public boolean isEmpty(){ return list.size() == 0; } //Return the top element of the stack public T peek(){ if(this.isEmpty() == true){ throw new EmptyStackException(); } return list.get(list.size()-1); } } linked list
package gsm; import java.util.EmptyStackException; public class LinkedStack<T> implements Stack<T>{ //No need to store nodes in data structures such as containers or arrays //Node定义一个节点类 private static class Node<U>{ private U item; //Stored data private Node next; //Similar to pointer Node(){ this.item = null; this.next = null; } Node(U item, Node next){ this.item = item; this.next = next; } boolean end(){ return item== null && next == null; } } private Node top ; //Stack top pointer LinkedStack(){ top = new Node(); } //pop the stack public T pop(){ if(this.isEmpty() == true){ throw new EmptyStackException(); } T result = top.item; if(!top.end()) { top = top.next; } return result; } //Push onto the stack public void push(T element){ top = new Node(element, top); } //Judge whether it is empty public boolean isEmpty(){ return top.end(); } //Return the top element of the stack public T peek(){ if(this.isEmpty() == true){ throw new EmptyStackException(); } T result = top.item; return result; } } It can be found that the container is indeed a powerful tool for Java, which is convenient and efficient.
This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)". Please keep the following tags for sharing and interpretation:
Original author:Jake Tao,source:"Three implementations of Java-Stack (array, container, linked list)"