I. Preface
Computer programs are inseparable from algorithms and data structures. Data structures are a discipline that is produced for computers to store and use data in a more efficient, simple and convenient way. This paper introduces the implementation of stack and queue
Two, diagram
III. linear table
1. Sequential storage structure: the data elements of linear table are successively stored in a storage unit with continuous address
2. Chain storage structure: a set of arbitrary storage units is used to store the data elements of a linear table. This set of storage units can be continuous or discontinuous, and there is no linear relationship between space and memory
Four, stack
Stack
1. Linear tables that can only be inserted and deleted at one end
2. Functions realized
- push: add data at the top level.
- pop: returns and removes the top-level data.
- peek: returns the value of the topmost data, but does not remove it.
- Empty: returns a Boolean value indicating whether the current stack is empty.
2-1. Initialization
private int[] arr; //Constant in uppercase private final static int SIZE = 1; //Current pointer to stack private int index; //Constructor has no parameters public StackDemo() { arr = new int[SIZE]; index = -1; }
2-2,push
//Push private void push(int target){ if (index == SIZE){ throw new StackOverflowError(); }else { //At the beginning, it's - 1, it needs to be added before arr[++index] = target; } }
2-3,peek
//Back to top of stack element private int peek(){ if (index == -1){ throw new StackOverflowError(); }else { return arr[index]; } }
2-4,empty
//Sentence blank private boolean empty(){ if (index == -1){ return true; } return false; }
3. Code implementation
import java.util.Arrays; /** * * @author buer * @date 2019/1/20 */ public class StackDemo { private int[] arr; //Constant in uppercase private final static int SIZE = 1; //Current pointer to stack private int index; //Constructor has no parameters public StackDemo() { arr = new int[SIZE]; index = -1; } //Push private void push(int target){ if (index == SIZE){ throw new StackOverflowError(); }else { //At the beginning, it's - 1, it needs to be added before arr[++index] = target; } } //Stack out private int pop(){ if (index == -1){ throw new StackOverflowError(); }else { return arr[index--]; } } //Back to top of stack element private int peek(){ if (index == -1){ throw new StackOverflowError(); }else { return arr[index]; } } //Sentence blank private boolean empty(){ if (index == -1){ return true; } return false; } public static void main(String[] args) { StackDemo stackDemo = new StackDemo(); stackDemo.push(1); System.out.println(stackDemo.toString()); stackDemo.pop(); System.out.println(stackDemo.toString()); } @Override public String toString() { return "StackDemo{" + "arr=" + Arrays.toString(arr) + ", index=" + index + '}'; } }
application
1. Bracket matching
2. Infix expression (human thinking) and suffix expression (computer calculation)
3, recursion
4. Browser forward and backward function
Reference material
https://zh.wikipedia.org
https://www.zhihu.com/question/21318658
http://www.ruanyifeng.com/blog/2013/11/stack.html