Member-only story
Data Structures— Stack
8 min readMay 23, 2024
1. Overview
In computer science, a stack is a linear data structure that can only add and remove data at one end. Traditionally, this end is called the top of the stack, and the other end is called the bottom of the stack if the data cannot be manipulated, just like a stack of books in life.
Provide a stack interface first
public interface Stack<E> {
/**
* Press the element towards the top of the stack
* @param value The value to be pressed in
* @return The press is successfully returned to true, otherwise it is returned to false
*/
boolean push(E value);
/**
* Elements pop up from the top of the stack
* @return A non-empty stack returns the top element of the stack, and a empty stack returns null
*/
E pop();
/**
* Returns to the top element of the stack without popping up
* @return A non-empty stack returns the top element of the stack, and a empty stack returns null
*/
E peek();
/**
* Determine whether the stack is empty
* @return Empty return to true, otherwise return to false
*/
boolean isEmpty();
/**
* Check whether the stack is full
* @return Full return to true, otherwise return to false
*/
boolean isFull();
}