单向链表
  1. public static class Node {
  2. public int value;
  3. public Node next;
  4. public Node(int data) {
  5. value = data;
  6. }
  7. }

双向链表

  1. public static class DoubleNode {
  2. public int value;
  3. public DoubleNode last;
  4. public DoubleNode next;
  5. public DoubleNode(int data) {
  6. value = data;
  7. }
  8. }

栈和队列的实际实现

用数组实现栈(数组+index)
  1. /**
  2. * @title: Algorithm
  3. * @description: 用数组来实现栈(数组+index)
  4. * @author:
  5. * @date: 2022/07/01 21:49
  6. */
  7. public class Code02_ArrayImplementsStack {
  8. private String[] arr=new String[5];
  9. private int index=0;
  10. public void put(String a){
  11. if (index>arr.length){
  12. System.out.println("超过数组长度");
  13. return;
  14. }
  15. arr[index] = a;
  16. index++;
  17. }
  18. public String take(){
  19. if(index <=0){
  20. System.out.println("已经空了,没得取");
  21. return "";
  22. }
  23. index--;
  24. return arr[index];
  25. }
  26. public static void main(String[] args) {
  27. Code02_ArrayImplementsStack c = new Code02_ArrayImplementsStack();
  28. c.put("0");
  29. c.put("1");
  30. System.out.println(c.index);
  31. c.take();
  32. System.out.println(c.index);
  33. }
  34. }

用数组实现队列(环形数组+begin+end+size)
  1. /**
  2. * @title: Algorithm
  3. * @description: 用数组实现队列(循环数组)
  4. * @author:
  5. * @date: 2022/07/01 22:40
  6. */
  7. public class Code03_RingArray {
  8. private static class MQueue{
  9. private int[] arr;
  10. private int begin; // 取的时候看它
  11. private int end; // 放的时候看它
  12. private int size;
  13. private int limit; // 数组的容量长度
  14. public MQueue(int limit) {
  15. arr = new int[limit];
  16. this.begin = 0;
  17. this.end =0;
  18. this.size = 0;
  19. this.limit = limit;
  20. }
  21. public void put(int a){
  22. if (size >= limit){
  23. throw new RuntimeException("队列已满,禁止put操作");
  24. }
  25. size++;
  26. arr[end] = a;
  27. end = nextIndex(end);
  28. }
  29. public int pop(){
  30. if (size==0){
  31. throw new RuntimeException("队列已空,没的弹了");
  32. }
  33. size--;
  34. int result = arr[begin];
  35. begin = nextIndex(begin);
  36. return result;
  37. }
  38. public int nextIndex(int i){
  39. if (i >= limit){
  40. i = 0;
  41. }else {
  42. i++;
  43. }
  44. return i;
  45. }
  46. }
  47. public static void main(String[] args) {
  48. MQueue queue = new MQueue(5);
  49. queue.put(1);
  50. queue.put(2);
  51. queue.put(3);
  52. queue.put(4);
  53. queue.pop();
  54. queue.put(5);
  55. //queue.put(6);
  56. System.out.println(Arrays.toString(queue.arr));
  57. }
  58. }

返回栈中最小元素功能

用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里。

递归

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