单向链表
public static class Node {public int value;public Node next;public Node(int data) {value = data;}}
双向链表
public static class DoubleNode {public int value;public DoubleNode last;public DoubleNode next;public DoubleNode(int data) {value = data;}}
栈和队列的实际实现
用数组实现栈(数组+index)
/*** @title: Algorithm* @description: 用数组来实现栈(数组+index)* @author:* @date: 2022/07/01 21:49*/public class Code02_ArrayImplementsStack {private String[] arr=new String[5];private int index=0;public void put(String a){if (index>arr.length){System.out.println("超过数组长度");return;}arr[index] = a;index++;}public String take(){if(index <=0){System.out.println("已经空了,没得取");return "";}index--;return arr[index];}public static void main(String[] args) {Code02_ArrayImplementsStack c = new Code02_ArrayImplementsStack();c.put("0");c.put("1");System.out.println(c.index);c.take();System.out.println(c.index);}}
用数组实现队列(环形数组+begin+end+size)
/*** @title: Algorithm* @description: 用数组实现队列(循环数组)* @author:* @date: 2022/07/01 22:40*/public class Code03_RingArray {private static class MQueue{private int[] arr;private int begin; // 取的时候看它private int end; // 放的时候看它private int size;private int limit; // 数组的容量长度public MQueue(int limit) {arr = new int[limit];this.begin = 0;this.end =0;this.size = 0;this.limit = limit;}public void put(int a){if (size >= limit){throw new RuntimeException("队列已满,禁止put操作");}size++;arr[end] = a;end = nextIndex(end);}public int pop(){if (size==0){throw new RuntimeException("队列已空,没的弹了");}size--;int result = arr[begin];begin = nextIndex(begin);return result;}public int nextIndex(int i){if (i >= limit){i = 0;}else {i++;}return i;}}public static void main(String[] args) {MQueue queue = new MQueue(5);queue.put(1);queue.put(2);queue.put(3);queue.put(4);queue.pop();queue.put(5);//queue.put(6);System.out.println(Arrays.toString(queue.arr));}}
返回栈中最小元素功能
用2个栈,1个是数据存储栈,另外一个是存放最小值栈。
每次压栈,数据栈肯定要压入,如果min栈是空,直接压入。如果min栈不为空,比较min栈顶数和压入的数大小。如果压入的数大,那min重复压入栈顶的数。
每次出战,min栈也同步跟着弹出即可。
/**
* @title: Algorithm
* @description: 获取栈最小元素
* @author:
* @date: 2022/07/03 16:17
*/
public class Code05_GetMinStack {
private static class MyStack{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack() {
this.stackData = new Stack<>();
this.stackMin = new Stack<>();
}
private void push(Integer a){
if (this.stackMin.isEmpty()){
this.stackMin.push(a);
}else {
this.stackMin.push(a< this.stackMin.peek() ? a: this.stackMin.peek());
}
this.stackData.push(a);
}
private int pop(){
if (this.stackData.isEmpty()){
throw new RuntimeException("stackData isEmpty!");
}
Integer value = this.stackData.pop();
this.stackMin.pop();
return value;
}
}
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(3);
stack.push(2);
stack.push(4);
System.out.println(stack.stackMin.peek());
stack.push(1);
System.out.println(stack.stackMin.peek());
System.out.println(stack.stackData);
System.out.println(stack.stackMin);
stack.pop();
System.out.println(stack.stackMin.peek());
stack.pop();
System.out.println(stack.stackMin.peek());
}
}
用栈实现队列
用2个栈,注意:①必须一次性倒完数据;② 如果pop栈没有拿完数据,那就不能导。
用队列实现栈
用2个队列,来回倒腾。每次pop,就把队列A的N-1都倒腾到另外一个队列B,然后弹出。然后更换两个队列的指针,下次push,放在队列B里。
递归

待更新下边的。
哈希表
有序表
