From 49bd9a75ab094fd136770de56b68d75f33fca745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=B6=E9=97=A8=E5=B0=8F=E8=B1=86=E5=AD=90?= <53906165+Nezuko-Wen@users.noreply.github.com> Date: Sun, 7 Aug 2022 19:15:16 +0800 Subject: [PATCH] Update README.md --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 47d6c69..6e976e4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,50 @@ public class OneCount { # 队列 ### 用队列实现栈 ### 环形队列 +```java +public class CircularQueue { + private int[] table; + private int size = 0; //元素个数 + private int front = 0; //队头 + private int rear = 0; //队尾 + private final int limit; + + private static final int DEFAULT_LIMIT = 10; + + public CircularQueue() { + this.limit = DEFAULT_LIMIT; + table = new int[limit]; + } + + public CircularQueue(int limit) { + this.limit = limit; + table = new int[limit]; + } + + public void put(int val) { + if (size == limit) { + throw new RuntimeException("当前队列已满"); + } + size ++; + table[rear] = val; + rear = rear == limit - 1 ? 0 : rear + 1; + } + + public int pop() { + if (size == 0) { + throw new RuntimeException("当前队列为空"); + } + size --; + int ret = table[front]; + front = front == limit - 1 ? 0 : front + 1; + return ret; + } + + public boolean isEmpty() { + return size == 0; + } +} +``` # hash # 递归 @@ -95,4 +139,4 @@ eg: [1,3,4,2,5]
4的小数:1,3
2的小数:1
5的小数:1,3,4,2
-小数和为:1+1+3+1+1+3+4+2 \ No newline at end of file +小数和为:1+1+3+1+1+3+4+2